blob: d4e188124308561f91289cba885a73b9532ed9b6 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief Custom DAG lowering for SI
12//
13//===----------------------------------------------------------------------===//
14
NAKAMURA Takumi45e0a832014-07-20 11:15:07 +000015#ifdef _MSC_VER
16// Provide M_PI.
17#define _USE_MATH_DEFINES
18#include <cmath>
19#endif
20
Christian Konig99ee0f42013-03-07 09:04:14 +000021#include "AMDGPU.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000022#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000023#include "AMDGPUSubtarget.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000024#include "SIISelLowering.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025#include "SIInstrInfo.h"
26#include "SIMachineFunctionInfo.h"
27#include "SIRegisterInfo.h"
Alexey Samsonova253bf92014-08-27 19:36:53 +000028#include "llvm/ADT/BitVector.h"
Matt Arsenault9a10cea2016-01-26 04:29:24 +000029#include "llvm/ADT/StringSwitch.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000030#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
32#include "llvm/CodeGen/MachineRegisterInfo.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Wei Ding07e03712016-07-28 16:42:13 +000034#include "llvm/CodeGen/Analysis.h"
Oliver Stannard7e7d9832016-02-02 13:52:43 +000035#include "llvm/IR/DiagnosticInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000036#include "llvm/IR/Function.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000037
38using namespace llvm;
39
Tom Stellardf110f8f2016-04-14 16:27:03 +000040static unsigned findFirstFreeSGPR(CCState &CCInfo) {
41 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
42 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) {
43 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) {
44 return AMDGPU::SGPR0 + Reg;
45 }
46 }
47 llvm_unreachable("Cannot allocate sgpr");
48}
49
Matt Arsenault43e92fe2016-06-24 06:30:11 +000050SITargetLowering::SITargetLowering(const TargetMachine &TM,
51 const SISubtarget &STI)
Eric Christopher7792e322015-01-30 23:24:40 +000052 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000053 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000054 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000055
Tom Stellard334b29c2014-04-17 21:00:09 +000056 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000057 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000058
Tom Stellard436780b2014-05-15 14:41:57 +000059 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
60 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
61 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000062
Matt Arsenault61001bb2015-11-25 19:58:34 +000063 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
64 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
65
Tom Stellard436780b2014-05-15 14:41:57 +000066 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
67 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000068
Tom Stellardf0a21072014-11-18 20:39:39 +000069 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000070 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
71
Tom Stellardf0a21072014-11-18 20:39:39 +000072 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000073 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000074
Eric Christopher23a3a7c2015-02-26 00:00:24 +000075 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000076
Tom Stellard35bb18c2013-08-26 15:06:04 +000077 // We need to custom lower vector stores from local memory
Matt Arsenault71e66762016-05-21 02:27:49 +000078 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000079 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000080 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
81 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000082 setOperationAction(ISD::LOAD, MVT::i1, Custom);
Matt Arsenault2b957b52016-05-02 20:07:26 +000083
Matt Arsenaultbcdfee72016-05-02 20:13:51 +000084 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000085 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
86 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
87 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
88 setOperationAction(ISD::STORE, MVT::i1, Custom);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +000089
Matt Arsenault71e66762016-05-21 02:27:49 +000090 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
91 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000092 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
93
94 setOperationAction(ISD::SELECT, MVT::i1, Promote);
Tom Stellard0ec134f2014-02-04 17:18:40 +000095 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +000096 setOperationAction(ISD::SELECT, MVT::f64, Promote);
97 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +000098
Tom Stellard3ca1bfc2014-06-10 16:01:22 +000099 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
100 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
101 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
102 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Matt Arsenault71e66762016-05-21 02:27:49 +0000103 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000104
Tom Stellardd1efda82016-01-20 21:48:24 +0000105 setOperationAction(ISD::SETCC, MVT::i1, Promote);
Tom Stellard83747202013-07-18 21:43:53 +0000106 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
107 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
108
Matt Arsenault71e66762016-05-21 02:27:49 +0000109 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
110 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000111
Matt Arsenault4e466652014-04-16 01:41:30 +0000112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000114 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
115 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000116 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
119
Tom Stellard9fa17912013-08-14 23:24:45 +0000120 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000121 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000122 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
123
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000124 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000125 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000126 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
127 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
128 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
129 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000130
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000131 // We only support LOAD/STORE and vector manipulation ops for vectors
132 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000133 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000134 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000135 switch (Op) {
Tom Stellard967bf582014-02-13 23:34:15 +0000136 case ISD::LOAD:
137 case ISD::STORE:
138 case ISD::BUILD_VECTOR:
139 case ISD::BITCAST:
140 case ISD::EXTRACT_VECTOR_ELT:
141 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000142 case ISD::INSERT_SUBVECTOR:
143 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000144 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000145 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000146 case ISD::CONCAT_VECTORS:
147 setOperationAction(Op, VT, Custom);
148 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000149 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000150 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000151 break;
152 }
153 }
154 }
155
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000156 // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that
157 // is expanded to avoid having two separate loops in case the index is a VGPR.
158
Matt Arsenault61001bb2015-11-25 19:58:34 +0000159 // Most operations are naturally 32-bit vector operations. We only support
160 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
161 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
162 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
163 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
164
165 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
166 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
167
168 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
169 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
170
171 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
172 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
173 }
174
Matt Arsenault71e66762016-05-21 02:27:49 +0000175 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
176 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
177 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
178 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000179
Tom Stellard354a43c2016-04-01 18:27:37 +0000180 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
181 // and output demarshalling
182 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
183 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
184
185 // We can't return success/failure, only the old value,
186 // let LLVM add the comparison
187 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand);
188 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand);
189
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000190 if (getSubtarget()->hasFlatAddressSpace()) {
Matt Arsenault99c14522016-04-25 19:27:24 +0000191 setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom);
192 setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom);
193 }
194
Matt Arsenault71e66762016-05-21 02:27:49 +0000195 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
196 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
197
198 // On SI this is s_memtime and s_memrealtime on VI.
199 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
Matt Arsenault0bb294b2016-06-17 22:27:03 +0000200 setOperationAction(ISD::TRAP, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000201
202 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
203 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
204
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000205 if (Subtarget->getGeneration() >= SISubtarget::SEA_ISLANDS) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000206 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
207 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
208 setOperationAction(ISD::FRINT, MVT::f64, Legal);
209 }
210
211 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
212
213 setOperationAction(ISD::FSIN, MVT::f32, Custom);
214 setOperationAction(ISD::FCOS, MVT::f32, Custom);
215 setOperationAction(ISD::FDIV, MVT::f32, Custom);
216 setOperationAction(ISD::FDIV, MVT::f64, Custom);
217
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000218 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000219 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000220 setTargetDAGCombine(ISD::FMINNUM);
221 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000222 setTargetDAGCombine(ISD::SMIN);
223 setTargetDAGCombine(ISD::SMAX);
224 setTargetDAGCombine(ISD::UMIN);
225 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000226 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000227 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000228 setTargetDAGCombine(ISD::OR);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000229 setTargetDAGCombine(ISD::XOR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000230 setTargetDAGCombine(ISD::UINT_TO_FP);
Matt Arsenault9cd90712016-04-14 01:42:16 +0000231 setTargetDAGCombine(ISD::FCANONICALIZE);
Matt Arsenault364a6742014-06-11 17:50:44 +0000232
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000233 // All memory operations. Some folding on the pointer operand is done to help
234 // matching the constant offsets in the addressing modes.
235 setTargetDAGCombine(ISD::LOAD);
236 setTargetDAGCombine(ISD::STORE);
237 setTargetDAGCombine(ISD::ATOMIC_LOAD);
238 setTargetDAGCombine(ISD::ATOMIC_STORE);
239 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
240 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
241 setTargetDAGCombine(ISD::ATOMIC_SWAP);
242 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
243 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
244 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
245 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
246 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
247 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
248 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
249 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
250 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
251 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
252
Christian Konigeecebd02013-03-26 14:04:02 +0000253 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000254}
255
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000256const SISubtarget *SITargetLowering::getSubtarget() const {
257 return static_cast<const SISubtarget *>(Subtarget);
258}
259
Tom Stellard0125f2a2013-06-25 02:39:35 +0000260//===----------------------------------------------------------------------===//
261// TargetLowering queries
262//===----------------------------------------------------------------------===//
263
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000264bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
265 const CallInst &CI,
266 unsigned IntrID) const {
267 switch (IntrID) {
268 case Intrinsic::amdgcn_atomic_inc:
269 case Intrinsic::amdgcn_atomic_dec:
270 Info.opc = ISD::INTRINSIC_W_CHAIN;
271 Info.memVT = MVT::getVT(CI.getType());
272 Info.ptrVal = CI.getOperand(0);
273 Info.align = 0;
274 Info.vol = false;
275 Info.readMem = true;
276 Info.writeMem = true;
277 return true;
278 default:
279 return false;
280 }
281}
282
Matt Arsenaulte306a322014-10-21 16:25:08 +0000283bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
284 EVT) const {
285 // SI has some legal vector types, but no legal vector operations. Say no
286 // shuffles are legal in order to prefer scalarizing some vector operations.
287 return false;
288}
289
Tom Stellard70580f82015-07-20 14:28:41 +0000290bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
291 // Flat instructions do not have offsets, and only have the register
292 // address.
293 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
294}
295
Matt Arsenault711b3902015-08-07 20:18:34 +0000296bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
297 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
298 // additionally can do r + r + i with addr64. 32-bit has more addressing
299 // mode options. Depending on the resource constant, it can also do
300 // (i64 r0) + (i32 r1) * (i14 i).
301 //
302 // Private arrays end up using a scratch buffer most of the time, so also
303 // assume those use MUBUF instructions. Scratch loads / stores are currently
304 // implemented as mubuf instructions with offen bit set, so slightly
305 // different than the normal addr64.
306 if (!isUInt<12>(AM.BaseOffs))
307 return false;
308
309 // FIXME: Since we can split immediate into soffset and immediate offset,
310 // would it make sense to allow any immediate?
311
312 switch (AM.Scale) {
313 case 0: // r + i or just i, depending on HasBaseReg.
314 return true;
315 case 1:
316 return true; // We have r + r or r + i.
317 case 2:
318 if (AM.HasBaseReg) {
319 // Reject 2 * r + r.
320 return false;
321 }
322
323 // Allow 2 * r as r + r
324 // Or 2 * r + i is allowed as r + r + i.
325 return true;
326 default: // Don't allow n * r
327 return false;
328 }
329}
330
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000331bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
332 const AddrMode &AM, Type *Ty,
333 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000334 // No global is ever allowed as a base.
335 if (AM.BaseGV)
336 return false;
337
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000338 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000339 case AMDGPUAS::GLOBAL_ADDRESS: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000340 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) {
Tom Stellard70580f82015-07-20 14:28:41 +0000341 // Assume the we will use FLAT for all global memory accesses
342 // on VI.
343 // FIXME: This assumption is currently wrong. On VI we still use
344 // MUBUF instructions for the r + i addressing mode. As currently
345 // implemented, the MUBUF instructions only work on buffer < 4GB.
346 // It may be possible to support > 4GB buffers with MUBUF instructions,
347 // by setting the stride value in the resource descriptor which would
348 // increase the size limit to (stride * 4GB). However, this is risky,
349 // because it has never been validated.
350 return isLegalFlatAddressingMode(AM);
351 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000352
Matt Arsenault711b3902015-08-07 20:18:34 +0000353 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000354 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000355 case AMDGPUAS::CONSTANT_ADDRESS: {
356 // If the offset isn't a multiple of 4, it probably isn't going to be
357 // correctly aligned.
Matt Arsenault3cc1e002016-08-13 01:43:51 +0000358 // FIXME: Can we get the real alignment here?
Matt Arsenault711b3902015-08-07 20:18:34 +0000359 if (AM.BaseOffs % 4 != 0)
360 return isLegalMUBUFAddressingMode(AM);
361
362 // There are no SMRD extloads, so if we have to do a small type access we
363 // will use a MUBUF load.
364 // FIXME?: We also need to do this if unaligned, but we don't know the
365 // alignment here.
366 if (DL.getTypeStoreSize(Ty) < 4)
367 return isLegalMUBUFAddressingMode(AM);
368
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000369 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000370 // SMRD instructions have an 8-bit, dword offset on SI.
371 if (!isUInt<8>(AM.BaseOffs / 4))
372 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000373 } else if (Subtarget->getGeneration() == SISubtarget::SEA_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000374 // On CI+, this can also be a 32-bit literal constant offset. If it fits
375 // in 8-bits, it can use a smaller encoding.
376 if (!isUInt<32>(AM.BaseOffs / 4))
377 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000378 } else if (Subtarget->getGeneration() == SISubtarget::VOLCANIC_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000379 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
380 if (!isUInt<20>(AM.BaseOffs))
381 return false;
382 } else
383 llvm_unreachable("unhandled generation");
384
385 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
386 return true;
387
388 if (AM.Scale == 1 && AM.HasBaseReg)
389 return true;
390
391 return false;
392 }
393
394 case AMDGPUAS::PRIVATE_ADDRESS:
Matt Arsenault711b3902015-08-07 20:18:34 +0000395 return isLegalMUBUFAddressingMode(AM);
396
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000397 case AMDGPUAS::LOCAL_ADDRESS:
398 case AMDGPUAS::REGION_ADDRESS: {
399 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
400 // field.
401 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
402 // an 8-bit dword offset but we don't know the alignment here.
403 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000404 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000405
406 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
407 return true;
408
409 if (AM.Scale == 1 && AM.HasBaseReg)
410 return true;
411
Matt Arsenault5015a892014-08-15 17:17:07 +0000412 return false;
413 }
Tom Stellard70580f82015-07-20 14:28:41 +0000414 case AMDGPUAS::FLAT_ADDRESS:
Matt Arsenault7d1b6c82016-04-29 06:25:10 +0000415 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
416 // For an unknown address space, this usually means that this is for some
417 // reason being used for pure arithmetic, and not based on some addressing
418 // computation. We don't have instructions that compute pointers with any
419 // addressing modes, so treat them as having no offset like flat
420 // instructions.
Tom Stellard70580f82015-07-20 14:28:41 +0000421 return isLegalFlatAddressingMode(AM);
422
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000423 default:
424 llvm_unreachable("unhandled address space");
425 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000426}
427
Matt Arsenaulte6986632015-01-14 01:35:22 +0000428bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000429 unsigned AddrSpace,
430 unsigned Align,
431 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000432 if (IsFast)
433 *IsFast = false;
434
Matt Arsenault1018c892014-04-24 17:08:26 +0000435 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
436 // which isn't a simple VT.
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000437 // Until MVT is extended to handle this, simply check for the size and
438 // rely on the condition below: allow accesses if the size is a multiple of 4.
439 if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 &&
440 VT.getStoreSize() > 16)) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000441 return false;
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000442 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000443
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000444 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
445 AddrSpace == AMDGPUAS::REGION_ADDRESS) {
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000446 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
447 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
448 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000449 bool AlignedBy4 = (Align % 4 == 0);
450 if (IsFast)
451 *IsFast = AlignedBy4;
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000452
Sanjay Patelce74db92015-09-03 15:03:19 +0000453 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000454 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000455
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000456 if (Subtarget->hasUnalignedBufferAccess()) {
457 // If we have an uniform constant load, it still requires using a slow
458 // buffer instruction if unaligned.
459 if (IsFast) {
460 *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) ?
461 (Align % 4 == 0) : true;
462 }
463
464 return true;
465 }
466
Tom Stellard33e64c62015-02-04 20:49:52 +0000467 // Smaller than dword value must be aligned.
Tom Stellard33e64c62015-02-04 20:49:52 +0000468 if (VT.bitsLT(MVT::i32))
469 return false;
470
Matt Arsenault1018c892014-04-24 17:08:26 +0000471 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
472 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000473 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000474 if (IsFast)
475 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000476
477 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000478}
479
Matt Arsenault46645fa2014-07-28 17:49:26 +0000480EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
481 unsigned SrcAlign, bool IsMemset,
482 bool ZeroMemset,
483 bool MemcpyStrSrc,
484 MachineFunction &MF) const {
485 // FIXME: Should account for address space here.
486
487 // The default fallback uses the private pointer size as a guess for a type to
488 // use. Make sure we switch these to 64-bit accesses.
489
490 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
491 return MVT::v4i32;
492
493 if (Size >= 8 && DstAlign >= 4)
494 return MVT::v2i32;
495
496 // Use the default.
497 return MVT::Other;
498}
499
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000500static bool isFlatGlobalAddrSpace(unsigned AS) {
501 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
502 AS == AMDGPUAS::FLAT_ADDRESS ||
503 AS == AMDGPUAS::CONSTANT_ADDRESS;
504}
505
506bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
507 unsigned DestAS) const {
Matt Arsenault37fefd62016-06-10 02:18:02 +0000508 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000509}
510
Tom Stellarda6f24c62015-12-15 20:55:55 +0000511bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
512 const MemSDNode *MemNode = cast<MemSDNode>(N);
513 const Value *Ptr = MemNode->getMemOperand()->getValue();
514
515 // UndefValue means this is a load of a kernel input. These are uniform.
Tom Stellard418beb72016-07-13 14:23:33 +0000516 // Sometimes LDS instructions have constant pointers.
517 // If Ptr is null, then that means this mem operand contains a
518 // PseudoSourceValue like GOT.
519 if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) ||
520 isa<Constant>(Ptr) || isa<GlobalValue>(Ptr))
Tom Stellarda6f24c62015-12-15 20:55:55 +0000521 return true;
522
Tom Stellard418beb72016-07-13 14:23:33 +0000523 const Instruction *I = dyn_cast<Instruction>(Ptr);
Tom Stellarda6f24c62015-12-15 20:55:55 +0000524 return I && I->getMetadata("amdgpu.uniform");
525}
526
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000527TargetLoweringBase::LegalizeTypeAction
528SITargetLowering::getPreferredVectorAction(EVT VT) const {
529 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
530 return TypeSplitVector;
531
532 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000533}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000534
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000535bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
536 Type *Ty) const {
Matt Arsenault749035b2016-07-30 01:40:36 +0000537 // FIXME: Could be smarter if called for vector constants.
538 return true;
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000539}
540
Tom Stellard2e045bb2016-01-20 00:13:22 +0000541bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
542
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000543 // i16 is not desirable unless it is a load or a store.
544 if (VT == MVT::i16 && Op != ISD::LOAD && Op != ISD::STORE)
545 return false;
546
Tom Stellard2e045bb2016-01-20 00:13:22 +0000547 // SimplifySetCC uses this function to determine whether or not it should
548 // create setcc with i1 operands. We don't have instructions for i1 setcc.
549 if (VT == MVT::i1 && Op == ISD::SETCC)
550 return false;
551
552 return TargetLowering::isTypeDesirableForOp(Op, VT);
553}
554
Jan Veselyfea814d2016-06-21 20:46:20 +0000555SDValue SITargetLowering::LowerParameterPtr(SelectionDAG &DAG,
556 const SDLoc &SL, SDValue Chain,
557 unsigned Offset) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000558 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000559 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000560 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaultac234b62015-11-30 21:15:57 +0000561 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000562
Matt Arsenault86033ca2014-07-28 17:31:39 +0000563 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000564 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000565 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
566 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
Jan Veselyfea814d2016-06-21 20:46:20 +0000567 return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
568 DAG.getConstant(Offset, SL, PtrVT));
569}
570SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
571 const SDLoc &SL, SDValue Chain,
572 unsigned Offset, bool Signed) const {
573 const DataLayout &DL = DAG.getDataLayout();
574 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
575 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
576 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Mehdi Amini44ede332015-07-09 02:09:04 +0000577 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000578 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
579
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000580 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000581
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000582 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000583 if (MemVT.isFloatingPoint())
584 ExtTy = ISD::EXTLOAD;
585
Jan Veselyfea814d2016-06-21 20:46:20 +0000586 SDValue Ptr = LowerParameterPtr(DAG, SL, Chain, Offset);
Justin Lebar9c375812016-07-15 18:27:10 +0000587 return DAG.getLoad(ISD::UNINDEXED, ExtTy, VT, SL, Chain, Ptr, PtrOffset,
Justin Lebaradbf09e2016-09-11 01:38:58 +0000588 PtrInfo, MemVT, Align,
589 MachineMemOperand::MONonTemporal |
590 MachineMemOperand::MODereferenceable |
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 Stellardf110f8f2016-04-14 16:27:03 +0000691 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
692 } else {
693 assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() &&
694 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() &&
695 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
696 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
697 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
698 !Info->hasWorkItemIDZ());
Tom Stellardaf775432013-10-23 00:44:32 +0000699 }
700
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000701 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
702 if (Info->hasPrivateSegmentBuffer()) {
703 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
704 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
705 CCInfo.AllocateReg(PrivateSegmentBufferReg);
706 }
707
708 if (Info->hasDispatchPtr()) {
709 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
710 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
711 CCInfo.AllocateReg(DispatchPtrReg);
712 }
713
Matt Arsenault48ab5262016-04-25 19:27:18 +0000714 if (Info->hasQueuePtr()) {
715 unsigned QueuePtrReg = Info->addQueuePtr(*TRI);
716 MF.addLiveIn(QueuePtrReg, &AMDGPU::SReg_64RegClass);
717 CCInfo.AllocateReg(QueuePtrReg);
718 }
719
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000720 if (Info->hasKernargSegmentPtr()) {
721 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
722 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
723 CCInfo.AllocateReg(InputPtrReg);
724 }
725
Matt Arsenault8d718dc2016-07-22 17:01:30 +0000726 if (Info->hasDispatchID()) {
727 unsigned DispatchIDReg = Info->addDispatchID(*TRI);
728 MF.addLiveIn(DispatchIDReg, &AMDGPU::SReg_64RegClass);
729 CCInfo.AllocateReg(DispatchIDReg);
730 }
731
Matt Arsenault296b8492016-02-12 06:31:30 +0000732 if (Info->hasFlatScratchInit()) {
733 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
734 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass);
735 CCInfo.AllocateReg(FlatScratchInitReg);
736 }
737
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000738 if (!AMDGPU::isShader(CallConv))
739 analyzeFormalArgumentsCompute(CCInfo, Ins);
740 else
741 AnalyzeFormalArguments(CCInfo, Splits);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000742
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;
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000758 EVT MemVT = VA.getLocVT();
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
Tom Stellard0b76fc4c2016-09-16 21:34:26 +0000878 if (ST.isAmdCodeObjectV2()) {
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000879 // TODO: Assume we will spill without optimizations.
880 if (HasStackObjects) {
881 // If we have stack objects, we unquestionably need the private buffer
Tom Stellard0b76fc4c2016-09-16 21:34:26 +0000882 // resource. For the Code Object V2 ABI, this will be the first 4 user
883 // SGPR inputs. We can reserve those and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000884
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.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001121static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop(
1122 const SIInstrInfo *TII,
1123 MachineRegisterInfo &MRI,
1124 MachineBasicBlock &OrigBB,
1125 MachineBasicBlock &LoopBB,
1126 const DebugLoc &DL,
1127 const MachineOperand &IdxReg,
1128 unsigned InitReg,
1129 unsigned ResultReg,
1130 unsigned PhiReg,
1131 unsigned InitSaveExecReg,
1132 int Offset) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001133 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
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001177 // Update EXEC, switch all done bits to 0 and all todo bits to 1.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001178 MachineInstr *InsertPt =
1179 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001180 .addReg(AMDGPU::EXEC)
1181 .addReg(NewExec);
1182
1183 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use
1184 // s_cbranch_scc0?
1185
1186 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover.
1187 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
1188 .addMBB(&LoopBB);
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001189
1190 return InsertPt->getIterator();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001191}
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.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001198static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII,
1199 MachineBasicBlock &MBB,
1200 MachineInstr &MI,
1201 unsigned InitResultReg,
1202 unsigned PhiReg,
1203 int Offset) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001204 MachineFunction *MF = MBB.getParent();
1205 MachineRegisterInfo &MRI = MF->getRegInfo();
1206 const DebugLoc &DL = MI.getDebugLoc();
1207 MachineBasicBlock::iterator I(&MI);
1208
1209 unsigned DstReg = MI.getOperand(0).getReg();
1210 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1211 unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1212
1213 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec);
1214
1215 // Save the EXEC mask
1216 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec)
1217 .addReg(AMDGPU::EXEC);
1218
1219 // To insert the loop we need to split the block. Move everything after this
1220 // point to a new block, and insert a new empty block between the two.
1221 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock();
1222 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
1223 MachineFunction::iterator MBBI(MBB);
1224 ++MBBI;
1225
1226 MF->insert(MBBI, LoopBB);
1227 MF->insert(MBBI, RemainderBB);
1228
1229 LoopBB->addSuccessor(LoopBB);
1230 LoopBB->addSuccessor(RemainderBB);
1231
1232 // Move the rest of the block into a new block.
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001233 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001234 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
1235
1236 MBB.addSuccessor(LoopBB);
1237
1238 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1239
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001240 auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx,
1241 InitResultReg, DstReg, PhiReg, TmpExec,
1242 Offset);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001243
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
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001250 return InsPt;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001251}
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,
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001301 const SISubtarget &ST) {
1302 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001303 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1304 MachineFunction *MF = MBB.getParent();
1305 MachineRegisterInfo &MRI = MF->getRegInfo();
1306
1307 unsigned Dst = MI.getOperand(0).getReg();
1308 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
1309 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1310
1311 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
1312
1313 unsigned SubReg;
1314 std::tie(SubReg, Offset)
1315 = computeIndirectRegAndOffset(TRI, VecRC, SrcVec->getReg(), Offset);
1316
1317 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset)) {
1318 MachineBasicBlock::iterator I(&MI);
1319 const DebugLoc &DL = MI.getDebugLoc();
1320
1321 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
1322 .addReg(SrcVec->getReg(), RegState::Undef, SubReg)
1323 .addReg(SrcVec->getReg(), RegState::Implicit);
1324 MI.eraseFromParent();
1325
1326 return &MBB;
1327 }
1328
1329 const DebugLoc &DL = MI.getDebugLoc();
1330 MachineBasicBlock::iterator I(&MI);
1331
1332 unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1333 unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1334
1335 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg);
1336
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001337
1338 auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset);
1339
1340 BuildMI(*InsPt->getParent(), InsPt, DL,
1341 TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001342 .addReg(SrcVec->getReg(), RegState::Undef, SubReg)
1343 .addReg(SrcVec->getReg(), RegState::Implicit);
1344
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001345 return InsPt->getParent();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001346}
1347
1348static MachineBasicBlock *emitIndirectDst(MachineInstr &MI,
1349 MachineBasicBlock &MBB,
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001350 const SISubtarget &ST) {
1351 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001352 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1353 MachineFunction *MF = MBB.getParent();
1354 MachineRegisterInfo &MRI = MF->getRegInfo();
1355
1356 unsigned Dst = MI.getOperand(0).getReg();
1357 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
1358 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1359 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val);
1360 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1361 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
1362
1363 // This can be an immediate, but will be folded later.
1364 assert(Val->getReg());
1365
1366 unsigned SubReg;
1367 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC,
1368 SrcVec->getReg(),
1369 Offset);
1370 if (Idx->getReg() == AMDGPU::NoRegister) {
1371 MachineBasicBlock::iterator I(&MI);
1372 const DebugLoc &DL = MI.getDebugLoc();
1373
1374 assert(Offset == 0);
1375
1376 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst)
1377 .addOperand(*SrcVec)
1378 .addOperand(*Val)
1379 .addImm(SubReg);
1380
1381 MI.eraseFromParent();
1382 return &MBB;
1383 }
1384
1385 const MCInstrDesc &MovRelDesc = TII->get(AMDGPU::V_MOVRELD_B32_e32);
1386 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset)) {
1387 MachineBasicBlock::iterator I(&MI);
1388 const DebugLoc &DL = MI.getDebugLoc();
1389
1390 MachineInstr *MovRel =
1391 BuildMI(MBB, I, DL, MovRelDesc)
1392 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst
1393 .addOperand(*Val)
1394 .addReg(Dst, RegState::ImplicitDefine)
1395 .addReg(SrcVec->getReg(), RegState::Implicit);
1396
1397 const int ImpDefIdx = MovRelDesc.getNumOperands() +
1398 MovRelDesc.getNumImplicitUses();
1399 const int ImpUseIdx = ImpDefIdx + 1;
1400
1401 MovRel->tieOperands(ImpDefIdx, ImpUseIdx);
1402 MI.eraseFromParent();
1403 return &MBB;
1404 }
1405
1406 if (Val->isReg())
1407 MRI.clearKillFlags(Val->getReg());
1408
1409 const DebugLoc &DL = MI.getDebugLoc();
1410 unsigned PhiReg = MRI.createVirtualRegister(VecRC);
1411
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001412 auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg, Offset);
1413
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001414 // vdst is not actually read and just provides the base register index.
1415 MachineInstr *MovRel =
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001416 BuildMI(*InsPt->getParent(), InsPt, DL, MovRelDesc)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001417 .addReg(PhiReg, RegState::Undef, SubReg) // vdst
1418 .addOperand(*Val)
1419 .addReg(Dst, RegState::ImplicitDefine)
1420 .addReg(PhiReg, RegState::Implicit);
1421
1422 const int ImpDefIdx = MovRelDesc.getNumOperands() +
1423 MovRelDesc.getNumImplicitUses();
1424 const int ImpUseIdx = ImpDefIdx + 1;
1425
1426 MovRel->tieOperands(ImpDefIdx, ImpUseIdx);
1427
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001428 return InsPt->getParent();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001429}
1430
Matt Arsenault786724a2016-07-12 21:41:32 +00001431MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
1432 MachineInstr &MI, MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001433 switch (MI.getOpcode()) {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001434 case AMDGPU::SI_INIT_M0: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001435 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001436 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001437 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001438 .addOperand(MI.getOperand(0));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001439 MI.eraseFromParent();
Matt Arsenault20711b72015-02-20 22:10:45 +00001440 return BB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001441 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001442 case AMDGPU::GET_GROUPSTATICSIZE: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001443 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1444
Changpeng Fang01f60622016-03-15 17:28:44 +00001445 MachineFunction *MF = BB->getParent();
1446 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001447 DebugLoc DL = MI.getDebugLoc();
Matt Arsenault3c07c812016-07-22 17:01:33 +00001448 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32))
1449 .addOperand(MI.getOperand(0))
Matt Arsenault52ef4012016-07-26 16:45:58 +00001450 .addImm(MFI->getLDSSize());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001451 MI.eraseFromParent();
Changpeng Fang01f60622016-03-15 17:28:44 +00001452 return BB;
1453 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001454 case AMDGPU::SI_INDIRECT_SRC_V1:
1455 case AMDGPU::SI_INDIRECT_SRC_V2:
1456 case AMDGPU::SI_INDIRECT_SRC_V4:
1457 case AMDGPU::SI_INDIRECT_SRC_V8:
1458 case AMDGPU::SI_INDIRECT_SRC_V16:
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001459 return emitIndirectSrc(MI, *BB, *getSubtarget());
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001460 case AMDGPU::SI_INDIRECT_DST_V1:
1461 case AMDGPU::SI_INDIRECT_DST_V2:
1462 case AMDGPU::SI_INDIRECT_DST_V4:
1463 case AMDGPU::SI_INDIRECT_DST_V8:
1464 case AMDGPU::SI_INDIRECT_DST_V16:
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001465 return emitIndirectDst(MI, *BB, *getSubtarget());
Matt Arsenault786724a2016-07-12 21:41:32 +00001466 case AMDGPU::SI_KILL:
1467 return splitKillBlock(MI, BB);
Matt Arsenault22e41792016-08-27 01:00:37 +00001468 case AMDGPU::V_CNDMASK_B64_PSEUDO: {
1469 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
1470 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1471
1472 unsigned Dst = MI.getOperand(0).getReg();
1473 unsigned Src0 = MI.getOperand(1).getReg();
1474 unsigned Src1 = MI.getOperand(2).getReg();
1475 const DebugLoc &DL = MI.getDebugLoc();
1476 unsigned SrcCond = MI.getOperand(3).getReg();
1477
1478 unsigned DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1479 unsigned DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1480
1481 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo)
1482 .addReg(Src0, 0, AMDGPU::sub0)
1483 .addReg(Src1, 0, AMDGPU::sub0)
1484 .addReg(SrcCond);
1485 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi)
1486 .addReg(Src0, 0, AMDGPU::sub1)
1487 .addReg(Src1, 0, AMDGPU::sub1)
1488 .addReg(SrcCond);
1489
1490 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst)
1491 .addReg(DstLo)
1492 .addImm(AMDGPU::sub0)
1493 .addReg(DstHi)
1494 .addImm(AMDGPU::sub1);
1495 MI.eraseFromParent();
1496 return BB;
1497 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001498 default:
1499 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001500 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001501}
1502
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001503bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1504 // This currently forces unfolding various combinations of fsub into fma with
1505 // free fneg'd operands. As long as we have fast FMA (controlled by
1506 // isFMAFasterThanFMulAndFAdd), we should perform these.
1507
1508 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1509 // most of these combines appear to be cycle neutral but save on instruction
1510 // count / code size.
1511 return true;
1512}
1513
Mehdi Amini44ede332015-07-09 02:09:04 +00001514EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1515 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001516 if (!VT.isVector()) {
1517 return MVT::i1;
1518 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001519 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001520}
1521
Mehdi Aminieaabc512015-07-09 15:12:23 +00001522MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001523 return MVT::i32;
1524}
1525
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001526// Answering this is somewhat tricky and depends on the specific device which
1527// have different rates for fma or all f64 operations.
1528//
1529// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1530// regardless of which device (although the number of cycles differs between
1531// devices), so it is always profitable for f64.
1532//
1533// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1534// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1535// which we can always do even without fused FP ops since it returns the same
1536// result as the separate operations and since it is always full
1537// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1538// however does not support denormals, so we do report fma as faster if we have
1539// a fast fma device and require denormals.
1540//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001541bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1542 VT = VT.getScalarType();
1543
1544 if (!VT.isSimple())
1545 return false;
1546
1547 switch (VT.getSimpleVT().SimpleTy) {
1548 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001549 // This is as fast on some subtargets. However, we always have full rate f32
1550 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001551 // which we should prefer over fma. We can't use this if we want to support
1552 // denormals, so only report this in these cases.
1553 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001554 case MVT::f64:
1555 return true;
1556 default:
1557 break;
1558 }
1559
1560 return false;
1561}
1562
Tom Stellard75aadc22012-12-11 21:25:42 +00001563//===----------------------------------------------------------------------===//
1564// Custom DAG Lowering Operations
1565//===----------------------------------------------------------------------===//
1566
1567SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1568 switch (Op.getOpcode()) {
1569 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001570 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001571 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001572 SDValue Result = LowerLOAD(Op, DAG);
1573 assert((!Result.getNode() ||
1574 Result.getNode()->getNumValues() == 2) &&
1575 "Load should return a value and a chain");
1576 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001577 }
Tom Stellardaf775432013-10-23 00:44:32 +00001578
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001579 case ISD::FSIN:
1580 case ISD::FCOS:
1581 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001582 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001583 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001584 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001585 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001586 case ISD::GlobalAddress: {
1587 MachineFunction &MF = DAG.getMachineFunction();
1588 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1589 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001590 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001591 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001592 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001593 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Matt Arsenault99c14522016-04-25 19:27:24 +00001594 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG);
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001595 case ISD::TRAP: return lowerTRAP(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001596 }
1597 return SDValue();
1598}
1599
Tom Stellardf8794352012-12-19 22:10:31 +00001600/// \brief Helper function for LowerBRCOND
1601static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001602
Tom Stellardf8794352012-12-19 22:10:31 +00001603 SDNode *Parent = Value.getNode();
1604 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1605 I != E; ++I) {
1606
1607 if (I.getUse().get() != Value)
1608 continue;
1609
1610 if (I->getOpcode() == Opcode)
1611 return *I;
1612 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001613 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001614}
1615
Tom Stellardbc4497b2016-02-12 23:45:29 +00001616bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault6408c912016-09-16 22:11:18 +00001617 if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
1618 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1619 case AMDGPUIntrinsic::amdgcn_if:
1620 case AMDGPUIntrinsic::amdgcn_else:
1621 case AMDGPUIntrinsic::amdgcn_end_cf:
1622 case AMDGPUIntrinsic::amdgcn_loop:
1623 return true;
1624 default:
1625 return false;
1626 }
Tom Stellardbc4497b2016-02-12 23:45:29 +00001627 }
Matt Arsenault6408c912016-09-16 22:11:18 +00001628
1629 if (Intr->getOpcode() == ISD::INTRINSIC_WO_CHAIN) {
1630 switch (cast<ConstantSDNode>(Intr->getOperand(0))->getZExtValue()) {
1631 case AMDGPUIntrinsic::amdgcn_break:
1632 case AMDGPUIntrinsic::amdgcn_if_break:
1633 case AMDGPUIntrinsic::amdgcn_else_break:
1634 return true;
1635 default:
1636 return false;
1637 }
1638 }
1639
1640 return false;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001641}
1642
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001643void SITargetLowering::createDebuggerPrologueStackObjects(
1644 MachineFunction &MF) const {
1645 // Create stack objects that are used for emitting debugger prologue.
1646 //
1647 // Debugger prologue writes work group IDs and work item IDs to scratch memory
1648 // at fixed location in the following format:
1649 // offset 0: work group ID x
1650 // offset 4: work group ID y
1651 // offset 8: work group ID z
1652 // offset 16: work item ID x
1653 // offset 20: work item ID y
1654 // offset 24: work item ID z
1655 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1656 int ObjectIdx = 0;
1657
1658 // For each dimension:
1659 for (unsigned i = 0; i < 3; ++i) {
1660 // Create fixed stack object for work group ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001661 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001662 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx);
1663 // Create fixed stack object for work item ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001664 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001665 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx);
1666 }
1667}
1668
Tom Stellardf8794352012-12-19 22:10:31 +00001669/// This transforms the control flow intrinsics to get the branch destination as
1670/// last parameter, also switches branch target with BR if the need arise
1671SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1672 SelectionDAG &DAG) const {
1673
Andrew Trickef9de2a2013-05-25 02:42:55 +00001674 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001675
1676 SDNode *Intr = BRCOND.getOperand(1).getNode();
1677 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001678 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001679 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001680
1681 if (Intr->getOpcode() == ISD::SETCC) {
1682 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001683 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001684 Intr = SetCC->getOperand(0).getNode();
1685
1686 } else {
1687 // Get the target from BR if we don't negate the condition
1688 BR = findUser(BRCOND, ISD::BR);
1689 Target = BR->getOperand(1);
1690 }
1691
Matt Arsenault6408c912016-09-16 22:11:18 +00001692 // FIXME: This changes the types of the intrinsics instead of introducing new
1693 // nodes with the correct types.
1694 // e.g. llvm.amdgcn.loop
1695
1696 // eg: i1,ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3
1697 // => t9: ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3, BasicBlock:ch<bb1 0x7fee5286d088>
1698
Nicolai Haehnleffbd56a2016-05-05 17:36:36 +00001699 if (!isCFIntrinsic(Intr)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00001700 // This is a uniform branch so we don't need to legalize.
1701 return BRCOND;
1702 }
1703
Matt Arsenault6408c912016-09-16 22:11:18 +00001704 bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID ||
1705 Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN;
1706
Tom Stellardbc4497b2016-02-12 23:45:29 +00001707 assert(!SetCC ||
1708 (SetCC->getConstantOperandVal(1) == 1 &&
Tom Stellardbc4497b2016-02-12 23:45:29 +00001709 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1710 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001711
Tom Stellardf8794352012-12-19 22:10:31 +00001712 // operands of the new intrinsic call
1713 SmallVector<SDValue, 4> Ops;
Matt Arsenault6408c912016-09-16 22:11:18 +00001714 if (HaveChain)
1715 Ops.push_back(BRCOND.getOperand(0));
1716
1717 Ops.append(Intr->op_begin() + (HaveChain ? 1 : 0), Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001718 Ops.push_back(Target);
1719
Matt Arsenault6408c912016-09-16 22:11:18 +00001720 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
1721
Tom Stellardf8794352012-12-19 22:10:31 +00001722 // build the new intrinsic call
1723 SDNode *Result = DAG.getNode(
1724 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001725 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001726
Matt Arsenault6408c912016-09-16 22:11:18 +00001727 if (!HaveChain) {
1728 SDValue Ops[] = {
1729 SDValue(Result, 0),
1730 BRCOND.getOperand(0)
1731 };
1732
1733 Result = DAG.getMergeValues(Ops, DL).getNode();
1734 }
1735
Tom Stellardf8794352012-12-19 22:10:31 +00001736 if (BR) {
1737 // Give the branch instruction our target
1738 SDValue Ops[] = {
1739 BR->getOperand(0),
1740 BRCOND.getOperand(2)
1741 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001742 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1743 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1744 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001745 }
1746
1747 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1748
1749 // Copy the intrinsic results to registers
1750 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1751 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1752 if (!CopyToReg)
1753 continue;
1754
1755 Chain = DAG.getCopyToReg(
1756 Chain, DL,
1757 CopyToReg->getOperand(1),
1758 SDValue(Result, i - 1),
1759 SDValue());
1760
1761 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1762 }
1763
1764 // Remove the old intrinsic from the chain
1765 DAG.ReplaceAllUsesOfValueWith(
1766 SDValue(Intr, Intr->getNumValues() - 1),
1767 Intr->getOperand(0));
1768
1769 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001770}
1771
Matt Arsenault99c14522016-04-25 19:27:24 +00001772SDValue SITargetLowering::getSegmentAperture(unsigned AS,
1773 SelectionDAG &DAG) const {
1774 SDLoc SL;
1775 MachineFunction &MF = DAG.getMachineFunction();
1776 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00001777 unsigned UserSGPR = Info->getQueuePtrUserSGPR();
1778 assert(UserSGPR != AMDGPU::NoRegister);
1779
Matt Arsenault99c14522016-04-25 19:27:24 +00001780 SDValue QueuePtr = CreateLiveInRegister(
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00001781 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
Matt Arsenault99c14522016-04-25 19:27:24 +00001782
1783 // Offset into amd_queue_t for group_segment_aperture_base_hi /
1784 // private_segment_aperture_base_hi.
1785 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
1786
1787 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr,
1788 DAG.getConstant(StructOffset, SL, MVT::i64));
1789
1790 // TODO: Use custom target PseudoSourceValue.
1791 // TODO: We should use the value from the IR intrinsic call, but it might not
1792 // be available and how do we get it?
1793 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()),
1794 AMDGPUAS::CONSTANT_ADDRESS));
1795
1796 MachinePointerInfo PtrInfo(V, StructOffset);
Justin Lebar9c375812016-07-15 18:27:10 +00001797 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo,
1798 MinAlign(64, StructOffset),
Justin Lebaradbf09e2016-09-11 01:38:58 +00001799 MachineMemOperand::MODereferenceable |
1800 MachineMemOperand::MOInvariant);
Matt Arsenault99c14522016-04-25 19:27:24 +00001801}
1802
1803SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
1804 SelectionDAG &DAG) const {
1805 SDLoc SL(Op);
1806 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op);
1807
1808 SDValue Src = ASC->getOperand(0);
1809
1810 // FIXME: Really support non-0 null pointers.
1811 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32);
1812 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
1813
1814 // flat -> local/private
1815 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1816 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1817 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1818 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
1819 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
1820
1821 return DAG.getNode(ISD::SELECT, SL, MVT::i32,
1822 NonNull, Ptr, SegmentNullPtr);
1823 }
1824 }
1825
1826 // local/private -> flat
1827 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1828 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1829 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1830 SDValue NonNull
1831 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
1832
1833 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG);
1834 SDValue CvtPtr
1835 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
1836
1837 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull,
1838 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr),
1839 FlatNullPtr);
1840 }
1841 }
1842
1843 // global <-> flat are no-ops and never emitted.
1844
1845 const MachineFunction &MF = DAG.getMachineFunction();
1846 DiagnosticInfoUnsupported InvalidAddrSpaceCast(
1847 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc());
1848 DAG.getContext()->diagnose(InvalidAddrSpaceCast);
1849
1850 return DAG.getUNDEF(ASC->getValueType(0));
1851}
1852
Tom Stellard418beb72016-07-13 14:23:33 +00001853static bool shouldEmitGOTReloc(const GlobalValue *GV,
1854 const TargetMachine &TM) {
1855 return GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
1856 !TM.shouldAssumeDSOLocal(*GV->getParent(), GV);
Tom Stellardb164a982016-06-25 01:59:16 +00001857}
1858
Tom Stellard418beb72016-07-13 14:23:33 +00001859bool
1860SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1861 // We can fold offsets for anything that doesn't require a GOT relocation.
1862 return GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
1863 !shouldEmitGOTReloc(GA->getGlobal(), getTargetMachine());
1864}
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001865
Tom Stellard418beb72016-07-13 14:23:33 +00001866static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV,
1867 SDLoc DL, unsigned Offset, EVT PtrVT,
1868 unsigned GAFlags = SIInstrInfo::MO_NONE) {
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001869 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
1870 // lowered to the following code sequence:
1871 // s_getpc_b64 s[0:1]
1872 // s_add_u32 s0, s0, $symbol
1873 // s_addc_u32 s1, s1, 0
1874 //
1875 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
1876 // a fixup or relocation is emitted to replace $symbol with a literal
1877 // constant, which is a pc-relative offset from the encoding of the $symbol
1878 // operand to the global variable.
1879 //
1880 // What we want here is an offset from the value returned by s_getpc
1881 // (which is the address of the s_add_u32 instruction) to the global
1882 // variable, but since the encoding of $symbol starts 4 bytes after the start
1883 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too
1884 // small. This requires us to add 4 to the global variable offset in order to
1885 // compute the correct address.
Tom Stellard418beb72016-07-13 14:23:33 +00001886 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
1887 GAFlags);
Tom Stellardbf3e6e52016-06-14 20:29:59 +00001888 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, GA);
1889}
1890
Tom Stellard418beb72016-07-13 14:23:33 +00001891SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1892 SDValue Op,
1893 SelectionDAG &DAG) const {
1894 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1895
1896 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS &&
1897 GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS)
1898 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1899
1900 SDLoc DL(GSD);
1901 const GlobalValue *GV = GSD->getGlobal();
1902 EVT PtrVT = Op.getValueType();
1903
1904 if (!shouldEmitGOTReloc(GV, getTargetMachine()))
1905 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
1906
1907 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
1908 SIInstrInfo::MO_GOTPCREL);
1909
1910 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext());
1911 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
1912 const DataLayout &DataLayout = DAG.getDataLayout();
1913 unsigned Align = DataLayout.getABITypeAlignment(PtrTy);
1914 // FIXME: Use a PseudoSourceValue once those can be assigned an address space.
1915 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
1916
Justin Lebar9c375812016-07-15 18:27:10 +00001917 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align,
Justin Lebaradbf09e2016-09-11 01:38:58 +00001918 MachineMemOperand::MODereferenceable |
1919 MachineMemOperand::MOInvariant);
Tom Stellard418beb72016-07-13 14:23:33 +00001920}
1921
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001922SDValue SITargetLowering::lowerTRAP(SDValue Op,
1923 SelectionDAG &DAG) const {
1924 const MachineFunction &MF = DAG.getMachineFunction();
1925 DiagnosticInfoUnsupported NoTrap(*MF.getFunction(),
1926 "trap handler not supported",
1927 Op.getDebugLoc(),
1928 DS_Warning);
1929 DAG.getContext()->diagnose(NoTrap);
1930
1931 // Emit s_endpgm.
1932
1933 // FIXME: This should really be selected to s_trap, but that requires
1934 // setting up the trap handler for it o do anything.
Matt Arsenault9babdf42016-06-22 20:15:28 +00001935 return DAG.getNode(AMDGPUISD::ENDPGM, SDLoc(Op), MVT::Other,
1936 Op.getOperand(0));
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001937}
1938
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001939SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain,
1940 const SDLoc &DL, SDValue V) const {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001941 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
1942 // the destination register.
1943 //
Tom Stellardfc92e772015-05-12 14:18:14 +00001944 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1945 // so we will end up with redundant moves to m0.
1946 //
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001947 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
1948
1949 // A Null SDValue creates a glue result.
1950 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
1951 V, Chain);
1952 return SDValue(M0, 0);
Tom Stellardfc92e772015-05-12 14:18:14 +00001953}
1954
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001955SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1956 SDValue Op,
1957 MVT VT,
1958 unsigned Offset) const {
1959 SDLoc SL(Op);
1960 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1961 DAG.getEntryNode(), Offset, false);
1962 // The local size values will have the hi 16-bits as zero.
1963 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1964 DAG.getValueType(VT));
1965}
1966
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001967static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
Matt Arsenaulte0132462016-01-30 05:19:45 +00001968 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00001969 "non-hsa intrinsic with hsa target",
1970 DL.getDebugLoc());
1971 DAG.getContext()->diagnose(BadIntrin);
1972 return DAG.getUNDEF(VT);
1973}
1974
1975static SDValue emitRemovedIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
1976 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
1977 "intrinsic not supported on subtarget",
1978 DL.getDebugLoc());
Matt Arsenaulte0132462016-01-30 05:19:45 +00001979 DAG.getContext()->diagnose(BadIntrin);
1980 return DAG.getUNDEF(VT);
1981}
1982
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001983SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1984 SelectionDAG &DAG) const {
1985 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001986 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001987 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001988
1989 EVT VT = Op.getValueType();
1990 SDLoc DL(Op);
1991 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1992
Sanjay Patela2607012015-09-16 16:31:21 +00001993 // TODO: Should this propagate fast-math-flags?
1994
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001995 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001996 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault48ab5262016-04-25 19:27:18 +00001997 case Intrinsic::amdgcn_queue_ptr: {
Tom Stellard0b76fc4c2016-09-16 21:34:26 +00001998 if (!Subtarget->isAmdCodeObjectV2()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00001999 DiagnosticInfoUnsupported BadIntrin(
2000 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
2001 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00002002 DAG.getContext()->diagnose(BadIntrin);
2003 return DAG.getUNDEF(VT);
2004 }
2005
Matt Arsenault48ab5262016-04-25 19:27:18 +00002006 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
2007 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00002008 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
Matt Arsenault48ab5262016-04-25 19:27:18 +00002009 TRI->getPreloadedValue(MF, Reg), VT);
2010 }
Jan Veselyfea814d2016-06-21 20:46:20 +00002011 case Intrinsic::amdgcn_implicitarg_ptr: {
2012 unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
2013 return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset);
2014 }
Matt Arsenaultdc4ebad2016-04-29 21:16:52 +00002015 case Intrinsic::amdgcn_kernarg_segment_ptr: {
2016 unsigned Reg
2017 = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
2018 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2019 }
Matt Arsenault8d718dc2016-07-22 17:01:30 +00002020 case Intrinsic::amdgcn_dispatch_id: {
2021 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_ID);
2022 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2023 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002024 case Intrinsic::amdgcn_rcp:
2025 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
2026 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00002027 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002028 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002029 case Intrinsic::amdgcn_rsq_legacy: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002030 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002031 return emitRemovedIntrinsicError(DAG, DL, VT);
2032
2033 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
2034 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002035 case Intrinsic::amdgcn_rcp_legacy: {
2036 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
2037 return emitRemovedIntrinsicError(DAG, DL, VT);
2038 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1));
2039 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +00002040 case Intrinsic::amdgcn_rsq_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002041 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00002042 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00002043
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002044 Type *Type = VT.getTypeForEVT(*DAG.getContext());
2045 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
2046 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
2047
2048 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
2049 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
2050 DAG.getConstantFP(Max, DL, VT));
2051 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
2052 DAG.getConstantFP(Min, DL, VT));
2053 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002054 case Intrinsic::r600_read_ngroups_x:
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::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002060 case Intrinsic::r600_read_ngroups_y:
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
Tom Stellardec2e43c2014-09-22 15:35:29 +00002064 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2065 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002066 case Intrinsic::r600_read_ngroups_z:
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
Tom Stellardec2e43c2014-09-22 15:35:29 +00002070 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2071 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002072 case Intrinsic::r600_read_global_size_x:
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
Tom Stellardec2e43c2014-09-22 15:35:29 +00002076 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2077 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002078 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002079 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002080 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002081
Tom Stellardec2e43c2014-09-22 15:35:29 +00002082 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2083 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002084 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002085 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002086 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002087
Tom Stellardec2e43c2014-09-22 15:35:29 +00002088 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2089 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002090 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002091 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002092 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002093
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002094 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2095 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002096 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002097 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002098 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002099
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002100 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2101 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002102 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002103 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002104 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002105
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002106 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2107 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenault43976df2016-01-30 04:25:19 +00002108 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002109 case Intrinsic::r600_read_tgid_x:
2110 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002111 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002112 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002113 case Intrinsic::r600_read_tgid_y:
2114 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002115 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002116 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002117 case Intrinsic::r600_read_tgid_z:
2118 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002119 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002120 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002121 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002122 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002123 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002124 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002125 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002126 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002127 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002128 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002129 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002130 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002131 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002132 case AMDGPUIntrinsic::SI_load_const: {
2133 SDValue Ops[] = {
2134 Op.getOperand(1),
2135 Op.getOperand(2)
2136 };
2137
2138 MachineMemOperand *MMO = MF.getMachineMemOperand(
Justin Lebaradbf09e2016-09-11 01:38:58 +00002139 MachinePointerInfo(),
2140 MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable |
2141 MachineMemOperand::MOInvariant,
2142 VT.getStoreSize(), 4);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002143 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
2144 Op->getVTList(), Ops, VT, MMO);
2145 }
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002146 case AMDGPUIntrinsic::amdgcn_fdiv_fast: {
2147 return lowerFDIV_FAST(Op, DAG);
2148 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002149 case AMDGPUIntrinsic::SI_vs_load_input:
2150 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
2151 Op.getOperand(1),
2152 Op.getOperand(2),
2153 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00002154
Tom Stellard2a9d9472015-05-12 15:00:46 +00002155 case AMDGPUIntrinsic::SI_fs_constant: {
2156 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2157 SDValue Glue = M0.getValue(1);
2158 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
2159 DAG.getConstant(2, DL, MVT::i32), // P0
2160 Op.getOperand(1), Op.getOperand(2), Glue);
2161 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00002162 case AMDGPUIntrinsic::SI_packf16:
2163 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
2164 return DAG.getUNDEF(MVT::i32);
2165 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00002166 case AMDGPUIntrinsic::SI_fs_interp: {
2167 SDValue IJ = Op.getOperand(4);
2168 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2169 DAG.getConstant(0, DL, MVT::i32));
2170 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2171 DAG.getConstant(1, DL, MVT::i32));
2172 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2173 SDValue Glue = M0.getValue(1);
2174 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
2175 DAG.getVTList(MVT::f32, MVT::Glue),
2176 I, Op.getOperand(1), Op.getOperand(2), Glue);
2177 Glue = SDValue(P1.getNode(), 1);
2178 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
2179 Op.getOperand(1), Op.getOperand(2), Glue);
2180 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00002181 case Intrinsic::amdgcn_interp_p1: {
2182 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
2183 SDValue Glue = M0.getValue(1);
2184 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
2185 Op.getOperand(2), Op.getOperand(3), Glue);
2186 }
2187 case Intrinsic::amdgcn_interp_p2: {
2188 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
2189 SDValue Glue = SDValue(M0.getNode(), 1);
2190 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
2191 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
2192 Glue);
2193 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002194 case Intrinsic::amdgcn_sin:
2195 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
2196
2197 case Intrinsic::amdgcn_cos:
2198 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
2199
2200 case Intrinsic::amdgcn_log_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002201 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002202 return SDValue();
2203
2204 DiagnosticInfoUnsupported BadIntrin(
2205 *MF.getFunction(), "intrinsic not supported on subtarget",
2206 DL.getDebugLoc());
2207 DAG.getContext()->diagnose(BadIntrin);
2208 return DAG.getUNDEF(VT);
2209 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002210 case Intrinsic::amdgcn_ldexp:
2211 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
2212 Op.getOperand(1), Op.getOperand(2));
Matt Arsenault74015162016-05-28 00:19:52 +00002213
2214 case Intrinsic::amdgcn_fract:
2215 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
2216
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002217 case Intrinsic::amdgcn_class:
2218 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
2219 Op.getOperand(1), Op.getOperand(2));
2220 case Intrinsic::amdgcn_div_fmas:
2221 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
2222 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
2223 Op.getOperand(4));
2224
2225 case Intrinsic::amdgcn_div_fixup:
2226 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
2227 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
2228
2229 case Intrinsic::amdgcn_trig_preop:
2230 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
2231 Op.getOperand(1), Op.getOperand(2));
2232 case Intrinsic::amdgcn_div_scale: {
2233 // 3rd parameter required to be a constant.
2234 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2235 if (!Param)
2236 return DAG.getUNDEF(VT);
2237
2238 // Translate to the operands expected by the machine instruction. The
2239 // first parameter must be the same as the first instruction.
2240 SDValue Numerator = Op.getOperand(1);
2241 SDValue Denominator = Op.getOperand(2);
2242
2243 // Note this order is opposite of the machine instruction's operations,
2244 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
2245 // intrinsic has the numerator as the first operand to match a normal
2246 // division operation.
2247
2248 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
2249
2250 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
2251 Denominator, Numerator);
2252 }
Wei Ding07e03712016-07-28 16:42:13 +00002253 case Intrinsic::amdgcn_icmp: {
2254 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2255 int CondCode = CD->getSExtValue();
2256
2257 if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002258 CondCode >= ICmpInst::Predicate::BAD_ICMP_PREDICATE)
Wei Ding07e03712016-07-28 16:42:13 +00002259 return DAG.getUNDEF(VT);
2260
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002261 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002262 ISD::CondCode CCOpcode = getICmpCondCode(IcInput);
2263 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2264 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2265 }
2266 case Intrinsic::amdgcn_fcmp: {
2267 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2268 int CondCode = CD->getSExtValue();
2269
2270 if (CondCode <= FCmpInst::Predicate::FCMP_FALSE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002271 CondCode >= FCmpInst::Predicate::FCMP_TRUE)
Wei Ding07e03712016-07-28 16:42:13 +00002272 return DAG.getUNDEF(VT);
2273
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002274 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002275 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput);
2276 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2277 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2278 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002279 case Intrinsic::amdgcn_fmul_legacy:
2280 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT,
2281 Op.getOperand(1), Op.getOperand(2));
Matt Arsenaultc96e1de2016-07-18 18:35:05 +00002282 case Intrinsic::amdgcn_sffbh:
2283 case AMDGPUIntrinsic::AMDGPU_flbit_i32: // Legacy name.
2284 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002285 default:
2286 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
2287 }
2288}
2289
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002290SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
2291 SelectionDAG &DAG) const {
2292 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2293 switch (IntrID) {
2294 case Intrinsic::amdgcn_atomic_inc:
2295 case Intrinsic::amdgcn_atomic_dec: {
2296 MemSDNode *M = cast<MemSDNode>(Op);
2297 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
2298 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
2299 SDValue Ops[] = {
2300 M->getOperand(0), // Chain
2301 M->getOperand(2), // Ptr
2302 M->getOperand(3) // Value
2303 };
2304
2305 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
2306 M->getMemoryVT(), M->getMemOperand());
2307 }
2308 default:
2309 return SDValue();
2310 }
2311}
2312
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002313SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
2314 SelectionDAG &DAG) const {
2315 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00002316 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002317 SDValue Chain = Op.getOperand(0);
2318 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2319
2320 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00002321 case AMDGPUIntrinsic::SI_sendmsg: {
2322 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
2323 SDValue Glue = Chain.getValue(1);
2324 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
2325 Op.getOperand(2), Glue);
2326 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002327 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002328 SDValue Ops[] = {
2329 Chain,
2330 Op.getOperand(2),
2331 Op.getOperand(3),
2332 Op.getOperand(4),
2333 Op.getOperand(5),
2334 Op.getOperand(6),
2335 Op.getOperand(7),
2336 Op.getOperand(8),
2337 Op.getOperand(9),
2338 Op.getOperand(10),
2339 Op.getOperand(11),
2340 Op.getOperand(12),
2341 Op.getOperand(13),
2342 Op.getOperand(14)
2343 };
2344
2345 EVT VT = Op.getOperand(3).getValueType();
2346
2347 MachineMemOperand *MMO = MF.getMachineMemOperand(
2348 MachinePointerInfo(),
2349 MachineMemOperand::MOStore,
2350 VT.getStoreSize(), 4);
2351 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
2352 Op->getVTList(), Ops, VT, MMO);
2353 }
Matt Arsenault00568682016-07-13 06:04:22 +00002354 case AMDGPUIntrinsic::AMDGPU_kill: {
Matt Arsenault03006fd2016-07-19 16:27:56 +00002355 SDValue Src = Op.getOperand(2);
2356 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) {
Matt Arsenault00568682016-07-13 06:04:22 +00002357 if (!K->isNegative())
2358 return Chain;
Matt Arsenault03006fd2016-07-19 16:27:56 +00002359
2360 SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32);
2361 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne);
Matt Arsenault00568682016-07-13 06:04:22 +00002362 }
2363
Matt Arsenault03006fd2016-07-19 16:27:56 +00002364 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src);
2365 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast);
Matt Arsenault00568682016-07-13 06:04:22 +00002366 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002367 default:
2368 return SDValue();
2369 }
2370}
2371
Tom Stellard81d871d2013-11-13 23:36:50 +00002372SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2373 SDLoc DL(Op);
2374 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00002375 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00002376 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00002377
Matt Arsenaulta1436412016-02-10 18:21:45 +00002378 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
2379 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected");
Matt Arsenault6dfda962016-02-10 18:21:39 +00002380 // FIXME: Copied from PPC
2381 // First, load into 32 bits, then truncate to 1 bit.
2382
2383 SDValue Chain = Load->getChain();
2384 SDValue BasePtr = Load->getBasePtr();
2385 MachineMemOperand *MMO = Load->getMemOperand();
2386
2387 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
2388 BasePtr, MVT::i8, MMO);
2389
2390 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002391 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00002392 NewLD.getValue(1)
2393 };
2394
2395 return DAG.getMergeValues(Ops, DL);
2396 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002397
Matt Arsenaulta1436412016-02-10 18:21:45 +00002398 if (!MemVT.isVector())
2399 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002400
Matt Arsenaulta1436412016-02-10 18:21:45 +00002401 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
2402 "Custom lowering for non-i32 vectors hasn't been implemented.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002403
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002404 unsigned AS = Load->getAddressSpace();
2405 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
2406 AS, Load->getAlignment())) {
2407 SDValue Ops[2];
2408 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
2409 return DAG.getMergeValues(Ops, DL);
2410 }
2411
2412 unsigned NumElements = MemVT.getVectorNumElements();
2413 switch (AS) {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002414 case AMDGPUAS::CONSTANT_ADDRESS:
2415 if (isMemOpUniform(Load))
2416 return SDValue();
2417 // Non-uniform loads will be selected to MUBUF instructions, so they
2418 // have the same legalization requires ments as global and private
2419 // loads.
2420 //
Justin Bognerb03fd122016-08-17 05:10:15 +00002421 LLVM_FALLTHROUGH;
Matt Arsenaulta1436412016-02-10 18:21:45 +00002422 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002423 case AMDGPUAS::FLAT_ADDRESS:
2424 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00002425 return SplitVectorLoad(Op, DAG);
2426 // v4 loads are supported for private and global memory.
2427 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002428 case AMDGPUAS::PRIVATE_ADDRESS: {
2429 // Depending on the setting of the private_element_size field in the
2430 // resource descriptor, we can only make private accesses up to a certain
2431 // size.
2432 switch (Subtarget->getMaxPrivateElementSize()) {
2433 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002434 return scalarizeVectorLoad(Load, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002435 case 8:
2436 if (NumElements > 2)
2437 return SplitVectorLoad(Op, DAG);
2438 return SDValue();
2439 case 16:
2440 // Same as global/flat
2441 if (NumElements > 4)
2442 return SplitVectorLoad(Op, DAG);
2443 return SDValue();
2444 default:
2445 llvm_unreachable("unsupported private_element_size");
2446 }
2447 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002448 case AMDGPUAS::LOCAL_ADDRESS: {
2449 if (NumElements > 2)
2450 return SplitVectorLoad(Op, DAG);
2451
2452 if (NumElements == 2)
2453 return SDValue();
2454
Matt Arsenaulta1436412016-02-10 18:21:45 +00002455 // If properly aligned, if we split we might be able to use ds_read_b64.
2456 return SplitVectorLoad(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002457 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00002458 default:
2459 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00002460 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002461}
2462
Tom Stellard0ec134f2014-02-04 17:18:40 +00002463SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2464 if (Op.getValueType() != MVT::i64)
2465 return SDValue();
2466
2467 SDLoc DL(Op);
2468 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002469
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002470 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2471 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002472
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002473 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
2474 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
2475
2476 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
2477 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002478
2479 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
2480
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002481 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
2482 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002483
2484 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
2485
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002486 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002487 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002488}
2489
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002490// Catch division cases where we can use shortcuts with rcp and rsq
2491// instructions.
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002492SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op,
2493 SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002494 SDLoc SL(Op);
2495 SDValue LHS = Op.getOperand(0);
2496 SDValue RHS = Op.getOperand(1);
2497 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002498 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002499
2500 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault979902b2016-08-02 22:25:04 +00002501 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals()))) {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002502
Matt Arsenault979902b2016-08-02 22:25:04 +00002503 if (CLHS->isExactlyValue(1.0)) {
2504 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
2505 // the CI documentation has a worst case error of 1 ulp.
2506 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
2507 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002508
Matt Arsenault979902b2016-08-02 22:25:04 +00002509 // 1.0 / sqrt(x) -> rsq(x)
2510 //
2511 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
2512 // error seems really high at 2^29 ULP.
2513 if (RHS.getOpcode() == ISD::FSQRT)
2514 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
2515
2516 // 1.0 / x -> rcp(x)
2517 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
2518 }
2519
2520 // Same as for 1.0, but expand the sign out of the constant.
2521 if (CLHS->isExactlyValue(-1.0)) {
2522 // -1.0 / x -> rcp (fneg x)
2523 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2524 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS);
2525 }
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002526 }
2527 }
2528
Wei Dinged0f97f2016-06-09 19:17:15 +00002529 const SDNodeFlags *Flags = Op->getFlags();
2530
2531 if (Unsafe || Flags->hasAllowReciprocal()) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002532 // Turn into multiply by the reciprocal.
2533 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00002534 SDNodeFlags Flags;
2535 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002536 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00002537 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002538 }
2539
2540 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002541}
2542
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002543// Faster 2.5 ULP division that does not support denormals.
2544SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const {
2545 SDLoc SL(Op);
2546 SDValue LHS = Op.getOperand(1);
2547 SDValue RHS = Op.getOperand(2);
2548
2549 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
2550
2551 const APFloat K0Val(BitsToFloat(0x6f800000));
2552 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
2553
2554 const APFloat K1Val(BitsToFloat(0x2f800000));
2555 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
2556
2557 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
2558
2559 EVT SetCCVT =
2560 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
2561
2562 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
2563
2564 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
2565
2566 // TODO: Should this propagate fast-math-flags?
2567 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
2568
2569 // rcp does not support denormals.
2570 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
2571
2572 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
2573
2574 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
2575}
2576
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002577SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002578 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
Eric Christopher538d09d02016-06-07 20:27:12 +00002579 return FastLowered;
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002580
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002581 SDLoc SL(Op);
2582 SDValue LHS = Op.getOperand(0);
2583 SDValue RHS = Op.getOperand(1);
2584
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002585 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002586
Wei Dinged0f97f2016-06-09 19:17:15 +00002587 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002588
Wei Dinged0f97f2016-06-09 19:17:15 +00002589 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, RHS, RHS, LHS);
2590 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, LHS, RHS, LHS);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002591
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002592 // Denominator is scaled to not be denormal, so using rcp is ok.
Wei Dinged0f97f2016-06-09 19:17:15 +00002593 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002594
Wei Dinged0f97f2016-06-09 19:17:15 +00002595 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002596
Wei Dinged0f97f2016-06-09 19:17:15 +00002597 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, ApproxRcp, One);
2598 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, ApproxRcp);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002599
Wei Dinged0f97f2016-06-09 19:17:15 +00002600 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002601
Wei Dinged0f97f2016-06-09 19:17:15 +00002602 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, NumeratorScaled);
2603 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul);
2604 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, NumeratorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002605
Wei Dinged0f97f2016-06-09 19:17:15 +00002606 SDValue Scale = NumeratorScaled.getValue(1);
2607 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, Fma4, Fma1, Fma3, Scale);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002608
Wei Dinged0f97f2016-06-09 19:17:15 +00002609 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002610}
2611
2612SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002613 if (DAG.getTarget().Options.UnsafeFPMath)
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002614 return lowerFastUnsafeFDIV(Op, DAG);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002615
2616 SDLoc SL(Op);
2617 SDValue X = Op.getOperand(0);
2618 SDValue Y = Op.getOperand(1);
2619
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002620 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002621
2622 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
2623
2624 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
2625
2626 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
2627
2628 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
2629
2630 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
2631
2632 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
2633
2634 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
2635
2636 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
2637
2638 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
2639 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
2640
2641 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
2642 NegDivScale0, Mul, DivScale1);
2643
2644 SDValue Scale;
2645
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002646 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002647 // Workaround a hardware bug on SI where the condition output from div_scale
2648 // is not usable.
2649
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002650 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002651
2652 // Figure out if the scale to use for div_fmas.
2653 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
2654 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
2655 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
2656 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
2657
2658 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
2659 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
2660
2661 SDValue Scale0Hi
2662 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
2663 SDValue Scale1Hi
2664 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
2665
2666 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
2667 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
2668 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
2669 } else {
2670 Scale = DivScale1.getValue(1);
2671 }
2672
2673 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
2674 Fma4, Fma3, Mul, Scale);
2675
2676 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002677}
2678
2679SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
2680 EVT VT = Op.getValueType();
2681
2682 if (VT == MVT::f32)
2683 return LowerFDIV32(Op, DAG);
2684
2685 if (VT == MVT::f64)
2686 return LowerFDIV64(Op, DAG);
2687
2688 llvm_unreachable("Unexpected type for fdiv");
2689}
2690
Tom Stellard81d871d2013-11-13 23:36:50 +00002691SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2692 SDLoc DL(Op);
2693 StoreSDNode *Store = cast<StoreSDNode>(Op);
2694 EVT VT = Store->getMemoryVT();
2695
Matt Arsenault95245662016-02-11 05:32:46 +00002696 if (VT == MVT::i1) {
2697 return DAG.getTruncStore(Store->getChain(), DL,
2698 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
2699 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00002700 }
2701
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002702 assert(VT.isVector() &&
2703 Store->getValue().getValueType().getScalarType() == MVT::i32);
2704
2705 unsigned AS = Store->getAddressSpace();
2706 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
2707 AS, Store->getAlignment())) {
2708 return expandUnalignedStore(Store, DAG);
2709 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002710
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002711 unsigned NumElements = VT.getVectorNumElements();
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002712 switch (AS) {
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002713 case AMDGPUAS::GLOBAL_ADDRESS:
2714 case AMDGPUAS::FLAT_ADDRESS:
2715 if (NumElements > 4)
2716 return SplitVectorStore(Op, DAG);
2717 return SDValue();
2718 case AMDGPUAS::PRIVATE_ADDRESS: {
2719 switch (Subtarget->getMaxPrivateElementSize()) {
2720 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002721 return scalarizeVectorStore(Store, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002722 case 8:
2723 if (NumElements > 2)
2724 return SplitVectorStore(Op, DAG);
2725 return SDValue();
2726 case 16:
2727 if (NumElements > 4)
2728 return SplitVectorStore(Op, DAG);
2729 return SDValue();
2730 default:
2731 llvm_unreachable("unsupported private_element_size");
2732 }
2733 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002734 case AMDGPUAS::LOCAL_ADDRESS: {
2735 if (NumElements > 2)
2736 return SplitVectorStore(Op, DAG);
2737
2738 if (NumElements == 2)
2739 return Op;
2740
Matt Arsenault95245662016-02-11 05:32:46 +00002741 // If properly aligned, if we split we might be able to use ds_write_b64.
2742 return SplitVectorStore(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002743 }
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002744 default:
2745 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00002746 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002747}
2748
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002749SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002750 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002751 EVT VT = Op.getValueType();
2752 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00002753 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002754 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
2755 DAG.getNode(ISD::FMUL, DL, VT, Arg,
2756 DAG.getConstantFP(0.5/M_PI, DL,
2757 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002758
2759 switch (Op.getOpcode()) {
2760 case ISD::FCOS:
2761 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
2762 case ISD::FSIN:
2763 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
2764 default:
2765 llvm_unreachable("Wrong trig opcode");
2766 }
2767}
2768
Tom Stellard354a43c2016-04-01 18:27:37 +00002769SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
2770 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
2771 assert(AtomicNode->isCompareAndSwap());
2772 unsigned AS = AtomicNode->getAddressSpace();
2773
2774 // No custom lowering required for local address space
2775 if (!isFlatGlobalAddrSpace(AS))
2776 return Op;
2777
2778 // Non-local address space requires custom lowering for atomic compare
2779 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
2780 SDLoc DL(Op);
2781 SDValue ChainIn = Op.getOperand(0);
2782 SDValue Addr = Op.getOperand(1);
2783 SDValue Old = Op.getOperand(2);
2784 SDValue New = Op.getOperand(3);
2785 EVT VT = Op.getValueType();
2786 MVT SimpleVT = VT.getSimpleVT();
2787 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
2788
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002789 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
Tom Stellard354a43c2016-04-01 18:27:37 +00002790 SDValue Ops[] = { ChainIn, Addr, NewOld };
Matt Arsenault88701812016-06-09 23:42:48 +00002791
2792 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(),
2793 Ops, VT, AtomicNode->getMemOperand());
Tom Stellard354a43c2016-04-01 18:27:37 +00002794}
2795
Tom Stellard75aadc22012-12-11 21:25:42 +00002796//===----------------------------------------------------------------------===//
2797// Custom DAG optimizations
2798//===----------------------------------------------------------------------===//
2799
Matt Arsenault364a6742014-06-11 17:50:44 +00002800SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00002801 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00002802 EVT VT = N->getValueType(0);
2803 EVT ScalarVT = VT.getScalarType();
2804 if (ScalarVT != MVT::f32)
2805 return SDValue();
2806
2807 SelectionDAG &DAG = DCI.DAG;
2808 SDLoc DL(N);
2809
2810 SDValue Src = N->getOperand(0);
2811 EVT SrcVT = Src.getValueType();
2812
2813 // TODO: We could try to match extracting the higher bytes, which would be
2814 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
2815 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
2816 // about in practice.
2817 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
2818 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
2819 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
2820 DCI.AddToWorklist(Cvt.getNode());
2821 return Cvt;
2822 }
2823 }
2824
Matt Arsenault364a6742014-06-11 17:50:44 +00002825 return SDValue();
2826}
2827
Eric Christopher6c5b5112015-03-11 18:43:21 +00002828/// \brief Return true if the given offset Size in bytes can be folded into
2829/// the immediate offsets of a memory instruction for the given address space.
2830static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002831 const SISubtarget &STI) {
Eric Christopher6c5b5112015-03-11 18:43:21 +00002832 switch (AS) {
2833 case AMDGPUAS::GLOBAL_ADDRESS: {
2834 // MUBUF instructions a 12-bit offset in bytes.
2835 return isUInt<12>(OffsetSize);
2836 }
2837 case AMDGPUAS::CONSTANT_ADDRESS: {
2838 // SMRD instructions have an 8-bit offset in dwords on SI and
2839 // a 20-bit offset in bytes on VI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002840 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Eric Christopher6c5b5112015-03-11 18:43:21 +00002841 return isUInt<20>(OffsetSize);
2842 else
2843 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
2844 }
2845 case AMDGPUAS::LOCAL_ADDRESS:
2846 case AMDGPUAS::REGION_ADDRESS: {
2847 // The single offset versions have a 16-bit offset in bytes.
2848 return isUInt<16>(OffsetSize);
2849 }
2850 case AMDGPUAS::PRIVATE_ADDRESS:
2851 // Indirect register addressing does not use any offsets.
2852 default:
2853 return 0;
2854 }
2855}
2856
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002857// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
2858
2859// This is a variant of
2860// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
2861//
2862// The normal DAG combiner will do this, but only if the add has one use since
2863// that would increase the number of instructions.
2864//
2865// This prevents us from seeing a constant offset that can be folded into a
2866// memory instruction's addressing mode. If we know the resulting add offset of
2867// a pointer can be folded into an addressing offset, we can replace the pointer
2868// operand with the add of new constant offset. This eliminates one of the uses,
2869// and may allow the remaining use to also be simplified.
2870//
2871SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
2872 unsigned AddrSpace,
2873 DAGCombinerInfo &DCI) const {
2874 SDValue N0 = N->getOperand(0);
2875 SDValue N1 = N->getOperand(1);
2876
2877 if (N0.getOpcode() != ISD::ADD)
2878 return SDValue();
2879
2880 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
2881 if (!CN1)
2882 return SDValue();
2883
2884 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2885 if (!CAdd)
2886 return SDValue();
2887
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002888 // If the resulting offset is too large, we can't fold it into the addressing
2889 // mode offset.
2890 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002891 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget()))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002892 return SDValue();
2893
2894 SelectionDAG &DAG = DCI.DAG;
2895 SDLoc SL(N);
2896 EVT VT = N->getValueType(0);
2897
2898 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002899 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002900
2901 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
2902}
2903
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002904static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) {
2905 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) ||
2906 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) ||
2907 (Opc == ISD::XOR && Val == 0);
2908}
2909
2910// Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This
2911// will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit
2912// integer combine opportunities since most 64-bit operations are decomposed
2913// this way. TODO: We won't want this for SALU especially if it is an inline
2914// immediate.
2915SDValue SITargetLowering::splitBinaryBitConstantOp(
2916 DAGCombinerInfo &DCI,
2917 const SDLoc &SL,
2918 unsigned Opc, SDValue LHS,
2919 const ConstantSDNode *CRHS) const {
2920 uint64_t Val = CRHS->getZExtValue();
2921 uint32_t ValLo = Lo_32(Val);
2922 uint32_t ValHi = Hi_32(Val);
2923 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
2924
2925 if ((bitOpWithConstantIsReducible(Opc, ValLo) ||
2926 bitOpWithConstantIsReducible(Opc, ValHi)) ||
2927 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) {
2928 // If we need to materialize a 64-bit immediate, it will be split up later
2929 // anyway. Avoid creating the harder to understand 64-bit immediate
2930 // materialization.
2931 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi);
2932 }
2933
2934 return SDValue();
2935}
2936
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002937SDValue SITargetLowering::performAndCombine(SDNode *N,
2938 DAGCombinerInfo &DCI) const {
2939 if (DCI.isBeforeLegalize())
2940 return SDValue();
2941
2942 SelectionDAG &DAG = DCI.DAG;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002943 EVT VT = N->getValueType(0);
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002944 SDValue LHS = N->getOperand(0);
2945 SDValue RHS = N->getOperand(1);
2946
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002947
2948 if (VT == MVT::i64) {
2949 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
2950 if (CRHS) {
2951 if (SDValue Split
2952 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS))
2953 return Split;
2954 }
2955 }
2956
2957 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
2958 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
2959 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) {
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002960 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2961 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2962
2963 SDValue X = LHS.getOperand(0);
2964 SDValue Y = RHS.getOperand(0);
2965 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2966 return SDValue();
2967
2968 if (LCC == ISD::SETO) {
2969 if (X != LHS.getOperand(1))
2970 return SDValue();
2971
2972 if (RCC == ISD::SETUNE) {
2973 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2974 if (!C1 || !C1->isInfinity() || C1->isNegative())
2975 return SDValue();
2976
2977 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2978 SIInstrFlags::N_SUBNORMAL |
2979 SIInstrFlags::N_ZERO |
2980 SIInstrFlags::P_ZERO |
2981 SIInstrFlags::P_SUBNORMAL |
2982 SIInstrFlags::P_NORMAL;
2983
2984 static_assert(((~(SIInstrFlags::S_NAN |
2985 SIInstrFlags::Q_NAN |
2986 SIInstrFlags::N_INFINITY |
2987 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2988 "mask not equal");
2989
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002990 SDLoc DL(N);
2991 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2992 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002993 }
2994 }
2995 }
2996
2997 return SDValue();
2998}
2999
Matt Arsenaultf2290332015-01-06 23:00:39 +00003000SDValue SITargetLowering::performOrCombine(SDNode *N,
3001 DAGCombinerInfo &DCI) const {
3002 SelectionDAG &DAG = DCI.DAG;
3003 SDValue LHS = N->getOperand(0);
3004 SDValue RHS = N->getOperand(1);
3005
Matt Arsenault3b082382016-04-12 18:24:38 +00003006 EVT VT = N->getValueType(0);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003007 if (VT == MVT::i1) {
3008 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
3009 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
3010 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
3011 SDValue Src = LHS.getOperand(0);
3012 if (Src != RHS.getOperand(0))
3013 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003014
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003015 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
3016 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
3017 if (!CLHS || !CRHS)
3018 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003019
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003020 // Only 10 bits are used.
3021 static const uint32_t MaxMask = 0x3ff;
Matt Arsenault3b082382016-04-12 18:24:38 +00003022
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003023 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
3024 SDLoc DL(N);
3025 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
3026 Src, DAG.getConstant(NewMask, DL, MVT::i32));
3027 }
Matt Arsenault3b082382016-04-12 18:24:38 +00003028
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003029 return SDValue();
3030 }
3031
3032 if (VT != MVT::i64)
3033 return SDValue();
3034
3035 // TODO: This could be a generic combine with a predicate for extracting the
3036 // high half of an integer being free.
3037
3038 // (or i64:x, (zero_extend i32:y)) ->
3039 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
3040 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
3041 RHS.getOpcode() != ISD::ZERO_EXTEND)
3042 std::swap(LHS, RHS);
3043
3044 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
3045 SDValue ExtSrc = RHS.getOperand(0);
3046 EVT SrcVT = ExtSrc.getValueType();
3047 if (SrcVT == MVT::i32) {
3048 SDLoc SL(N);
3049 SDValue LowLHS, HiBits;
3050 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
3051 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
3052
3053 DCI.AddToWorklist(LowOr.getNode());
3054 DCI.AddToWorklist(HiBits.getNode());
3055
3056 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3057 LowOr, HiBits);
3058 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
Matt Arsenault3b082382016-04-12 18:24:38 +00003059 }
3060 }
3061
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003062 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
3063 if (CRHS) {
3064 if (SDValue Split
3065 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS))
3066 return Split;
3067 }
Matt Arsenaultf2290332015-01-06 23:00:39 +00003068
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003069 return SDValue();
3070}
Matt Arsenaultf2290332015-01-06 23:00:39 +00003071
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003072SDValue SITargetLowering::performXorCombine(SDNode *N,
3073 DAGCombinerInfo &DCI) const {
3074 EVT VT = N->getValueType(0);
3075 if (VT != MVT::i64)
3076 return SDValue();
Matt Arsenaultf2290332015-01-06 23:00:39 +00003077
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003078 SDValue LHS = N->getOperand(0);
3079 SDValue RHS = N->getOperand(1);
3080
3081 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
3082 if (CRHS) {
3083 if (SDValue Split
3084 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS))
3085 return Split;
Matt Arsenaultf2290332015-01-06 23:00:39 +00003086 }
3087
3088 return SDValue();
3089}
3090
3091SDValue SITargetLowering::performClassCombine(SDNode *N,
3092 DAGCombinerInfo &DCI) const {
3093 SelectionDAG &DAG = DCI.DAG;
3094 SDValue Mask = N->getOperand(1);
3095
3096 // fp_class x, 0 -> false
3097 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
3098 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003099 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003100 }
3101
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003102 if (N->getOperand(0).isUndef())
3103 return DAG.getUNDEF(MVT::i1);
3104
Matt Arsenaultf2290332015-01-06 23:00:39 +00003105 return SDValue();
3106}
3107
Matt Arsenault9cd90712016-04-14 01:42:16 +00003108// Constant fold canonicalize.
3109SDValue SITargetLowering::performFCanonicalizeCombine(
3110 SDNode *N,
3111 DAGCombinerInfo &DCI) const {
3112 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
3113 if (!CFP)
3114 return SDValue();
3115
3116 SelectionDAG &DAG = DCI.DAG;
3117 const APFloat &C = CFP->getValueAPF();
3118
3119 // Flush denormals to 0 if not enabled.
3120 if (C.isDenormal()) {
3121 EVT VT = N->getValueType(0);
3122 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
3123 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3124
3125 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
3126 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3127 }
3128
3129 if (C.isNaN()) {
3130 EVT VT = N->getValueType(0);
3131 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
3132 if (C.isSignaling()) {
3133 // Quiet a signaling NaN.
3134 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3135 }
3136
3137 // Make sure it is the canonical NaN bitpattern.
3138 //
3139 // TODO: Can we use -1 as the canonical NaN value since it's an inline
3140 // immediate?
3141 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
3142 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3143 }
3144
3145 return SDValue(CFP, 0);
3146}
3147
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003148static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
3149 switch (Opc) {
3150 case ISD::FMAXNUM:
3151 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003152 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003153 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003154 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003155 return AMDGPUISD::UMAX3;
3156 case ISD::FMINNUM:
3157 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003158 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003159 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003160 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003161 return AMDGPUISD::UMIN3;
3162 default:
3163 llvm_unreachable("Not a min/max opcode");
3164 }
3165}
3166
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003167static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3168 SDValue Op0, SDValue Op1, bool Signed) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003169 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
3170 if (!K1)
3171 return SDValue();
3172
3173 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
3174 if (!K0)
3175 return SDValue();
3176
Matt Arsenaultf639c322016-01-28 20:53:42 +00003177 if (Signed) {
3178 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
3179 return SDValue();
3180 } else {
3181 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
3182 return SDValue();
3183 }
3184
3185 EVT VT = K0->getValueType(0);
3186 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
3187 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
3188}
3189
3190static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
3191 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
3192 return true;
3193
3194 return DAG.isKnownNeverNaN(Op);
3195}
3196
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003197static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3198 SDValue Op0, SDValue Op1) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003199 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
3200 if (!K1)
3201 return SDValue();
3202
3203 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
3204 if (!K0)
3205 return SDValue();
3206
3207 // Ordered >= (although NaN inputs should have folded away by now).
3208 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
3209 if (Cmp == APFloat::cmpGreaterThan)
3210 return SDValue();
3211
3212 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
3213 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
3214 // give the other result, which is different from med3 with a NaN input.
3215 SDValue Var = Op0.getOperand(0);
3216 if (!isKnownNeverSNan(DAG, Var))
3217 return SDValue();
3218
3219 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
3220 Var, SDValue(K0, 0), SDValue(K1, 0));
3221}
3222
3223SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
3224 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003225 SelectionDAG &DAG = DCI.DAG;
3226
3227 unsigned Opc = N->getOpcode();
3228 SDValue Op0 = N->getOperand(0);
3229 SDValue Op1 = N->getOperand(1);
3230
3231 // Only do this if the inner op has one use since this will just increases
3232 // register pressure for no benefit.
3233
Matt Arsenault5b39b342016-01-28 20:53:48 +00003234 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
3235 // max(max(a, b), c) -> max3(a, b, c)
3236 // min(min(a, b), c) -> min3(a, b, c)
3237 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
3238 SDLoc DL(N);
3239 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3240 DL,
3241 N->getValueType(0),
3242 Op0.getOperand(0),
3243 Op0.getOperand(1),
3244 Op1);
3245 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003246
Matt Arsenault5b39b342016-01-28 20:53:48 +00003247 // Try commuted.
3248 // max(a, max(b, c)) -> max3(a, b, c)
3249 // min(a, min(b, c)) -> min3(a, b, c)
3250 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
3251 SDLoc DL(N);
3252 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3253 DL,
3254 N->getValueType(0),
3255 Op0,
3256 Op1.getOperand(0),
3257 Op1.getOperand(1));
3258 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003259 }
3260
Matt Arsenaultf639c322016-01-28 20:53:42 +00003261 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
3262 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
3263 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
3264 return Med3;
3265 }
3266
3267 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
3268 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
3269 return Med3;
3270 }
3271
3272 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00003273 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
3274 (Opc == AMDGPUISD::FMIN_LEGACY &&
3275 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00003276 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
3277 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
3278 return Res;
3279 }
3280
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003281 return SDValue();
3282}
3283
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003284SDValue SITargetLowering::performSetCCCombine(SDNode *N,
3285 DAGCombinerInfo &DCI) const {
3286 SelectionDAG &DAG = DCI.DAG;
3287 SDLoc SL(N);
3288
3289 SDValue LHS = N->getOperand(0);
3290 SDValue RHS = N->getOperand(1);
3291 EVT VT = LHS.getValueType();
3292
3293 if (VT != MVT::f32 && VT != MVT::f64)
3294 return SDValue();
3295
3296 // Match isinf pattern
3297 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
3298 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
3299 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
3300 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
3301 if (!CRHS)
3302 return SDValue();
3303
3304 const APFloat &APF = CRHS->getValueAPF();
3305 if (APF.isInfinity() && !APF.isNegative()) {
3306 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003307 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
3308 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003309 }
3310 }
3311
3312 return SDValue();
3313}
3314
Tom Stellard75aadc22012-12-11 21:25:42 +00003315SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
3316 DAGCombinerInfo &DCI) const {
3317 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003318 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00003319
3320 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00003321 default:
3322 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003323 case ISD::SETCC:
3324 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00003325 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003326 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003327 case ISD::SMAX:
3328 case ISD::SMIN:
3329 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00003330 case ISD::UMIN:
3331 case AMDGPUISD::FMIN_LEGACY:
3332 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003333 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00003334 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003335 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00003336 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003337 break;
3338 }
Matt Arsenault364a6742014-06-11 17:50:44 +00003339
3340 case AMDGPUISD::CVT_F32_UBYTE0:
3341 case AMDGPUISD::CVT_F32_UBYTE1:
3342 case AMDGPUISD::CVT_F32_UBYTE2:
3343 case AMDGPUISD::CVT_F32_UBYTE3: {
3344 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
Matt Arsenault364a6742014-06-11 17:50:44 +00003345 SDValue Src = N->getOperand(0);
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003346
Matt Arsenault327bb5a2016-07-01 22:47:50 +00003347 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero.
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003348 if (Src.getOpcode() == ISD::SRL) {
3349 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
3350 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
3351 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
3352
3353 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(1))) {
3354 unsigned SrcOffset = C->getZExtValue() + 8 * Offset;
3355 if (SrcOffset < 32 && SrcOffset % 8 == 0) {
3356 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, DL,
3357 MVT::f32, Src.getOperand(0));
3358 }
3359 }
3360 }
3361
Matt Arsenault364a6742014-06-11 17:50:44 +00003362 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
3363
3364 APInt KnownZero, KnownOne;
3365 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3366 !DCI.isBeforeLegalizeOps());
3367 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3368 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
3369 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
3370 DCI.CommitTargetLoweringOpt(TLO);
3371 }
3372
3373 break;
3374 }
3375
3376 case ISD::UINT_TO_FP: {
3377 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003378 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003379 case ISD::FADD: {
3380 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3381 break;
3382
3383 EVT VT = N->getValueType(0);
3384 if (VT != MVT::f32)
3385 break;
3386
Matt Arsenault8d630032015-02-20 22:10:41 +00003387 // Only do this if we are not trying to support denormals. v_mad_f32 does
3388 // not support denormals ever.
3389 if (Subtarget->hasFP32Denormals())
3390 break;
3391
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003392 SDValue LHS = N->getOperand(0);
3393 SDValue RHS = N->getOperand(1);
3394
3395 // These should really be instruction patterns, but writing patterns with
3396 // source modiifiers is a pain.
3397
3398 // fadd (fadd (a, a), b) -> mad 2.0, a, b
3399 if (LHS.getOpcode() == ISD::FADD) {
3400 SDValue A = LHS.getOperand(0);
3401 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003402 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003403 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003404 }
3405 }
3406
3407 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
3408 if (RHS.getOpcode() == ISD::FADD) {
3409 SDValue A = RHS.getOperand(0);
3410 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003411 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003412 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003413 }
3414 }
3415
Matt Arsenault8d630032015-02-20 22:10:41 +00003416 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003417 }
Matt Arsenault8675db12014-08-29 16:01:14 +00003418 case ISD::FSUB: {
3419 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3420 break;
3421
3422 EVT VT = N->getValueType(0);
3423
3424 // Try to get the fneg to fold into the source modifier. This undoes generic
3425 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00003426 //
3427 // Only do this if we are not trying to support denormals. v_mad_f32 does
3428 // not support denormals ever.
3429 if (VT == MVT::f32 &&
3430 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00003431 SDValue LHS = N->getOperand(0);
3432 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003433 if (LHS.getOpcode() == ISD::FADD) {
3434 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
3435
3436 SDValue A = LHS.getOperand(0);
3437 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003438 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003439 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
3440
Matt Arsenault8d630032015-02-20 22:10:41 +00003441 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003442 }
3443 }
3444
3445 if (RHS.getOpcode() == ISD::FADD) {
3446 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
3447
3448 SDValue A = RHS.getOperand(0);
3449 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003450 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003451 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003452 }
3453 }
Matt Arsenault8d630032015-02-20 22:10:41 +00003454
3455 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00003456 }
3457
3458 break;
3459 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003460 case ISD::LOAD:
3461 case ISD::STORE:
3462 case ISD::ATOMIC_LOAD:
3463 case ISD::ATOMIC_STORE:
3464 case ISD::ATOMIC_CMP_SWAP:
3465 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
3466 case ISD::ATOMIC_SWAP:
3467 case ISD::ATOMIC_LOAD_ADD:
3468 case ISD::ATOMIC_LOAD_SUB:
3469 case ISD::ATOMIC_LOAD_AND:
3470 case ISD::ATOMIC_LOAD_OR:
3471 case ISD::ATOMIC_LOAD_XOR:
3472 case ISD::ATOMIC_LOAD_NAND:
3473 case ISD::ATOMIC_LOAD_MIN:
3474 case ISD::ATOMIC_LOAD_MAX:
3475 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003476 case ISD::ATOMIC_LOAD_UMAX:
3477 case AMDGPUISD::ATOMIC_INC:
3478 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003479 if (DCI.isBeforeLegalize())
3480 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003481
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003482 MemSDNode *MemNode = cast<MemSDNode>(N);
3483 SDValue Ptr = MemNode->getBasePtr();
3484
3485 // TODO: We could also do this for multiplies.
3486 unsigned AS = MemNode->getAddressSpace();
3487 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
3488 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
3489 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00003490 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003491
3492 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
3493 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
3494 }
3495 }
3496 break;
3497 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003498 case ISD::AND:
3499 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003500 case ISD::OR:
3501 return performOrCombine(N, DCI);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003502 case ISD::XOR:
3503 return performXorCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003504 case AMDGPUISD::FP_CLASS:
3505 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00003506 case ISD::FCANONICALIZE:
3507 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003508 case AMDGPUISD::FRACT:
3509 case AMDGPUISD::RCP:
3510 case AMDGPUISD::RSQ:
Matt Arsenault32fc5272016-07-26 16:45:45 +00003511 case AMDGPUISD::RCP_LEGACY:
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003512 case AMDGPUISD::RSQ_LEGACY:
3513 case AMDGPUISD::RSQ_CLAMP:
3514 case AMDGPUISD::LDEXP: {
3515 SDValue Src = N->getOperand(0);
3516 if (Src.isUndef())
3517 return Src;
3518 break;
3519 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003520 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003521 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00003522}
Christian Konigd910b7d2013-02-26 17:52:16 +00003523
Christian Konig8e06e2a2013-04-10 08:39:08 +00003524/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00003525static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00003526 switch (Idx) {
3527 default: return 0;
3528 case AMDGPU::sub0: return 0;
3529 case AMDGPU::sub1: return 1;
3530 case AMDGPU::sub2: return 2;
3531 case AMDGPU::sub3: return 3;
3532 }
3533}
3534
3535/// \brief Adjust the writemask of MIMG instructions
3536void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
3537 SelectionDAG &DAG) const {
3538 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00003539 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003540 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
3541 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00003542 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003543
3544 // Try to figure out the used register components
3545 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
3546 I != E; ++I) {
3547
3548 // Abort if we can't understand the usage
3549 if (!I->isMachineOpcode() ||
3550 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
3551 return;
3552
Tom Stellard54774e52013-10-23 02:53:47 +00003553 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
3554 // Note that subregs are packed, i.e. Lane==0 is the first bit set
3555 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
3556 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00003557 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00003558
Tom Stellard54774e52013-10-23 02:53:47 +00003559 // Set which texture component corresponds to the lane.
3560 unsigned Comp;
3561 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
3562 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00003563 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00003564 Dmask &= ~(1 << Comp);
3565 }
3566
Christian Konig8e06e2a2013-04-10 08:39:08 +00003567 // Abort if we have more than one user per component
3568 if (Users[Lane])
3569 return;
3570
3571 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00003572 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003573 }
3574
Tom Stellard54774e52013-10-23 02:53:47 +00003575 // Abort if there's no change
3576 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00003577 return;
3578
3579 // Adjust the writemask in the node
3580 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003581 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003582 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003583 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00003584 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003585
Christian Konig8b1ed282013-04-10 08:39:16 +00003586 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00003587 // (if NewDmask has only one bit set...)
3588 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003589 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
3590 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00003591 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003592 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00003593 SDValue(Node, 0), RC);
3594 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
3595 return;
3596 }
3597
Christian Konig8e06e2a2013-04-10 08:39:08 +00003598 // Update the users of the node with the new indices
3599 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
3600
3601 SDNode *User = Users[i];
3602 if (!User)
3603 continue;
3604
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003605 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003606 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
3607
3608 switch (Idx) {
3609 default: break;
3610 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
3611 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
3612 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
3613 }
3614 }
3615}
3616
Tom Stellardc98ee202015-07-16 19:40:07 +00003617static bool isFrameIndexOp(SDValue Op) {
3618 if (Op.getOpcode() == ISD::AssertZext)
3619 Op = Op.getOperand(0);
3620
3621 return isa<FrameIndexSDNode>(Op);
3622}
3623
Tom Stellard3457a842014-10-09 19:06:00 +00003624/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
3625/// with frame index operands.
3626/// LLVM assumes that inputs are to these instructions are registers.
3627void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
3628 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003629
3630 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00003631 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00003632 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00003633 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003634 continue;
3635 }
3636
Tom Stellard3457a842014-10-09 19:06:00 +00003637 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003638 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00003639 Node->getOperand(i).getValueType(),
3640 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003641 }
3642
Tom Stellard3457a842014-10-09 19:06:00 +00003643 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003644}
3645
Matt Arsenault08d84942014-06-03 23:06:13 +00003646/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00003647SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
3648 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003649 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003650 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00003651
Nicolai Haehnlec06bfa12016-07-11 21:59:43 +00003652 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() &&
3653 !TII->isGather4(Opcode))
Christian Konig8e06e2a2013-04-10 08:39:08 +00003654 adjustWritemask(Node, DAG);
3655
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003656 if (Opcode == AMDGPU::INSERT_SUBREG ||
3657 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003658 legalizeTargetIndependentNode(Node, DAG);
3659 return Node;
3660 }
Tom Stellard654d6692015-01-08 15:08:17 +00003661 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003662}
Christian Konig8b1ed282013-04-10 08:39:16 +00003663
3664/// \brief Assign the register class depending on the number of
3665/// bits set in the writemask
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003666void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Christian Konig8b1ed282013-04-10 08:39:16 +00003667 SDNode *Node) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003668 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003669
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003670 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003671
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003672 if (TII->isVOP3(MI.getOpcode())) {
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003673 // Make sure constant bus requirements are respected.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003674 TII->legalizeOperandsVOP3(MRI, MI);
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003675 return;
3676 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00003677
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003678 if (TII->isMIMG(MI)) {
3679 unsigned VReg = MI.getOperand(0).getReg();
3680 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4;
3681 unsigned Writemask = MI.getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003682 unsigned BitsSet = 0;
3683 for (unsigned i = 0; i < 4; ++i)
3684 BitsSet += Writemask & (1 << i) ? 1 : 0;
3685
3686 const TargetRegisterClass *RC;
3687 switch (BitsSet) {
3688 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00003689 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003690 case 2: RC = &AMDGPU::VReg_64RegClass; break;
3691 case 3: RC = &AMDGPU::VReg_96RegClass; break;
3692 }
3693
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003694 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet);
3695 MI.setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003696 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00003697 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00003698 }
3699
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003700 // Replace unused atomics with the no return version.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003701 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003702 if (NoRetAtomicOp != -1) {
3703 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003704 MI.setDesc(TII->get(NoRetAtomicOp));
3705 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003706 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003707 }
3708
Tom Stellard354a43c2016-04-01 18:27:37 +00003709 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
3710 // instruction, because the return type of these instructions is a vec2 of
3711 // the memory type, so it can be tied to the input operand.
3712 // This means these instructions always have a use, so we need to add a
3713 // special case to check if the atomic has only one extract_subreg use,
3714 // which itself has no uses.
3715 if ((Node->hasNUsesOfValue(1, 0) &&
Nicolai Haehnle750082d2016-04-15 14:42:36 +00003716 Node->use_begin()->isMachineOpcode() &&
Tom Stellard354a43c2016-04-01 18:27:37 +00003717 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
3718 !Node->use_begin()->hasAnyUseOfValue(0))) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003719 unsigned Def = MI.getOperand(0).getReg();
Tom Stellard354a43c2016-04-01 18:27:37 +00003720
3721 // Change this into a noret atomic.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003722 MI.setDesc(TII->get(NoRetAtomicOp));
3723 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003724
3725 // If we only remove the def operand from the atomic instruction, the
3726 // extract_subreg will be left with a use of a vreg without a def.
3727 // So we need to insert an implicit_def to avoid machine verifier
3728 // errors.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00003729 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
Tom Stellard354a43c2016-04-01 18:27:37 +00003730 TII->get(AMDGPU::IMPLICIT_DEF), Def);
3731 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003732 return;
3733 }
Christian Konig8b1ed282013-04-10 08:39:16 +00003734}
Tom Stellard0518ff82013-06-03 17:39:58 +00003735
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003736static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL,
3737 uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003738 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00003739 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
3740}
3741
3742MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003743 const SDLoc &DL,
Matt Arsenault485defe2014-11-05 19:01:17 +00003744 SDValue Ptr) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003745 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault485defe2014-11-05 19:01:17 +00003746
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003747 // Build the half of the subregister with the constants before building the
3748 // full 128-bit register. If we are building multiple resource descriptors,
3749 // this will allow CSEing of the 2-component register.
3750 const SDValue Ops0[] = {
3751 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
3752 buildSMovImm32(DAG, DL, 0),
3753 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
3754 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
3755 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
3756 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003757
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003758 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
3759 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00003760
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003761 // Combine the constants and the pointer.
3762 const SDValue Ops1[] = {
3763 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
3764 Ptr,
3765 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
3766 SubRegHi,
3767 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
3768 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003769
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003770 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00003771}
3772
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003773/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003774/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
3775/// of the resource descriptor) to create an offset, which is added to
3776/// the resource pointer.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003777MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL,
3778 SDValue Ptr, uint32_t RsrcDword1,
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003779 uint64_t RsrcDword2And3) const {
3780 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
3781 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
3782 if (RsrcDword1) {
3783 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003784 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
3785 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003786 }
3787
3788 SDValue DataLo = buildSMovImm32(DAG, DL,
3789 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
3790 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
3791
3792 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003793 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003794 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003795 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003796 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003797 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003798 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003799 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003800 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003801 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003802 };
3803
3804 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
3805}
3806
Tom Stellard94593ee2013-06-03 17:40:18 +00003807SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3808 const TargetRegisterClass *RC,
3809 unsigned Reg, EVT VT) const {
3810 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
3811
3812 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
3813 cast<RegisterSDNode>(VReg)->getReg(), VT);
3814}
Tom Stellardd7e6f132015-04-08 01:09:26 +00003815
3816//===----------------------------------------------------------------------===//
3817// SI Inline Assembly Support
3818//===----------------------------------------------------------------------===//
3819
3820std::pair<unsigned, const TargetRegisterClass *>
3821SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003822 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00003823 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003824
3825 if (Constraint.size() == 1) {
3826 switch (Constraint[0]) {
3827 case 's':
3828 case 'r':
3829 switch (VT.getSizeInBits()) {
3830 default:
3831 return std::make_pair(0U, nullptr);
3832 case 32:
Matt Arsenaulta609e2d2016-08-30 20:50:08 +00003833 return std::make_pair(0U, &AMDGPU::SReg_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003834 case 64:
3835 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
3836 case 128:
3837 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
3838 case 256:
3839 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
3840 }
3841
3842 case 'v':
3843 switch (VT.getSizeInBits()) {
3844 default:
3845 return std::make_pair(0U, nullptr);
3846 case 32:
3847 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
3848 case 64:
3849 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
3850 case 96:
3851 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
3852 case 128:
3853 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
3854 case 256:
3855 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
3856 case 512:
3857 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
3858 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00003859 }
3860 }
3861
3862 if (Constraint.size() > 1) {
3863 const TargetRegisterClass *RC = nullptr;
3864 if (Constraint[1] == 'v') {
3865 RC = &AMDGPU::VGPR_32RegClass;
3866 } else if (Constraint[1] == 's') {
3867 RC = &AMDGPU::SGPR_32RegClass;
3868 }
3869
3870 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00003871 uint32_t Idx;
3872 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
3873 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00003874 return std::make_pair(RC->getRegister(Idx), RC);
3875 }
3876 }
3877 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
3878}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003879
3880SITargetLowering::ConstraintType
3881SITargetLowering::getConstraintType(StringRef Constraint) const {
3882 if (Constraint.size() == 1) {
3883 switch (Constraint[0]) {
3884 default: break;
3885 case 's':
3886 case 'v':
3887 return C_RegisterClass;
3888 }
3889 }
3890 return TargetLowering::getConstraintType(Constraint);
3891}