blob: 2674efd3373a636540c611ae40782f70cddd1bb5 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief Custom DAG lowering for SI
12//
13//===----------------------------------------------------------------------===//
14
NAKAMURA Takumi45e0a832014-07-20 11:15:07 +000015#ifdef _MSC_VER
16// Provide M_PI.
17#define _USE_MATH_DEFINES
18#include <cmath>
19#endif
20
Christian Konig99ee0f42013-03-07 09:04:14 +000021#include "AMDGPU.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000022#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000023#include "AMDGPUSubtarget.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000024#include "SIISelLowering.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025#include "SIInstrInfo.h"
26#include "SIMachineFunctionInfo.h"
27#include "SIRegisterInfo.h"
Alexey Samsonova253bf92014-08-27 19:36:53 +000028#include "llvm/ADT/BitVector.h"
Matt Arsenault9a10cea2016-01-26 04:29:24 +000029#include "llvm/ADT/StringSwitch.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000030#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
32#include "llvm/CodeGen/MachineRegisterInfo.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Oliver Stannard7e7d9832016-02-02 13:52:43 +000034#include "llvm/IR/DiagnosticInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000035#include "llvm/IR/Function.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000036
37using namespace llvm;
38
Tom Stellardf110f8f2016-04-14 16:27:03 +000039static unsigned findFirstFreeSGPR(CCState &CCInfo) {
40 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
41 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) {
42 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) {
43 return AMDGPU::SGPR0 + Reg;
44 }
45 }
46 llvm_unreachable("Cannot allocate sgpr");
47}
48
Eric Christopher7792e322015-01-30 23:24:40 +000049SITargetLowering::SITargetLowering(TargetMachine &TM,
50 const AMDGPUSubtarget &STI)
51 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000052 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000053 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000054
Tom Stellard334b29c2014-04-17 21:00:09 +000055 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000056 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000057
Tom Stellard436780b2014-05-15 14:41:57 +000058 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
59 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
60 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000061
Matt Arsenault61001bb2015-11-25 19:58:34 +000062 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
63 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
64
Tom Stellard436780b2014-05-15 14:41:57 +000065 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
66 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000067
Tom Stellardf0a21072014-11-18 20:39:39 +000068 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000069 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
70
Tom Stellardf0a21072014-11-18 20:39:39 +000071 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000072 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000073
Eric Christopher23a3a7c2015-02-26 00:00:24 +000074 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000075
Christian Konig2989ffc2013-03-18 11:34:16 +000076 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
77 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
78 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
79 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
80
Tom Stellard75aadc22012-12-11 21:25:42 +000081 setOperationAction(ISD::ADD, MVT::i32, Legal);
Matt Arsenaulte8d21462013-11-18 20:09:40 +000082 setOperationAction(ISD::ADDC, MVT::i32, Legal);
83 setOperationAction(ISD::ADDE, MVT::i32, Legal);
Matt Arsenaultb8b51532014-06-23 18:00:38 +000084 setOperationAction(ISD::SUBC, MVT::i32, Legal);
85 setOperationAction(ISD::SUBE, MVT::i32, Legal);
Aaron Watrydaabb202013-06-25 13:55:52 +000086
Matt Arsenaultad14ce82014-07-19 18:44:39 +000087 setOperationAction(ISD::FSIN, MVT::f32, Custom);
88 setOperationAction(ISD::FCOS, MVT::f32, Custom);
89
Matt Arsenault7c936902014-10-21 23:01:01 +000090 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
91 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
92
Tom Stellard35bb18c2013-08-26 15:06:04 +000093 // We need to custom lower vector stores from local memory
Tom Stellard35bb18c2013-08-26 15:06:04 +000094 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000095 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
96 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
97
98 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
99 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000100
Tom Stellard1c8788e2014-03-07 20:12:33 +0000101 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +0000102 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
103
Tom Stellard0ec134f2014-02-04 17:18:40 +0000104 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +0000105 setOperationAction(ISD::SELECT, MVT::f64, Promote);
106 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000107
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000108 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
109 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
110 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
111 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000112
Tom Stellardd1efda82016-01-20 21:48:24 +0000113 setOperationAction(ISD::SETCC, MVT::i1, Promote);
Tom Stellard83747202013-07-18 21:43:53 +0000114 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
115 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
116
Matt Arsenaulte306a322014-10-21 16:25:08 +0000117 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
Matt Arsenaultd0792852015-12-14 17:25:38 +0000118 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000119
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
123
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000125 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
127
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000128 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000129 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
130 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
131
Matt Arsenault94812212014-11-14 18:18:16 +0000132 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000133 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
134
Tom Stellard94593ee2013-06-03 17:40:18 +0000135 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000136 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
137 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
138 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000139
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000140 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
141
Tom Stellardafcf12f2013-09-12 02:55:14 +0000142 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000143 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000144 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
145 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
146 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
147 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000148
Matt Arsenault61738cb2016-02-27 08:53:46 +0000149 // On SI this is s_memtime and s_memrealtime on VI.
150 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
151
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000152 for (MVT VT : MVT::integer_valuetypes()) {
Matt Arsenaultbd223422015-01-14 01:35:17 +0000153 if (VT == MVT::i64)
154 continue;
155
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000156 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000157 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
158 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000159 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000160
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000161 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000162 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
163 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000164 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000165
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000166 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000167 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
168 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000169 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
170 }
171
172 for (MVT VT : MVT::integer_vector_valuetypes()) {
173 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
174 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
175 }
176
177 for (MVT VT : MVT::fp_valuetypes())
178 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000179
Matt Arsenault61001bb2015-11-25 19:58:34 +0000180 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
181 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
182
Matt Arsenault6f243792013-09-05 19:41:10 +0000183 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000184 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
Matt Arsenaulte1ce3442015-07-31 04:12:04 +0000185 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000186 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000187
Matt Arsenault61001bb2015-11-25 19:58:34 +0000188
189 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
190
191 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
192 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
193
Matt Arsenault470acd82014-04-15 22:28:39 +0000194 setOperationAction(ISD::LOAD, MVT::i1, Custom);
195
Matt Arsenault61001bb2015-11-25 19:58:34 +0000196 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
197 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
198
199 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
200 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
201
202 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
203
Tom Stellardfd155822013-08-26 15:05:36 +0000204 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000205 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000206 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000207
Tom Stellard5f337882014-04-29 23:12:43 +0000208 // These should use UDIVREM, so set them to expand
209 setOperationAction(ISD::UDIV, MVT::i64, Expand);
210 setOperationAction(ISD::UREM, MVT::i64, Expand);
211
Matt Arsenault0d89e842014-07-15 21:44:37 +0000212 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
213 setOperationAction(ISD::SELECT, MVT::i1, Promote);
214
Matt Arsenault61001bb2015-11-25 19:58:34 +0000215 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
216
217
218 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
219
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000220 // We only support LOAD/STORE and vector manipulation ops for vectors
221 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000222 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000223 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
224 switch(Op) {
225 case ISD::LOAD:
226 case ISD::STORE:
227 case ISD::BUILD_VECTOR:
228 case ISD::BITCAST:
229 case ISD::EXTRACT_VECTOR_ELT:
230 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000231 case ISD::INSERT_SUBVECTOR:
232 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000233 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000234 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000235 case ISD::CONCAT_VECTORS:
236 setOperationAction(Op, VT, Custom);
237 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000238 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000239 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000240 break;
241 }
242 }
243 }
244
Matt Arsenault61001bb2015-11-25 19:58:34 +0000245 // Most operations are naturally 32-bit vector operations. We only support
246 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
247 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
248 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
249 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
250
251 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
252 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
253
254 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
255 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
256
257 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
258 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
259 }
260
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000261 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
262 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
263 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000264 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000265 }
266
Marek Olsak7d777282015-03-24 13:40:15 +0000267 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000268 setOperationAction(ISD::FDIV, MVT::f32, Custom);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +0000269 setOperationAction(ISD::FDIV, MVT::f64, Custom);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000270
Tom Stellard354a43c2016-04-01 18:27:37 +0000271 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
272 // and output demarshalling
273 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
274 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
275
276 // We can't return success/failure, only the old value,
277 // let LLVM add the comparison
278 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand);
279 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand);
280
Matt Arsenault99c14522016-04-25 19:27:24 +0000281 if (Subtarget->hasFlatAddressSpace()) {
282 setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom);
283 setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom);
284 }
285
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000286 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000287 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000288 setTargetDAGCombine(ISD::FMINNUM);
289 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000290 setTargetDAGCombine(ISD::SMIN);
291 setTargetDAGCombine(ISD::SMAX);
292 setTargetDAGCombine(ISD::UMIN);
293 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000294 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000295 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000296 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000297 setTargetDAGCombine(ISD::UINT_TO_FP);
Matt Arsenault9cd90712016-04-14 01:42:16 +0000298 setTargetDAGCombine(ISD::FCANONICALIZE);
Matt Arsenault364a6742014-06-11 17:50:44 +0000299
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000300 // All memory operations. Some folding on the pointer operand is done to help
301 // matching the constant offsets in the addressing modes.
302 setTargetDAGCombine(ISD::LOAD);
303 setTargetDAGCombine(ISD::STORE);
304 setTargetDAGCombine(ISD::ATOMIC_LOAD);
305 setTargetDAGCombine(ISD::ATOMIC_STORE);
306 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
307 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
308 setTargetDAGCombine(ISD::ATOMIC_SWAP);
309 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
310 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
311 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
312 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
313 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
314 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
315 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
316 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
317 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
318 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
319
Christian Konigeecebd02013-03-26 14:04:02 +0000320 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000321}
322
Tom Stellard0125f2a2013-06-25 02:39:35 +0000323//===----------------------------------------------------------------------===//
324// TargetLowering queries
325//===----------------------------------------------------------------------===//
326
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000327bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
328 const CallInst &CI,
329 unsigned IntrID) const {
330 switch (IntrID) {
331 case Intrinsic::amdgcn_atomic_inc:
332 case Intrinsic::amdgcn_atomic_dec:
333 Info.opc = ISD::INTRINSIC_W_CHAIN;
334 Info.memVT = MVT::getVT(CI.getType());
335 Info.ptrVal = CI.getOperand(0);
336 Info.align = 0;
337 Info.vol = false;
338 Info.readMem = true;
339 Info.writeMem = true;
340 return true;
341 default:
342 return false;
343 }
344}
345
Matt Arsenaulte306a322014-10-21 16:25:08 +0000346bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
347 EVT) const {
348 // SI has some legal vector types, but no legal vector operations. Say no
349 // shuffles are legal in order to prefer scalarizing some vector operations.
350 return false;
351}
352
Tom Stellard70580f82015-07-20 14:28:41 +0000353bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
354 // Flat instructions do not have offsets, and only have the register
355 // address.
356 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
357}
358
Matt Arsenault711b3902015-08-07 20:18:34 +0000359bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
360 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
361 // additionally can do r + r + i with addr64. 32-bit has more addressing
362 // mode options. Depending on the resource constant, it can also do
363 // (i64 r0) + (i32 r1) * (i14 i).
364 //
365 // Private arrays end up using a scratch buffer most of the time, so also
366 // assume those use MUBUF instructions. Scratch loads / stores are currently
367 // implemented as mubuf instructions with offen bit set, so slightly
368 // different than the normal addr64.
369 if (!isUInt<12>(AM.BaseOffs))
370 return false;
371
372 // FIXME: Since we can split immediate into soffset and immediate offset,
373 // would it make sense to allow any immediate?
374
375 switch (AM.Scale) {
376 case 0: // r + i or just i, depending on HasBaseReg.
377 return true;
378 case 1:
379 return true; // We have r + r or r + i.
380 case 2:
381 if (AM.HasBaseReg) {
382 // Reject 2 * r + r.
383 return false;
384 }
385
386 // Allow 2 * r as r + r
387 // Or 2 * r + i is allowed as r + r + i.
388 return true;
389 default: // Don't allow n * r
390 return false;
391 }
392}
393
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000394bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
395 const AddrMode &AM, Type *Ty,
396 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000397 // No global is ever allowed as a base.
398 if (AM.BaseGV)
399 return false;
400
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000401 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000402 case AMDGPUAS::GLOBAL_ADDRESS: {
Tom Stellard70580f82015-07-20 14:28:41 +0000403 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
404 // Assume the we will use FLAT for all global memory accesses
405 // on VI.
406 // FIXME: This assumption is currently wrong. On VI we still use
407 // MUBUF instructions for the r + i addressing mode. As currently
408 // implemented, the MUBUF instructions only work on buffer < 4GB.
409 // It may be possible to support > 4GB buffers with MUBUF instructions,
410 // by setting the stride value in the resource descriptor which would
411 // increase the size limit to (stride * 4GB). However, this is risky,
412 // because it has never been validated.
413 return isLegalFlatAddressingMode(AM);
414 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000415
Matt Arsenault711b3902015-08-07 20:18:34 +0000416 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000417 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000418 case AMDGPUAS::CONSTANT_ADDRESS: {
419 // If the offset isn't a multiple of 4, it probably isn't going to be
420 // correctly aligned.
421 if (AM.BaseOffs % 4 != 0)
422 return isLegalMUBUFAddressingMode(AM);
423
424 // There are no SMRD extloads, so if we have to do a small type access we
425 // will use a MUBUF load.
426 // FIXME?: We also need to do this if unaligned, but we don't know the
427 // alignment here.
428 if (DL.getTypeStoreSize(Ty) < 4)
429 return isLegalMUBUFAddressingMode(AM);
430
431 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
432 // SMRD instructions have an 8-bit, dword offset on SI.
433 if (!isUInt<8>(AM.BaseOffs / 4))
434 return false;
435 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) {
436 // On CI+, this can also be a 32-bit literal constant offset. If it fits
437 // in 8-bits, it can use a smaller encoding.
438 if (!isUInt<32>(AM.BaseOffs / 4))
439 return false;
440 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) {
441 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
442 if (!isUInt<20>(AM.BaseOffs))
443 return false;
444 } else
445 llvm_unreachable("unhandled generation");
446
447 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
448 return true;
449
450 if (AM.Scale == 1 && AM.HasBaseReg)
451 return true;
452
453 return false;
454 }
455
456 case AMDGPUAS::PRIVATE_ADDRESS:
457 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
458 return isLegalMUBUFAddressingMode(AM);
459
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000460 case AMDGPUAS::LOCAL_ADDRESS:
461 case AMDGPUAS::REGION_ADDRESS: {
462 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
463 // field.
464 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
465 // an 8-bit dword offset but we don't know the alignment here.
466 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000467 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000468
469 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
470 return true;
471
472 if (AM.Scale == 1 && AM.HasBaseReg)
473 return true;
474
Matt Arsenault5015a892014-08-15 17:17:07 +0000475 return false;
476 }
Tom Stellard70580f82015-07-20 14:28:41 +0000477 case AMDGPUAS::FLAT_ADDRESS:
478 return isLegalFlatAddressingMode(AM);
479
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000480 default:
481 llvm_unreachable("unhandled address space");
482 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000483}
484
Matt Arsenaulte6986632015-01-14 01:35:22 +0000485bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000486 unsigned AddrSpace,
487 unsigned Align,
488 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000489 if (IsFast)
490 *IsFast = false;
491
Matt Arsenault1018c892014-04-24 17:08:26 +0000492 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
493 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000494 if (!VT.isSimple() || VT == MVT::Other)
495 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000496
Tom Stellardc6b299c2015-02-02 18:02:28 +0000497 // TODO - CI+ supports unaligned memory accesses, but this requires driver
498 // support.
Matt Arsenault1018c892014-04-24 17:08:26 +0000499
Matt Arsenault1018c892014-04-24 17:08:26 +0000500 // XXX - The only mention I see of this in the ISA manual is for LDS direct
501 // reads the "byte address and must be dword aligned". Is it also true for the
502 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000503 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
504 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
505 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
506 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000507 bool AlignedBy4 = (Align % 4 == 0);
508 if (IsFast)
509 *IsFast = AlignedBy4;
510 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000511 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000512
Tom Stellard33e64c62015-02-04 20:49:52 +0000513 // Smaller than dword value must be aligned.
514 // FIXME: This should be allowed on CI+
515 if (VT.bitsLT(MVT::i32))
516 return false;
517
Matt Arsenault1018c892014-04-24 17:08:26 +0000518 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
519 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000520 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000521 if (IsFast)
522 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000523
524 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000525}
526
Matt Arsenault46645fa2014-07-28 17:49:26 +0000527EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
528 unsigned SrcAlign, bool IsMemset,
529 bool ZeroMemset,
530 bool MemcpyStrSrc,
531 MachineFunction &MF) const {
532 // FIXME: Should account for address space here.
533
534 // The default fallback uses the private pointer size as a guess for a type to
535 // use. Make sure we switch these to 64-bit accesses.
536
537 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
538 return MVT::v4i32;
539
540 if (Size >= 8 && DstAlign >= 4)
541 return MVT::v2i32;
542
543 // Use the default.
544 return MVT::Other;
545}
546
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000547static bool isFlatGlobalAddrSpace(unsigned AS) {
548 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
549 AS == AMDGPUAS::FLAT_ADDRESS ||
550 AS == AMDGPUAS::CONSTANT_ADDRESS;
551}
552
553bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
554 unsigned DestAS) const {
555 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
556}
557
Tom Stellarda6f24c62015-12-15 20:55:55 +0000558
559bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
560 const MemSDNode *MemNode = cast<MemSDNode>(N);
561 const Value *Ptr = MemNode->getMemOperand()->getValue();
562
563 // UndefValue means this is a load of a kernel input. These are uniform.
564 // Sometimes LDS instructions have constant pointers
565 if (isa<UndefValue>(Ptr) || isa<Argument>(Ptr) || isa<Constant>(Ptr) ||
566 isa<GlobalValue>(Ptr))
567 return true;
568
569 const Instruction *I = dyn_cast_or_null<Instruction>(Ptr);
570 return I && I->getMetadata("amdgpu.uniform");
571}
572
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000573TargetLoweringBase::LegalizeTypeAction
574SITargetLowering::getPreferredVectorAction(EVT VT) const {
575 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
576 return TypeSplitVector;
577
578 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000579}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000580
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000581bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
582 Type *Ty) const {
Eric Christopher7792e322015-01-30 23:24:40 +0000583 const SIInstrInfo *TII =
584 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000585 return TII->isInlineConstant(Imm);
586}
587
Tom Stellard2e045bb2016-01-20 00:13:22 +0000588bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
589
590 // SimplifySetCC uses this function to determine whether or not it should
591 // create setcc with i1 operands. We don't have instructions for i1 setcc.
592 if (VT == MVT::i1 && Op == ISD::SETCC)
593 return false;
594
595 return TargetLowering::isTypeDesirableForOp(Op, VT);
596}
597
Tom Stellardaf775432013-10-23 00:44:32 +0000598SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000599 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000600 unsigned Offset, bool Signed) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000601 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000602 MachineFunction &MF = DAG.getMachineFunction();
603 const SIRegisterInfo *TRI =
604 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
Matt Arsenaultac234b62015-11-30 21:15:57 +0000605 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000606
Matt Arsenault86033ca2014-07-28 17:31:39 +0000607 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
608
609 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000610 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000611 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000612 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
613 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
614 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
615 DAG.getConstant(Offset, SL, PtrVT));
Mehdi Amini44ede332015-07-09 02:09:04 +0000616 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000617 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
618
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000619 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000620
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000621 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000622 if (MemVT.isFloatingPoint())
623 ExtTy = ISD::EXTLOAD;
624
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000625 return DAG.getLoad(ISD::UNINDEXED, ExtTy,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000626 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
627 false, // isVolatile
628 true, // isNonTemporal
629 true, // isInvariant
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000630 Align); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000631}
632
Christian Konig2c8f6d52013-03-07 09:03:52 +0000633SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000634 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
635 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
636 SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardec2e43c2014-09-22 15:35:29 +0000637 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000638 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000639
640 MachineFunction &MF = DAG.getMachineFunction();
641 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000642 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000643 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000644
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000645 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) {
Matt Arsenaultd48da142015-11-02 23:23:02 +0000646 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000647 DiagnosticInfoUnsupported NoGraphicsHSA(
648 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000649 DAG.getContext()->diagnose(NoGraphicsHSA);
650 return SDValue();
651 }
652
Christian Konig2c8f6d52013-03-07 09:03:52 +0000653 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000654 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000655
656 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000657 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000658
659 // First check if it's a PS input addr
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000660 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000661 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000662
Marek Olsakfccabaf2016-01-13 11:45:36 +0000663 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000664 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000665 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000666 ++PSInputNum;
667 continue;
668 }
669
Marek Olsakfccabaf2016-01-13 11:45:36 +0000670 Info->markPSInputAllocated(PSInputNum);
671 if (Arg.Used)
672 Info->PSInputEna |= 1 << PSInputNum;
673
674 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000675 }
676
677 // Second split vertices into their elements
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000678 if (AMDGPU::isShader(CallConv) &&
679 Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000680 ISD::InputArg NewArg = Arg;
681 NewArg.Flags.setSplit();
682 NewArg.VT = Arg.VT.getVectorElementType();
683
684 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
685 // three or five element vertex only needs three or five registers,
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000686 // NOT four or eight.
Andrew Trick05938a52015-02-16 18:10:47 +0000687 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000688 unsigned NumElements = ParamType->getVectorNumElements();
689
690 for (unsigned j = 0; j != NumElements; ++j) {
691 Splits.push_back(NewArg);
692 NewArg.PartOffset += NewArg.VT.getStoreSize();
693 }
694
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000695 } else if (AMDGPU::isShader(CallConv)) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000696 Splits.push_back(Arg);
697 }
698 }
699
700 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000701 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
702 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000703
Christian Konig99ee0f42013-03-07 09:04:14 +0000704 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000705 //
706 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
707 // PSInputAddr, the user wants to enable some bits after the compilation
708 // based on run-time states. Since we can't know what the final PSInputEna
709 // will look like, so we shouldn't do anything here and the user should take
710 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000711 //
712 // Otherwise, the following restrictions apply:
713 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
714 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
715 // enabled too.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000716 if (CallConv == CallingConv::AMDGPU_PS &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000717 ((Info->getPSInputAddr() & 0x7F) == 0 ||
718 ((Info->getPSInputAddr() & 0xF) == 0 &&
719 Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000720 CCInfo.AllocateReg(AMDGPU::VGPR0);
721 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000722 Info->markPSInputAllocated(0);
723 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000724 }
725
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000726 if (!AMDGPU::isShader(CallConv)) {
Tom Stellardaf775432013-10-23 00:44:32 +0000727 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
728 Splits);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000729
730 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
731 } else {
732 assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() &&
733 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() &&
734 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
735 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
736 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
737 !Info->hasWorkItemIDZ());
Tom Stellardaf775432013-10-23 00:44:32 +0000738 }
739
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000740 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
741 if (Info->hasPrivateSegmentBuffer()) {
742 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
743 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
744 CCInfo.AllocateReg(PrivateSegmentBufferReg);
745 }
746
747 if (Info->hasDispatchPtr()) {
748 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
749 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
750 CCInfo.AllocateReg(DispatchPtrReg);
751 }
752
Matt Arsenault48ab5262016-04-25 19:27:18 +0000753 if (Info->hasQueuePtr()) {
754 unsigned QueuePtrReg = Info->addQueuePtr(*TRI);
755 MF.addLiveIn(QueuePtrReg, &AMDGPU::SReg_64RegClass);
756 CCInfo.AllocateReg(QueuePtrReg);
757 }
758
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000759 if (Info->hasKernargSegmentPtr()) {
760 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
761 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
762 CCInfo.AllocateReg(InputPtrReg);
763 }
764
Matt Arsenault296b8492016-02-12 06:31:30 +0000765 if (Info->hasFlatScratchInit()) {
766 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
767 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass);
768 CCInfo.AllocateReg(FlatScratchInitReg);
769 }
770
Christian Konig2c8f6d52013-03-07 09:03:52 +0000771 AnalyzeFormalArguments(CCInfo, Splits);
772
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000773 SmallVector<SDValue, 16> Chains;
774
Christian Konig2c8f6d52013-03-07 09:03:52 +0000775 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
776
Christian Konigb7be72d2013-05-17 09:46:48 +0000777 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000778 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000779 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000780 continue;
781 }
782
Christian Konig2c8f6d52013-03-07 09:03:52 +0000783 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000784 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000785
786 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000787 VT = Ins[i].VT;
788 EVT MemVT = Splits[i].VT;
Tom Stellardb5798b02015-06-26 21:15:03 +0000789 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
790 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000791 // The first 36 bytes of the input buffer contains information about
792 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000793 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000794 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000795 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000796
Craig Toppere3dcce92015-08-01 22:20:21 +0000797 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000798 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000799 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
800 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
801 // On SI local pointers are just offsets into LDS, so they are always
802 // less than 16-bits. On CI and newer they could potentially be
803 // real pointers, so we can't guarantee their size.
804 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
805 DAG.getValueType(MVT::i16));
806 }
807
Tom Stellarded882c22013-06-03 17:40:11 +0000808 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000809 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000810 continue;
811 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000812 assert(VA.isRegLoc() && "Parameter must be in a register!");
813
814 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000815
816 if (VT == MVT::i64) {
817 // For now assume it is a pointer
818 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
819 &AMDGPU::SReg_64RegClass);
820 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000821 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
822 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000823 continue;
824 }
825
826 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
827
828 Reg = MF.addLiveIn(Reg, RC);
829 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
830
Christian Konig2c8f6d52013-03-07 09:03:52 +0000831 if (Arg.VT.isVector()) {
832
833 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000834 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000835 unsigned NumElements = ParamType->getVectorNumElements();
836
837 SmallVector<SDValue, 4> Regs;
838 Regs.push_back(Val);
839 for (unsigned j = 1; j != NumElements; ++j) {
840 Reg = ArgLocs[ArgIdx++].getLocReg();
841 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000842
843 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
844 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000845 }
846
847 // Fill up the missing vector elements
848 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000849 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000850
Craig Topper48d114b2014-04-26 18:35:24 +0000851 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000852 continue;
853 }
854
855 InVals.push_back(Val);
856 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000857
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000858 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
859 // these from the dispatch pointer.
860
861 // Start adding system SGPRs.
862 if (Info->hasWorkGroupIDX()) {
863 unsigned Reg = Info->addWorkGroupIDX();
864 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
865 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000866 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000867
868 if (Info->hasWorkGroupIDY()) {
869 unsigned Reg = Info->addWorkGroupIDY();
870 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
871 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000872 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000873
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000874 if (Info->hasWorkGroupIDZ()) {
875 unsigned Reg = Info->addWorkGroupIDZ();
876 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
877 CCInfo.AllocateReg(Reg);
878 }
879
880 if (Info->hasWorkGroupInfo()) {
881 unsigned Reg = Info->addWorkGroupInfo();
882 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
883 CCInfo.AllocateReg(Reg);
884 }
885
886 if (Info->hasPrivateSegmentWaveByteOffset()) {
887 // Scratch wave offset passed in system SGPR.
Tom Stellardf110f8f2016-04-14 16:27:03 +0000888 unsigned PrivateSegmentWaveByteOffsetReg;
889
890 if (AMDGPU::isShader(CallConv)) {
891 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
892 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
893 } else
894 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset();
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000895
896 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
897 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
898 }
899
900 // Now that we've figured out where the scratch register inputs are, see if
901 // should reserve the arguments and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000902 bool HasStackObjects = MF.getFrameInfo()->hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +0000903 // Record that we know we have non-spill stack objects so we don't need to
904 // check all stack objects later.
905 if (HasStackObjects)
906 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000907
908 if (ST.isAmdHsaOS()) {
909 // TODO: Assume we will spill without optimizations.
910 if (HasStackObjects) {
911 // If we have stack objects, we unquestionably need the private buffer
912 // resource. For the HSA ABI, this will be the first 4 user SGPR
913 // inputs. We can reserve those and use them directly.
914
915 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
916 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
917 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
918
919 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
920 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
921 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
922 } else {
923 unsigned ReservedBufferReg
924 = TRI->reservedPrivateSegmentBufferReg(MF);
925 unsigned ReservedOffsetReg
926 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
927
928 // We tentatively reserve the last registers (skipping the last two
929 // which may contain VCC). After register allocation, we'll replace
930 // these with the ones immediately after those which were really
931 // allocated. In the prologue copies will be inserted from the argument
932 // to these reserved registers.
933 Info->setScratchRSrcReg(ReservedBufferReg);
934 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
935 }
936 } else {
937 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
938
939 // Without HSA, relocations are used for the scratch pointer and the
940 // buffer resource setup is always inserted in the prologue. Scratch wave
941 // offset is still in an input SGPR.
942 Info->setScratchRSrcReg(ReservedBufferReg);
943
944 if (HasStackObjects) {
945 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
946 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
947 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
948 } else {
949 unsigned ReservedOffsetReg
950 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
951 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
952 }
953 }
954
955 if (Info->hasWorkItemIDX()) {
956 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
957 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
958 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000959 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000960
961 if (Info->hasWorkItemIDY()) {
962 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
963 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
964 CCInfo.AllocateReg(Reg);
965 }
966
967 if (Info->hasWorkItemIDZ()) {
968 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
969 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
970 CCInfo.AllocateReg(Reg);
971 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000972
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000973 if (Chains.empty())
974 return Chain;
975
976 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000977}
978
Marek Olsak8a0f3352016-01-13 17:23:04 +0000979SDValue SITargetLowering::LowerReturn(SDValue Chain,
980 CallingConv::ID CallConv,
981 bool isVarArg,
982 const SmallVectorImpl<ISD::OutputArg> &Outs,
983 const SmallVectorImpl<SDValue> &OutVals,
984 SDLoc DL, SelectionDAG &DAG) const {
985 MachineFunction &MF = DAG.getMachineFunction();
986 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
987
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000988 if (!AMDGPU::isShader(CallConv))
Marek Olsak8a0f3352016-01-13 17:23:04 +0000989 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
990 OutVals, DL, DAG);
991
Marek Olsak8e9cc632016-01-13 17:23:09 +0000992 Info->setIfReturnsVoid(Outs.size() == 0);
993
Marek Olsak8a0f3352016-01-13 17:23:04 +0000994 SmallVector<ISD::OutputArg, 48> Splits;
995 SmallVector<SDValue, 48> SplitVals;
996
997 // Split vectors into their elements.
998 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
999 const ISD::OutputArg &Out = Outs[i];
1000
1001 if (Out.VT.isVector()) {
1002 MVT VT = Out.VT.getVectorElementType();
1003 ISD::OutputArg NewOut = Out;
1004 NewOut.Flags.setSplit();
1005 NewOut.VT = VT;
1006
1007 // We want the original number of vector elements here, e.g.
1008 // three or five, not four or eight.
1009 unsigned NumElements = Out.ArgVT.getVectorNumElements();
1010
1011 for (unsigned j = 0; j != NumElements; ++j) {
1012 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
1013 DAG.getConstant(j, DL, MVT::i32));
1014 SplitVals.push_back(Elem);
1015 Splits.push_back(NewOut);
1016 NewOut.PartOffset += NewOut.VT.getStoreSize();
1017 }
1018 } else {
1019 SplitVals.push_back(OutVals[i]);
1020 Splits.push_back(Out);
1021 }
1022 }
1023
1024 // CCValAssign - represent the assignment of the return value to a location.
1025 SmallVector<CCValAssign, 48> RVLocs;
1026
1027 // CCState - Info about the registers and stack slots.
1028 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1029 *DAG.getContext());
1030
1031 // Analyze outgoing return values.
1032 AnalyzeReturn(CCInfo, Splits);
1033
1034 SDValue Flag;
1035 SmallVector<SDValue, 48> RetOps;
1036 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1037
1038 // Copy the result values into the output registers.
1039 for (unsigned i = 0, realRVLocIdx = 0;
1040 i != RVLocs.size();
1041 ++i, ++realRVLocIdx) {
1042 CCValAssign &VA = RVLocs[i];
1043 assert(VA.isRegLoc() && "Can only return in registers!");
1044
1045 SDValue Arg = SplitVals[realRVLocIdx];
1046
1047 // Copied from other backends.
1048 switch (VA.getLocInfo()) {
1049 default: llvm_unreachable("Unknown loc info!");
1050 case CCValAssign::Full:
1051 break;
1052 case CCValAssign::BCvt:
1053 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1054 break;
1055 }
1056
1057 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
1058 Flag = Chain.getValue(1);
1059 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1060 }
1061
1062 // Update chain and glue.
1063 RetOps[0] = Chain;
1064 if (Flag.getNode())
1065 RetOps.push_back(Flag);
1066
1067 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, RetOps);
1068}
1069
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001070unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1071 SelectionDAG &DAG) const {
1072 unsigned Reg = StringSwitch<unsigned>(RegName)
1073 .Case("m0", AMDGPU::M0)
1074 .Case("exec", AMDGPU::EXEC)
1075 .Case("exec_lo", AMDGPU::EXEC_LO)
1076 .Case("exec_hi", AMDGPU::EXEC_HI)
1077 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1078 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1079 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1080 .Default(AMDGPU::NoRegister);
1081
1082 if (Reg == AMDGPU::NoRegister) {
1083 report_fatal_error(Twine("invalid register name \""
1084 + StringRef(RegName) + "\"."));
1085
1086 }
1087
1088 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
1089 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1090 report_fatal_error(Twine("invalid register \""
1091 + StringRef(RegName) + "\" for subtarget."));
1092 }
1093
1094 switch (Reg) {
1095 case AMDGPU::M0:
1096 case AMDGPU::EXEC_LO:
1097 case AMDGPU::EXEC_HI:
1098 case AMDGPU::FLAT_SCR_LO:
1099 case AMDGPU::FLAT_SCR_HI:
1100 if (VT.getSizeInBits() == 32)
1101 return Reg;
1102 break;
1103 case AMDGPU::EXEC:
1104 case AMDGPU::FLAT_SCR:
1105 if (VT.getSizeInBits() == 64)
1106 return Reg;
1107 break;
1108 default:
1109 llvm_unreachable("missing register type checking");
1110 }
1111
1112 report_fatal_error(Twine("invalid type for register \""
1113 + StringRef(RegName) + "\"."));
1114}
1115
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001116MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
1117 MachineInstr *MI, MachineBasicBlock *BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +00001118 switch (MI->getOpcode()) {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001119 case AMDGPU::SI_INIT_M0: {
1120 const SIInstrInfo *TII =
1121 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
1122 BuildMI(*BB, MI->getIterator(), MI->getDebugLoc(),
1123 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1124 .addOperand(MI->getOperand(0));
1125 MI->eraseFromParent();
1126 break;
1127 }
Matt Arsenault20711b72015-02-20 22:10:45 +00001128 case AMDGPU::BRANCH:
1129 return BB;
Changpeng Fang01f60622016-03-15 17:28:44 +00001130 case AMDGPU::GET_GROUPSTATICSIZE: {
1131 const SIInstrInfo *TII =
1132 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
1133 MachineFunction *MF = BB->getParent();
1134 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1135 DebugLoc DL = MI->getDebugLoc();
1136 BuildMI (*BB, MI, DL, TII->get(AMDGPU::S_MOVK_I32))
1137 .addOperand(MI->getOperand(0))
1138 .addImm(MFI->LDSSize);
1139 MI->eraseFromParent();
1140 return BB;
1141 }
1142 default:
1143 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001144 }
1145 return BB;
1146}
1147
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001148bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1149 // This currently forces unfolding various combinations of fsub into fma with
1150 // free fneg'd operands. As long as we have fast FMA (controlled by
1151 // isFMAFasterThanFMulAndFAdd), we should perform these.
1152
1153 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1154 // most of these combines appear to be cycle neutral but save on instruction
1155 // count / code size.
1156 return true;
1157}
1158
Mehdi Amini44ede332015-07-09 02:09:04 +00001159EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1160 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001161 if (!VT.isVector()) {
1162 return MVT::i1;
1163 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001164 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001165}
1166
Mehdi Aminieaabc512015-07-09 15:12:23 +00001167MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001168 return MVT::i32;
1169}
1170
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001171// Answering this is somewhat tricky and depends on the specific device which
1172// have different rates for fma or all f64 operations.
1173//
1174// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1175// regardless of which device (although the number of cycles differs between
1176// devices), so it is always profitable for f64.
1177//
1178// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1179// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1180// which we can always do even without fused FP ops since it returns the same
1181// result as the separate operations and since it is always full
1182// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1183// however does not support denormals, so we do report fma as faster if we have
1184// a fast fma device and require denormals.
1185//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001186bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1187 VT = VT.getScalarType();
1188
1189 if (!VT.isSimple())
1190 return false;
1191
1192 switch (VT.getSimpleVT().SimpleTy) {
1193 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001194 // This is as fast on some subtargets. However, we always have full rate f32
1195 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001196 // which we should prefer over fma. We can't use this if we want to support
1197 // denormals, so only report this in these cases.
1198 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001199 case MVT::f64:
1200 return true;
1201 default:
1202 break;
1203 }
1204
1205 return false;
1206}
1207
Tom Stellard75aadc22012-12-11 21:25:42 +00001208//===----------------------------------------------------------------------===//
1209// Custom DAG Lowering Operations
1210//===----------------------------------------------------------------------===//
1211
1212SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1213 switch (Op.getOpcode()) {
1214 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001215 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001216 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001217 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001218 SDValue Result = LowerLOAD(Op, DAG);
1219 assert((!Result.getNode() ||
1220 Result.getNode()->getNumValues() == 2) &&
1221 "Load should return a value and a chain");
1222 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001223 }
Tom Stellardaf775432013-10-23 00:44:32 +00001224
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001225 case ISD::FSIN:
1226 case ISD::FCOS:
1227 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001228 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001229 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001230 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001231 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001232 case ISD::GlobalAddress: {
1233 MachineFunction &MF = DAG.getMachineFunction();
1234 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1235 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001236 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001237 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001238 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001239 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Matt Arsenault99c14522016-04-25 19:27:24 +00001240 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001241 }
1242 return SDValue();
1243}
1244
Tom Stellardf8794352012-12-19 22:10:31 +00001245/// \brief Helper function for LowerBRCOND
1246static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001247
Tom Stellardf8794352012-12-19 22:10:31 +00001248 SDNode *Parent = Value.getNode();
1249 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1250 I != E; ++I) {
1251
1252 if (I.getUse().get() != Value)
1253 continue;
1254
1255 if (I->getOpcode() == Opcode)
1256 return *I;
1257 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001258 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001259}
1260
Tom Stellardb02094e2014-07-21 15:45:01 +00001261SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
1262
Tom Stellardc98ee202015-07-16 19:40:07 +00001263 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +00001264 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
1265 unsigned FrameIndex = FINode->getIndex();
1266
Matt Arsenault3a619852016-02-27 20:26:57 +00001267 // A FrameIndex node represents a 32-bit offset into scratch memory. If the
1268 // high bit of a frame index offset were to be set, this would mean that it
1269 // represented an offset of ~2GB * 64 = ~128GB from the start of the scratch
1270 // buffer, with 64 being the number of threads per wave.
Tom Stellardc98ee202015-07-16 19:40:07 +00001271 //
Matt Arsenault3a619852016-02-27 20:26:57 +00001272 // The maximum private allocation for the entire GPU is 4G, and we are
1273 // concerned with the largest the index could ever be for an individual
1274 // workitem. This will occur with the minmum dispatch size. If a program
1275 // requires more, the dispatch size will be reduced.
1276 //
1277 // With this limit, we can mark the high bit of the FrameIndex node as known
1278 // zero, which is important, because it means in most situations we can prove
1279 // that values derived from FrameIndex nodes are non-negative. This enables us
1280 // to take advantage of more addressing modes when accessing scratch buffers,
1281 // since for scratch reads/writes, the register offset must always be
1282 // positive.
1283
1284 uint64_t MaxGPUAlloc = UINT64_C(4) * 1024 * 1024 * 1024;
1285
1286 // XXX - It is unclear if partial dispatch works. Assume it works at half wave
1287 // granularity. It is probably a full wave.
1288 uint64_t MinGranularity = 32;
1289
1290 unsigned KnownBits = Log2_64(MaxGPUAlloc / MinGranularity);
1291 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), KnownBits);
Tom Stellardc98ee202015-07-16 19:40:07 +00001292
1293 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
Tom Stellardc98ee202015-07-16 19:40:07 +00001294 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
Matt Arsenault3a619852016-02-27 20:26:57 +00001295 DAG.getValueType(ExtVT));
Tom Stellardb02094e2014-07-21 15:45:01 +00001296}
1297
Tom Stellardbc4497b2016-02-12 23:45:29 +00001298bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault16f48d72016-02-13 00:36:10 +00001299 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN)
Tom Stellardbc4497b2016-02-12 23:45:29 +00001300 return false;
1301
1302 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1303 default: return false;
1304 case AMDGPUIntrinsic::amdgcn_if:
1305 case AMDGPUIntrinsic::amdgcn_else:
1306 case AMDGPUIntrinsic::amdgcn_break:
1307 case AMDGPUIntrinsic::amdgcn_if_break:
1308 case AMDGPUIntrinsic::amdgcn_else_break:
1309 case AMDGPUIntrinsic::amdgcn_loop:
1310 case AMDGPUIntrinsic::amdgcn_end_cf:
1311 return true;
1312 }
1313}
1314
Tom Stellardf8794352012-12-19 22:10:31 +00001315/// This transforms the control flow intrinsics to get the branch destination as
1316/// last parameter, also switches branch target with BR if the need arise
1317SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1318 SelectionDAG &DAG) const {
1319
Andrew Trickef9de2a2013-05-25 02:42:55 +00001320 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001321
1322 SDNode *Intr = BRCOND.getOperand(1).getNode();
1323 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001324 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001325 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001326
1327 if (Intr->getOpcode() == ISD::SETCC) {
1328 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001329 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001330 Intr = SetCC->getOperand(0).getNode();
1331
1332 } else {
1333 // Get the target from BR if we don't negate the condition
1334 BR = findUser(BRCOND, ISD::BR);
1335 Target = BR->getOperand(1);
1336 }
1337
Tom Stellardbc4497b2016-02-12 23:45:29 +00001338 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN) {
1339 // This is a uniform branch so we don't need to legalize.
1340 return BRCOND;
1341 }
1342
1343 assert(!SetCC ||
1344 (SetCC->getConstantOperandVal(1) == 1 &&
1345 isCFIntrinsic(Intr) &&
1346 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1347 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001348
1349 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001350 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001351
1352 // operands of the new intrinsic call
1353 SmallVector<SDValue, 4> Ops;
1354 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001355 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001356 Ops.push_back(Target);
1357
1358 // build the new intrinsic call
1359 SDNode *Result = DAG.getNode(
1360 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001361 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001362
1363 if (BR) {
1364 // Give the branch instruction our target
1365 SDValue Ops[] = {
1366 BR->getOperand(0),
1367 BRCOND.getOperand(2)
1368 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001369 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1370 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1371 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001372 }
1373
1374 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1375
1376 // Copy the intrinsic results to registers
1377 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1378 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1379 if (!CopyToReg)
1380 continue;
1381
1382 Chain = DAG.getCopyToReg(
1383 Chain, DL,
1384 CopyToReg->getOperand(1),
1385 SDValue(Result, i - 1),
1386 SDValue());
1387
1388 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1389 }
1390
1391 // Remove the old intrinsic from the chain
1392 DAG.ReplaceAllUsesOfValueWith(
1393 SDValue(Intr, Intr->getNumValues() - 1),
1394 Intr->getOperand(0));
1395
1396 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001397}
1398
Matt Arsenault99c14522016-04-25 19:27:24 +00001399SDValue SITargetLowering::getSegmentAperture(unsigned AS,
1400 SelectionDAG &DAG) const {
1401 SDLoc SL;
1402 MachineFunction &MF = DAG.getMachineFunction();
1403 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1404 SDValue QueuePtr = CreateLiveInRegister(
1405 DAG, &AMDGPU::SReg_64RegClass, Info->getQueuePtrUserSGPR(), MVT::i64);
1406
1407 // Offset into amd_queue_t for group_segment_aperture_base_hi /
1408 // private_segment_aperture_base_hi.
1409 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
1410
1411 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr,
1412 DAG.getConstant(StructOffset, SL, MVT::i64));
1413
1414 // TODO: Use custom target PseudoSourceValue.
1415 // TODO: We should use the value from the IR intrinsic call, but it might not
1416 // be available and how do we get it?
1417 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()),
1418 AMDGPUAS::CONSTANT_ADDRESS));
1419
1420 MachinePointerInfo PtrInfo(V, StructOffset);
1421 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr,
1422 PtrInfo, false,
1423 false, true,
1424 MinAlign(64, StructOffset));
1425}
1426
1427SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
1428 SelectionDAG &DAG) const {
1429 SDLoc SL(Op);
1430 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op);
1431
1432 SDValue Src = ASC->getOperand(0);
1433
1434 // FIXME: Really support non-0 null pointers.
1435 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32);
1436 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
1437
1438 // flat -> local/private
1439 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1440 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1441 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1442 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
1443 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
1444
1445 return DAG.getNode(ISD::SELECT, SL, MVT::i32,
1446 NonNull, Ptr, SegmentNullPtr);
1447 }
1448 }
1449
1450 // local/private -> flat
1451 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
1452 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1453 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1454 SDValue NonNull
1455 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
1456
1457 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG);
1458 SDValue CvtPtr
1459 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
1460
1461 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull,
1462 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr),
1463 FlatNullPtr);
1464 }
1465 }
1466
1467 // global <-> flat are no-ops and never emitted.
1468
1469 const MachineFunction &MF = DAG.getMachineFunction();
1470 DiagnosticInfoUnsupported InvalidAddrSpaceCast(
1471 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc());
1472 DAG.getContext()->diagnose(InvalidAddrSpaceCast);
1473
1474 return DAG.getUNDEF(ASC->getValueType(0));
1475}
1476
Tom Stellard067c8152014-07-21 14:01:14 +00001477SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1478 SDValue Op,
1479 SelectionDAG &DAG) const {
1480 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1481
1482 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
1483 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1484
1485 SDLoc DL(GSD);
1486 const GlobalValue *GV = GSD->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001487 MVT PtrVT = getPointerTy(DAG.getDataLayout(), GSD->getAddressSpace());
Tom Stellard067c8152014-07-21 14:01:14 +00001488
Tom Stellard067c8152014-07-21 14:01:14 +00001489 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
Tom Stellardc93fc112015-12-10 02:13:01 +00001490 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT, GA);
Tom Stellard067c8152014-07-21 14:01:14 +00001491}
1492
Tom Stellardfc92e772015-05-12 14:18:14 +00001493SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL,
1494 SDValue V) const {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001495 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
1496 // the destination register.
1497 //
Tom Stellardfc92e772015-05-12 14:18:14 +00001498 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1499 // so we will end up with redundant moves to m0.
1500 //
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001501 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
1502
1503 // A Null SDValue creates a glue result.
1504 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
1505 V, Chain);
1506 return SDValue(M0, 0);
Tom Stellardfc92e772015-05-12 14:18:14 +00001507}
1508
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001509SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1510 SDValue Op,
1511 MVT VT,
1512 unsigned Offset) const {
1513 SDLoc SL(Op);
1514 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1515 DAG.getEntryNode(), Offset, false);
1516 // The local size values will have the hi 16-bits as zero.
1517 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1518 DAG.getValueType(VT));
1519}
1520
Matt Arsenaulte0132462016-01-30 05:19:45 +00001521static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, EVT VT) {
1522 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
1523 "non-hsa intrinsic with hsa target");
1524 DAG.getContext()->diagnose(BadIntrin);
1525 return DAG.getUNDEF(VT);
1526}
1527
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001528SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1529 SelectionDAG &DAG) const {
1530 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001531 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardec2e43c2014-09-22 15:35:29 +00001532 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +00001533 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001534
1535 EVT VT = Op.getValueType();
1536 SDLoc DL(Op);
1537 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1538
Sanjay Patela2607012015-09-16 16:31:21 +00001539 // TODO: Should this propagate fast-math-flags?
1540
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001541 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001542 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault48ab5262016-04-25 19:27:18 +00001543 case Intrinsic::amdgcn_queue_ptr: {
Matt Arsenault800fecf2016-01-11 21:18:33 +00001544 if (!Subtarget->isAmdHsaOS()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00001545 DiagnosticInfoUnsupported BadIntrin(
1546 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
1547 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00001548 DAG.getContext()->diagnose(BadIntrin);
1549 return DAG.getUNDEF(VT);
1550 }
1551
Matt Arsenault48ab5262016-04-25 19:27:18 +00001552 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
1553 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00001554 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
Matt Arsenault48ab5262016-04-25 19:27:18 +00001555 TRI->getPreloadedValue(MF, Reg), VT);
1556 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001557 case Intrinsic::amdgcn_rcp:
1558 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
1559 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001560 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001561 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenault79963e82016-02-13 01:03:00 +00001562 case Intrinsic::amdgcn_rsq_clamp:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001563 case AMDGPUIntrinsic::AMDGPU_rsq_clamped: { // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001564 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00001565 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00001566
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001567 Type *Type = VT.getTypeForEVT(*DAG.getContext());
1568 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
1569 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
1570
1571 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
1572 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
1573 DAG.getConstantFP(Max, DL, VT));
1574 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
1575 DAG.getConstantFP(Min, DL, VT));
1576 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001577 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001578 if (Subtarget->isAmdHsaOS())
1579 return emitNonHSAIntrinsicError(DAG, VT);
1580
Tom Stellardec2e43c2014-09-22 15:35:29 +00001581 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1582 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001583 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001584 if (Subtarget->isAmdHsaOS())
1585 return emitNonHSAIntrinsicError(DAG, VT);
1586
Tom Stellardec2e43c2014-09-22 15:35:29 +00001587 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1588 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001589 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001590 if (Subtarget->isAmdHsaOS())
1591 return emitNonHSAIntrinsicError(DAG, VT);
1592
Tom Stellardec2e43c2014-09-22 15:35:29 +00001593 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1594 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001595 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001596 if (Subtarget->isAmdHsaOS())
1597 return emitNonHSAIntrinsicError(DAG, VT);
1598
Tom Stellardec2e43c2014-09-22 15:35:29 +00001599 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1600 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001601 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001602 if (Subtarget->isAmdHsaOS())
1603 return emitNonHSAIntrinsicError(DAG, VT);
1604
Tom Stellardec2e43c2014-09-22 15:35:29 +00001605 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1606 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001607 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001608 if (Subtarget->isAmdHsaOS())
1609 return emitNonHSAIntrinsicError(DAG, VT);
1610
Tom Stellardec2e43c2014-09-22 15:35:29 +00001611 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1612 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001613 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001614 if (Subtarget->isAmdHsaOS())
1615 return emitNonHSAIntrinsicError(DAG, VT);
1616
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001617 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1618 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001619 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001620 if (Subtarget->isAmdHsaOS())
1621 return emitNonHSAIntrinsicError(DAG, VT);
1622
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001623 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1624 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001625 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001626 if (Subtarget->isAmdHsaOS())
1627 return emitNonHSAIntrinsicError(DAG, VT);
1628
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001629 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1630 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenaultbef34e22016-01-22 21:30:34 +00001631 case Intrinsic::amdgcn_read_workdim:
1632 case AMDGPUIntrinsic::AMDGPU_read_workdim: // Legacy name.
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001633 // Really only 2 bits.
1634 return lowerImplicitZextParam(DAG, Op, MVT::i8,
1635 getImplicitParameterOffset(MFI, GRID_DIM));
Matt Arsenault43976df2016-01-30 04:25:19 +00001636 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001637 case Intrinsic::r600_read_tgid_x:
1638 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001639 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001640 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001641 case Intrinsic::r600_read_tgid_y:
1642 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001643 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001644 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001645 case Intrinsic::r600_read_tgid_z:
1646 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001647 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001648 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001649 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001650 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001651 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001652 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001653 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001654 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001655 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001656 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001657 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001658 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001659 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001660 case AMDGPUIntrinsic::SI_load_const: {
1661 SDValue Ops[] = {
1662 Op.getOperand(1),
1663 Op.getOperand(2)
1664 };
1665
1666 MachineMemOperand *MMO = MF.getMachineMemOperand(
1667 MachinePointerInfo(),
1668 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
1669 VT.getStoreSize(), 4);
1670 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
1671 Op->getVTList(), Ops, VT, MMO);
1672 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001673 case AMDGPUIntrinsic::SI_vs_load_input:
1674 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1675 Op.getOperand(1),
1676 Op.getOperand(2),
1677 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00001678
Tom Stellard2a9d9472015-05-12 15:00:46 +00001679 case AMDGPUIntrinsic::SI_fs_constant: {
1680 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1681 SDValue Glue = M0.getValue(1);
1682 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
1683 DAG.getConstant(2, DL, MVT::i32), // P0
1684 Op.getOperand(1), Op.getOperand(2), Glue);
1685 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00001686 case AMDGPUIntrinsic::SI_packf16:
1687 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
1688 return DAG.getUNDEF(MVT::i32);
1689 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00001690 case AMDGPUIntrinsic::SI_fs_interp: {
1691 SDValue IJ = Op.getOperand(4);
1692 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1693 DAG.getConstant(0, DL, MVT::i32));
1694 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1695 DAG.getConstant(1, DL, MVT::i32));
1696 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1697 SDValue Glue = M0.getValue(1);
1698 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
1699 DAG.getVTList(MVT::f32, MVT::Glue),
1700 I, Op.getOperand(1), Op.getOperand(2), Glue);
1701 Glue = SDValue(P1.getNode(), 1);
1702 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
1703 Op.getOperand(1), Op.getOperand(2), Glue);
1704 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00001705 case Intrinsic::amdgcn_interp_p1: {
1706 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
1707 SDValue Glue = M0.getValue(1);
1708 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
1709 Op.getOperand(2), Op.getOperand(3), Glue);
1710 }
1711 case Intrinsic::amdgcn_interp_p2: {
1712 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
1713 SDValue Glue = SDValue(M0.getNode(), 1);
1714 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
1715 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
1716 Glue);
1717 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00001718 case Intrinsic::amdgcn_sin:
1719 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
1720
1721 case Intrinsic::amdgcn_cos:
1722 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
1723
1724 case Intrinsic::amdgcn_log_clamp: {
1725 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
1726 return SDValue();
1727
1728 DiagnosticInfoUnsupported BadIntrin(
1729 *MF.getFunction(), "intrinsic not supported on subtarget",
1730 DL.getDebugLoc());
1731 DAG.getContext()->diagnose(BadIntrin);
1732 return DAG.getUNDEF(VT);
1733 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001734 case Intrinsic::amdgcn_ldexp:
1735 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
1736 Op.getOperand(1), Op.getOperand(2));
1737 case Intrinsic::amdgcn_class:
1738 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
1739 Op.getOperand(1), Op.getOperand(2));
1740 case Intrinsic::amdgcn_div_fmas:
1741 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
1742 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
1743 Op.getOperand(4));
1744
1745 case Intrinsic::amdgcn_div_fixup:
1746 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
1747 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
1748
1749 case Intrinsic::amdgcn_trig_preop:
1750 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
1751 Op.getOperand(1), Op.getOperand(2));
1752 case Intrinsic::amdgcn_div_scale: {
1753 // 3rd parameter required to be a constant.
1754 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1755 if (!Param)
1756 return DAG.getUNDEF(VT);
1757
1758 // Translate to the operands expected by the machine instruction. The
1759 // first parameter must be the same as the first instruction.
1760 SDValue Numerator = Op.getOperand(1);
1761 SDValue Denominator = Op.getOperand(2);
1762
1763 // Note this order is opposite of the machine instruction's operations,
1764 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
1765 // intrinsic has the numerator as the first operand to match a normal
1766 // division operation.
1767
1768 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
1769
1770 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
1771 Denominator, Numerator);
1772 }
1773 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte0:
1774 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Op.getOperand(1));
1775 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte1:
1776 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE1, DL, VT, Op.getOperand(1));
1777 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte2:
1778 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE2, DL, VT, Op.getOperand(1));
1779 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte3:
1780 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE3, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001781 default:
1782 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1783 }
1784}
1785
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001786SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
1787 SelectionDAG &DAG) const {
1788 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1789 switch (IntrID) {
1790 case Intrinsic::amdgcn_atomic_inc:
1791 case Intrinsic::amdgcn_atomic_dec: {
1792 MemSDNode *M = cast<MemSDNode>(Op);
1793 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
1794 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
1795 SDValue Ops[] = {
1796 M->getOperand(0), // Chain
1797 M->getOperand(2), // Ptr
1798 M->getOperand(3) // Value
1799 };
1800
1801 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
1802 M->getMemoryVT(), M->getMemOperand());
1803 }
1804 default:
1805 return SDValue();
1806 }
1807}
1808
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001809SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1810 SelectionDAG &DAG) const {
1811 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00001812 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001813 SDValue Chain = Op.getOperand(0);
1814 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1815
1816 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00001817 case AMDGPUIntrinsic::SI_sendmsg: {
1818 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1819 SDValue Glue = Chain.getValue(1);
1820 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1821 Op.getOperand(2), Glue);
1822 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001823 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001824 SDValue Ops[] = {
1825 Chain,
1826 Op.getOperand(2),
1827 Op.getOperand(3),
1828 Op.getOperand(4),
1829 Op.getOperand(5),
1830 Op.getOperand(6),
1831 Op.getOperand(7),
1832 Op.getOperand(8),
1833 Op.getOperand(9),
1834 Op.getOperand(10),
1835 Op.getOperand(11),
1836 Op.getOperand(12),
1837 Op.getOperand(13),
1838 Op.getOperand(14)
1839 };
1840
1841 EVT VT = Op.getOperand(3).getValueType();
1842
1843 MachineMemOperand *MMO = MF.getMachineMemOperand(
1844 MachinePointerInfo(),
1845 MachineMemOperand::MOStore,
1846 VT.getStoreSize(), 4);
1847 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1848 Op->getVTList(), Ops, VT, MMO);
1849 }
1850 default:
1851 return SDValue();
1852 }
1853}
1854
Tom Stellard81d871d2013-11-13 23:36:50 +00001855SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1856 SDLoc DL(Op);
1857 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001858 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00001859 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00001860
Matt Arsenaulta1436412016-02-10 18:21:45 +00001861 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
1862 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected");
Matt Arsenault6dfda962016-02-10 18:21:39 +00001863 // FIXME: Copied from PPC
1864 // First, load into 32 bits, then truncate to 1 bit.
1865
1866 SDValue Chain = Load->getChain();
1867 SDValue BasePtr = Load->getBasePtr();
1868 MachineMemOperand *MMO = Load->getMemOperand();
1869
1870 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
1871 BasePtr, MVT::i8, MMO);
1872
1873 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00001874 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00001875 NewLD.getValue(1)
1876 };
1877
1878 return DAG.getMergeValues(Ops, DL);
1879 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001880
Matt Arsenaulta1436412016-02-10 18:21:45 +00001881 if (!MemVT.isVector())
1882 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001883
Matt Arsenaulta1436412016-02-10 18:21:45 +00001884 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1885 "Custom lowering for non-i32 vectors hasn't been implemented.");
1886 unsigned NumElements = MemVT.getVectorNumElements();
1887 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001888
Matt Arsenaulta1436412016-02-10 18:21:45 +00001889 switch (Load->getAddressSpace()) {
1890 case AMDGPUAS::CONSTANT_ADDRESS:
1891 if (isMemOpUniform(Load))
1892 return SDValue();
1893 // Non-uniform loads will be selected to MUBUF instructions, so they
1894 // have the same legalization requires ments as global and private
1895 // loads.
1896 //
1897 // Fall-through
1898 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001899 case AMDGPUAS::FLAT_ADDRESS:
1900 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00001901 return SplitVectorLoad(Op, DAG);
1902 // v4 loads are supported for private and global memory.
1903 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001904 case AMDGPUAS::PRIVATE_ADDRESS: {
1905 // Depending on the setting of the private_element_size field in the
1906 // resource descriptor, we can only make private accesses up to a certain
1907 // size.
1908 switch (Subtarget->getMaxPrivateElementSize()) {
1909 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00001910 return scalarizeVectorLoad(Load, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001911 case 8:
1912 if (NumElements > 2)
1913 return SplitVectorLoad(Op, DAG);
1914 return SDValue();
1915 case 16:
1916 // Same as global/flat
1917 if (NumElements > 4)
1918 return SplitVectorLoad(Op, DAG);
1919 return SDValue();
1920 default:
1921 llvm_unreachable("unsupported private_element_size");
1922 }
1923 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00001924 case AMDGPUAS::LOCAL_ADDRESS:
1925 // If properly aligned, if we split we might be able to use ds_read_b64.
1926 return SplitVectorLoad(Op, DAG);
1927 default:
1928 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00001929 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001930}
1931
Tom Stellard0ec134f2014-02-04 17:18:40 +00001932SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1933 if (Op.getValueType() != MVT::i64)
1934 return SDValue();
1935
1936 SDLoc DL(Op);
1937 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001938
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001939 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1940 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001941
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001942 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1943 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1944
1945 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1946 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001947
1948 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1949
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001950 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1951 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001952
1953 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1954
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001955 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1956 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001957}
1958
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001959// Catch division cases where we can use shortcuts with rcp and rsq
1960// instructions.
1961SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001962 SDLoc SL(Op);
1963 SDValue LHS = Op.getOperand(0);
1964 SDValue RHS = Op.getOperand(1);
1965 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001966 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001967
1968 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001969 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1970 CLHS->isExactlyValue(1.0)) {
1971 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1972 // the CI documentation has a worst case error of 1 ulp.
1973 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1974 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001975
1976 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001977 //
1978 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1979 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001980 if (RHS.getOpcode() == ISD::FSQRT)
1981 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1982
1983 // 1.0 / x -> rcp(x)
1984 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1985 }
1986 }
1987
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001988 if (Unsafe) {
1989 // Turn into multiply by the reciprocal.
1990 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00001991 SDNodeFlags Flags;
1992 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001993 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00001994 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001995 }
1996
1997 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001998}
1999
2000SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00002001 if (SDValue FastLowered = LowerFastFDIV(Op, DAG))
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002002 return FastLowered;
2003
2004 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
2005 // selection error for now rather than do something incorrect.
2006 if (Subtarget->hasFP32Denormals())
2007 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002008
2009 SDLoc SL(Op);
2010 SDValue LHS = Op.getOperand(0);
2011 SDValue RHS = Op.getOperand(1);
2012
2013 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
2014
2015 const APFloat K0Val(BitsToFloat(0x6f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002016 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002017
2018 const APFloat K1Val(BitsToFloat(0x2f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002019 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002020
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002021 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002022
Mehdi Amini44ede332015-07-09 02:09:04 +00002023 EVT SetCCVT =
2024 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002025
2026 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
2027
2028 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
2029
Sanjay Patela2607012015-09-16 16:31:21 +00002030 // TODO: Should this propagate fast-math-flags?
2031
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002032 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
2033
2034 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
2035
2036 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
2037
2038 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
2039}
2040
2041SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002042 if (DAG.getTarget().Options.UnsafeFPMath)
2043 return LowerFastFDIV(Op, DAG);
2044
2045 SDLoc SL(Op);
2046 SDValue X = Op.getOperand(0);
2047 SDValue Y = Op.getOperand(1);
2048
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002049 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002050
2051 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
2052
2053 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
2054
2055 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
2056
2057 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
2058
2059 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
2060
2061 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
2062
2063 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
2064
2065 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
2066
2067 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
2068 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
2069
2070 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
2071 NegDivScale0, Mul, DivScale1);
2072
2073 SDValue Scale;
2074
2075 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
2076 // Workaround a hardware bug on SI where the condition output from div_scale
2077 // is not usable.
2078
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002079 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002080
2081 // Figure out if the scale to use for div_fmas.
2082 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
2083 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
2084 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
2085 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
2086
2087 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
2088 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
2089
2090 SDValue Scale0Hi
2091 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
2092 SDValue Scale1Hi
2093 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
2094
2095 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
2096 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
2097 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
2098 } else {
2099 Scale = DivScale1.getValue(1);
2100 }
2101
2102 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
2103 Fma4, Fma3, Mul, Scale);
2104
2105 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002106}
2107
2108SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
2109 EVT VT = Op.getValueType();
2110
2111 if (VT == MVT::f32)
2112 return LowerFDIV32(Op, DAG);
2113
2114 if (VT == MVT::f64)
2115 return LowerFDIV64(Op, DAG);
2116
2117 llvm_unreachable("Unexpected type for fdiv");
2118}
2119
Tom Stellard81d871d2013-11-13 23:36:50 +00002120SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2121 SDLoc DL(Op);
2122 StoreSDNode *Store = cast<StoreSDNode>(Op);
2123 EVT VT = Store->getMemoryVT();
2124
Matt Arsenault95245662016-02-11 05:32:46 +00002125 if (VT == MVT::i1) {
2126 return DAG.getTruncStore(Store->getChain(), DL,
2127 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
2128 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00002129 }
2130
Matt Arsenault95245662016-02-11 05:32:46 +00002131 assert(Store->getValue().getValueType().getScalarType() == MVT::i32);
Tom Stellard81d871d2013-11-13 23:36:50 +00002132
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002133 unsigned NumElements = VT.getVectorNumElements();
2134 switch (Store->getAddressSpace()) {
2135 case AMDGPUAS::GLOBAL_ADDRESS:
2136 case AMDGPUAS::FLAT_ADDRESS:
2137 if (NumElements > 4)
2138 return SplitVectorStore(Op, DAG);
2139 return SDValue();
2140 case AMDGPUAS::PRIVATE_ADDRESS: {
2141 switch (Subtarget->getMaxPrivateElementSize()) {
2142 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002143 return scalarizeVectorStore(Store, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002144 case 8:
2145 if (NumElements > 2)
2146 return SplitVectorStore(Op, DAG);
2147 return SDValue();
2148 case 16:
2149 if (NumElements > 4)
2150 return SplitVectorStore(Op, DAG);
2151 return SDValue();
2152 default:
2153 llvm_unreachable("unsupported private_element_size");
2154 }
2155 }
2156 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenault95245662016-02-11 05:32:46 +00002157 // If properly aligned, if we split we might be able to use ds_write_b64.
2158 return SplitVectorStore(Op, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002159 default:
2160 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00002161 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002162}
2163
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002164SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002165 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002166 EVT VT = Op.getValueType();
2167 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00002168 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002169 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
2170 DAG.getNode(ISD::FMUL, DL, VT, Arg,
2171 DAG.getConstantFP(0.5/M_PI, DL,
2172 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002173
2174 switch (Op.getOpcode()) {
2175 case ISD::FCOS:
2176 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
2177 case ISD::FSIN:
2178 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
2179 default:
2180 llvm_unreachable("Wrong trig opcode");
2181 }
2182}
2183
Tom Stellard354a43c2016-04-01 18:27:37 +00002184SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
2185 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
2186 assert(AtomicNode->isCompareAndSwap());
2187 unsigned AS = AtomicNode->getAddressSpace();
2188
2189 // No custom lowering required for local address space
2190 if (!isFlatGlobalAddrSpace(AS))
2191 return Op;
2192
2193 // Non-local address space requires custom lowering for atomic compare
2194 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
2195 SDLoc DL(Op);
2196 SDValue ChainIn = Op.getOperand(0);
2197 SDValue Addr = Op.getOperand(1);
2198 SDValue Old = Op.getOperand(2);
2199 SDValue New = Op.getOperand(3);
2200 EVT VT = Op.getValueType();
2201 MVT SimpleVT = VT.getSimpleVT();
2202 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
2203
2204 SDValue NewOld = DAG.getNode(ISD::BUILD_VECTOR, DL, VecType,
2205 New, Old);
2206 SDValue Ops[] = { ChainIn, Addr, NewOld };
2207 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2208 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL,
2209 VTList, Ops, VT, AtomicNode->getMemOperand());
2210}
2211
Tom Stellard75aadc22012-12-11 21:25:42 +00002212//===----------------------------------------------------------------------===//
2213// Custom DAG optimizations
2214//===----------------------------------------------------------------------===//
2215
Matt Arsenault364a6742014-06-11 17:50:44 +00002216SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00002217 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00002218 EVT VT = N->getValueType(0);
2219 EVT ScalarVT = VT.getScalarType();
2220 if (ScalarVT != MVT::f32)
2221 return SDValue();
2222
2223 SelectionDAG &DAG = DCI.DAG;
2224 SDLoc DL(N);
2225
2226 SDValue Src = N->getOperand(0);
2227 EVT SrcVT = Src.getValueType();
2228
2229 // TODO: We could try to match extracting the higher bytes, which would be
2230 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
2231 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
2232 // about in practice.
2233 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
2234 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
2235 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
2236 DCI.AddToWorklist(Cvt.getNode());
2237 return Cvt;
2238 }
2239 }
2240
2241 // We are primarily trying to catch operations on illegal vector types
2242 // before they are expanded.
2243 // For scalars, we can use the more flexible method of checking masked bits
2244 // after legalization.
2245 if (!DCI.isBeforeLegalize() ||
2246 !SrcVT.isVector() ||
2247 SrcVT.getVectorElementType() != MVT::i8) {
2248 return SDValue();
2249 }
2250
2251 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
2252
2253 // Weird sized vectors are a pain to handle, but we know 3 is really the same
2254 // size as 4.
2255 unsigned NElts = SrcVT.getVectorNumElements();
2256 if (!SrcVT.isSimple() && NElts != 3)
2257 return SDValue();
2258
2259 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
2260 // prevent a mess from expanding to v4i32 and repacking.
2261 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
2262 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
2263 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
2264 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00002265 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00002266
2267 unsigned AS = Load->getAddressSpace();
2268 unsigned Align = Load->getAlignment();
2269 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +00002270 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
Matt Arsenaulte6986632015-01-14 01:35:22 +00002271
2272 // Don't try to replace the load if we have to expand it due to alignment
2273 // problems. Otherwise we will end up scalarizing the load, and trying to
2274 // repack into the vector for no real reason.
2275 if (Align < ABIAlignment &&
2276 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
2277 return SDValue();
2278 }
2279
Matt Arsenault364a6742014-06-11 17:50:44 +00002280 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
2281 Load->getChain(),
2282 Load->getBasePtr(),
2283 LoadVT,
2284 Load->getMemOperand());
2285
2286 // Make sure successors of the original load stay after it by updating
2287 // them to use the new Chain.
2288 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
2289
2290 SmallVector<SDValue, 4> Elts;
2291 if (RegVT.isVector())
2292 DAG.ExtractVectorElements(NewLoad, Elts);
2293 else
2294 Elts.push_back(NewLoad);
2295
2296 SmallVector<SDValue, 4> Ops;
2297
2298 unsigned EltIdx = 0;
2299 for (SDValue Elt : Elts) {
2300 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
2301 for (unsigned I = 0; I < ComponentsInElt; ++I) {
2302 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
2303 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
2304 DCI.AddToWorklist(Cvt.getNode());
2305 Ops.push_back(Cvt);
2306 }
2307
2308 ++EltIdx;
2309 }
2310
2311 assert(Ops.size() == NElts);
2312
2313 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
2314 }
2315
2316 return SDValue();
2317}
2318
Eric Christopher6c5b5112015-03-11 18:43:21 +00002319/// \brief Return true if the given offset Size in bytes can be folded into
2320/// the immediate offsets of a memory instruction for the given address space.
2321static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
2322 const AMDGPUSubtarget &STI) {
2323 switch (AS) {
2324 case AMDGPUAS::GLOBAL_ADDRESS: {
2325 // MUBUF instructions a 12-bit offset in bytes.
2326 return isUInt<12>(OffsetSize);
2327 }
2328 case AMDGPUAS::CONSTANT_ADDRESS: {
2329 // SMRD instructions have an 8-bit offset in dwords on SI and
2330 // a 20-bit offset in bytes on VI.
2331 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
2332 return isUInt<20>(OffsetSize);
2333 else
2334 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
2335 }
2336 case AMDGPUAS::LOCAL_ADDRESS:
2337 case AMDGPUAS::REGION_ADDRESS: {
2338 // The single offset versions have a 16-bit offset in bytes.
2339 return isUInt<16>(OffsetSize);
2340 }
2341 case AMDGPUAS::PRIVATE_ADDRESS:
2342 // Indirect register addressing does not use any offsets.
2343 default:
2344 return 0;
2345 }
2346}
2347
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002348// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
2349
2350// This is a variant of
2351// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
2352//
2353// The normal DAG combiner will do this, but only if the add has one use since
2354// that would increase the number of instructions.
2355//
2356// This prevents us from seeing a constant offset that can be folded into a
2357// memory instruction's addressing mode. If we know the resulting add offset of
2358// a pointer can be folded into an addressing offset, we can replace the pointer
2359// operand with the add of new constant offset. This eliminates one of the uses,
2360// and may allow the remaining use to also be simplified.
2361//
2362SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
2363 unsigned AddrSpace,
2364 DAGCombinerInfo &DCI) const {
2365 SDValue N0 = N->getOperand(0);
2366 SDValue N1 = N->getOperand(1);
2367
2368 if (N0.getOpcode() != ISD::ADD)
2369 return SDValue();
2370
2371 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
2372 if (!CN1)
2373 return SDValue();
2374
2375 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2376 if (!CAdd)
2377 return SDValue();
2378
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002379 // If the resulting offset is too large, we can't fold it into the addressing
2380 // mode offset.
2381 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Eric Christopher6c5b5112015-03-11 18:43:21 +00002382 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002383 return SDValue();
2384
2385 SelectionDAG &DAG = DCI.DAG;
2386 SDLoc SL(N);
2387 EVT VT = N->getValueType(0);
2388
2389 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002390 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002391
2392 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
2393}
2394
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002395SDValue SITargetLowering::performAndCombine(SDNode *N,
2396 DAGCombinerInfo &DCI) const {
2397 if (DCI.isBeforeLegalize())
2398 return SDValue();
2399
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002400 if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI))
2401 return Base;
2402
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002403 SelectionDAG &DAG = DCI.DAG;
2404
2405 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
2406 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
2407 SDValue LHS = N->getOperand(0);
2408 SDValue RHS = N->getOperand(1);
2409
2410 if (LHS.getOpcode() == ISD::SETCC &&
2411 RHS.getOpcode() == ISD::SETCC) {
2412 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2413 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2414
2415 SDValue X = LHS.getOperand(0);
2416 SDValue Y = RHS.getOperand(0);
2417 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2418 return SDValue();
2419
2420 if (LCC == ISD::SETO) {
2421 if (X != LHS.getOperand(1))
2422 return SDValue();
2423
2424 if (RCC == ISD::SETUNE) {
2425 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2426 if (!C1 || !C1->isInfinity() || C1->isNegative())
2427 return SDValue();
2428
2429 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2430 SIInstrFlags::N_SUBNORMAL |
2431 SIInstrFlags::N_ZERO |
2432 SIInstrFlags::P_ZERO |
2433 SIInstrFlags::P_SUBNORMAL |
2434 SIInstrFlags::P_NORMAL;
2435
2436 static_assert(((~(SIInstrFlags::S_NAN |
2437 SIInstrFlags::Q_NAN |
2438 SIInstrFlags::N_INFINITY |
2439 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2440 "mask not equal");
2441
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002442 SDLoc DL(N);
2443 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2444 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002445 }
2446 }
2447 }
2448
2449 return SDValue();
2450}
2451
Matt Arsenaultf2290332015-01-06 23:00:39 +00002452SDValue SITargetLowering::performOrCombine(SDNode *N,
2453 DAGCombinerInfo &DCI) const {
2454 SelectionDAG &DAG = DCI.DAG;
2455 SDValue LHS = N->getOperand(0);
2456 SDValue RHS = N->getOperand(1);
2457
Matt Arsenault3b082382016-04-12 18:24:38 +00002458 EVT VT = N->getValueType(0);
2459 if (VT == MVT::i64) {
2460 // TODO: This could be a generic combine with a predicate for extracting the
2461 // high half of an integer being free.
2462
2463 // (or i64:x, (zero_extend i32:y)) ->
2464 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
2465 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
2466 RHS.getOpcode() != ISD::ZERO_EXTEND)
2467 std::swap(LHS, RHS);
2468
2469 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
2470 SDValue ExtSrc = RHS.getOperand(0);
2471 EVT SrcVT = ExtSrc.getValueType();
2472 if (SrcVT == MVT::i32) {
2473 SDLoc SL(N);
2474 SDValue LowLHS, HiBits;
2475 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
2476 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
2477
2478 DCI.AddToWorklist(LowOr.getNode());
2479 DCI.AddToWorklist(HiBits.getNode());
2480
2481 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
2482 LowOr, HiBits);
2483 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2484 }
2485 }
2486 }
2487
Matt Arsenaultf2290332015-01-06 23:00:39 +00002488 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
2489 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
2490 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
2491 SDValue Src = LHS.getOperand(0);
2492 if (Src != RHS.getOperand(0))
2493 return SDValue();
2494
2495 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
2496 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
2497 if (!CLHS || !CRHS)
2498 return SDValue();
2499
2500 // Only 10 bits are used.
2501 static const uint32_t MaxMask = 0x3ff;
2502
2503 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002504 SDLoc DL(N);
2505 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2506 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00002507 }
2508
2509 return SDValue();
2510}
2511
2512SDValue SITargetLowering::performClassCombine(SDNode *N,
2513 DAGCombinerInfo &DCI) const {
2514 SelectionDAG &DAG = DCI.DAG;
2515 SDValue Mask = N->getOperand(1);
2516
2517 // fp_class x, 0 -> false
2518 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
2519 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002520 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002521 }
2522
2523 return SDValue();
2524}
2525
Matt Arsenault9cd90712016-04-14 01:42:16 +00002526// Constant fold canonicalize.
2527SDValue SITargetLowering::performFCanonicalizeCombine(
2528 SDNode *N,
2529 DAGCombinerInfo &DCI) const {
2530 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
2531 if (!CFP)
2532 return SDValue();
2533
2534 SelectionDAG &DAG = DCI.DAG;
2535 const APFloat &C = CFP->getValueAPF();
2536
2537 // Flush denormals to 0 if not enabled.
2538 if (C.isDenormal()) {
2539 EVT VT = N->getValueType(0);
2540 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
2541 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2542
2543 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
2544 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2545 }
2546
2547 if (C.isNaN()) {
2548 EVT VT = N->getValueType(0);
2549 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
2550 if (C.isSignaling()) {
2551 // Quiet a signaling NaN.
2552 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
2553 }
2554
2555 // Make sure it is the canonical NaN bitpattern.
2556 //
2557 // TODO: Can we use -1 as the canonical NaN value since it's an inline
2558 // immediate?
2559 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
2560 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
2561 }
2562
2563 return SDValue(CFP, 0);
2564}
2565
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002566static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
2567 switch (Opc) {
2568 case ISD::FMAXNUM:
2569 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002570 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002571 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002572 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002573 return AMDGPUISD::UMAX3;
2574 case ISD::FMINNUM:
2575 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002576 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002577 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002578 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002579 return AMDGPUISD::UMIN3;
2580 default:
2581 llvm_unreachable("Not a min/max opcode");
2582 }
2583}
2584
Matt Arsenaultf639c322016-01-28 20:53:42 +00002585static SDValue performIntMed3ImmCombine(SelectionDAG &DAG,
2586 SDLoc SL,
2587 SDValue Op0,
2588 SDValue Op1,
2589 bool Signed) {
2590 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
2591 if (!K1)
2592 return SDValue();
2593
2594 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
2595 if (!K0)
2596 return SDValue();
2597
2598
2599 if (Signed) {
2600 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
2601 return SDValue();
2602 } else {
2603 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
2604 return SDValue();
2605 }
2606
2607 EVT VT = K0->getValueType(0);
2608 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
2609 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
2610}
2611
2612static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
2613 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
2614 return true;
2615
2616 return DAG.isKnownNeverNaN(Op);
2617}
2618
2619static SDValue performFPMed3ImmCombine(SelectionDAG &DAG,
2620 SDLoc SL,
2621 SDValue Op0,
2622 SDValue Op1) {
2623 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
2624 if (!K1)
2625 return SDValue();
2626
2627 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
2628 if (!K0)
2629 return SDValue();
2630
2631 // Ordered >= (although NaN inputs should have folded away by now).
2632 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
2633 if (Cmp == APFloat::cmpGreaterThan)
2634 return SDValue();
2635
2636 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
2637 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
2638 // give the other result, which is different from med3 with a NaN input.
2639 SDValue Var = Op0.getOperand(0);
2640 if (!isKnownNeverSNan(DAG, Var))
2641 return SDValue();
2642
2643 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
2644 Var, SDValue(K0, 0), SDValue(K1, 0));
2645}
2646
2647SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
2648 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002649 SelectionDAG &DAG = DCI.DAG;
2650
2651 unsigned Opc = N->getOpcode();
2652 SDValue Op0 = N->getOperand(0);
2653 SDValue Op1 = N->getOperand(1);
2654
2655 // Only do this if the inner op has one use since this will just increases
2656 // register pressure for no benefit.
2657
Matt Arsenault5b39b342016-01-28 20:53:48 +00002658 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
2659 // max(max(a, b), c) -> max3(a, b, c)
2660 // min(min(a, b), c) -> min3(a, b, c)
2661 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
2662 SDLoc DL(N);
2663 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2664 DL,
2665 N->getValueType(0),
2666 Op0.getOperand(0),
2667 Op0.getOperand(1),
2668 Op1);
2669 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002670
Matt Arsenault5b39b342016-01-28 20:53:48 +00002671 // Try commuted.
2672 // max(a, max(b, c)) -> max3(a, b, c)
2673 // min(a, min(b, c)) -> min3(a, b, c)
2674 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
2675 SDLoc DL(N);
2676 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2677 DL,
2678 N->getValueType(0),
2679 Op0,
2680 Op1.getOperand(0),
2681 Op1.getOperand(1));
2682 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002683 }
2684
Matt Arsenaultf639c322016-01-28 20:53:42 +00002685 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
2686 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
2687 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
2688 return Med3;
2689 }
2690
2691 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
2692 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
2693 return Med3;
2694 }
2695
2696 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00002697 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
2698 (Opc == AMDGPUISD::FMIN_LEGACY &&
2699 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00002700 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
2701 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
2702 return Res;
2703 }
2704
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002705 return SDValue();
2706}
2707
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002708SDValue SITargetLowering::performSetCCCombine(SDNode *N,
2709 DAGCombinerInfo &DCI) const {
2710 SelectionDAG &DAG = DCI.DAG;
2711 SDLoc SL(N);
2712
2713 SDValue LHS = N->getOperand(0);
2714 SDValue RHS = N->getOperand(1);
2715 EVT VT = LHS.getValueType();
2716
2717 if (VT != MVT::f32 && VT != MVT::f64)
2718 return SDValue();
2719
2720 // Match isinf pattern
2721 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
2722 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
2723 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
2724 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2725 if (!CRHS)
2726 return SDValue();
2727
2728 const APFloat &APF = CRHS->getValueAPF();
2729 if (APF.isInfinity() && !APF.isNegative()) {
2730 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002731 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
2732 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002733 }
2734 }
2735
2736 return SDValue();
2737}
2738
Tom Stellard75aadc22012-12-11 21:25:42 +00002739SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
2740 DAGCombinerInfo &DCI) const {
2741 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002742 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00002743
2744 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00002745 default:
2746 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002747 case ISD::SETCC:
2748 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00002749 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002750 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002751 case ISD::SMAX:
2752 case ISD::SMIN:
2753 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00002754 case ISD::UMIN:
2755 case AMDGPUISD::FMIN_LEGACY:
2756 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002757 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00002758 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002759 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00002760 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002761 break;
2762 }
Matt Arsenault364a6742014-06-11 17:50:44 +00002763
2764 case AMDGPUISD::CVT_F32_UBYTE0:
2765 case AMDGPUISD::CVT_F32_UBYTE1:
2766 case AMDGPUISD::CVT_F32_UBYTE2:
2767 case AMDGPUISD::CVT_F32_UBYTE3: {
2768 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
2769
2770 SDValue Src = N->getOperand(0);
2771 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
2772
2773 APInt KnownZero, KnownOne;
2774 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2775 !DCI.isBeforeLegalizeOps());
2776 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2777 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
2778 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
2779 DCI.CommitTargetLoweringOpt(TLO);
2780 }
2781
2782 break;
2783 }
2784
2785 case ISD::UINT_TO_FP: {
2786 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002787 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002788 case ISD::FADD: {
2789 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2790 break;
2791
2792 EVT VT = N->getValueType(0);
2793 if (VT != MVT::f32)
2794 break;
2795
Matt Arsenault8d630032015-02-20 22:10:41 +00002796 // Only do this if we are not trying to support denormals. v_mad_f32 does
2797 // not support denormals ever.
2798 if (Subtarget->hasFP32Denormals())
2799 break;
2800
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002801 SDValue LHS = N->getOperand(0);
2802 SDValue RHS = N->getOperand(1);
2803
2804 // These should really be instruction patterns, but writing patterns with
2805 // source modiifiers is a pain.
2806
2807 // fadd (fadd (a, a), b) -> mad 2.0, a, b
2808 if (LHS.getOpcode() == ISD::FADD) {
2809 SDValue A = LHS.getOperand(0);
2810 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002811 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002812 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002813 }
2814 }
2815
2816 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
2817 if (RHS.getOpcode() == ISD::FADD) {
2818 SDValue A = RHS.getOperand(0);
2819 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002820 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002821 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002822 }
2823 }
2824
Matt Arsenault8d630032015-02-20 22:10:41 +00002825 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002826 }
Matt Arsenault8675db12014-08-29 16:01:14 +00002827 case ISD::FSUB: {
2828 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2829 break;
2830
2831 EVT VT = N->getValueType(0);
2832
2833 // Try to get the fneg to fold into the source modifier. This undoes generic
2834 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00002835 //
2836 // Only do this if we are not trying to support denormals. v_mad_f32 does
2837 // not support denormals ever.
2838 if (VT == MVT::f32 &&
2839 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00002840 SDValue LHS = N->getOperand(0);
2841 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002842 if (LHS.getOpcode() == ISD::FADD) {
2843 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
2844
2845 SDValue A = LHS.getOperand(0);
2846 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002847 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002848 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
2849
Matt Arsenault8d630032015-02-20 22:10:41 +00002850 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002851 }
2852 }
2853
2854 if (RHS.getOpcode() == ISD::FADD) {
2855 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
2856
2857 SDValue A = RHS.getOperand(0);
2858 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002859 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002860 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002861 }
2862 }
Matt Arsenault8d630032015-02-20 22:10:41 +00002863
2864 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00002865 }
2866
2867 break;
2868 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002869 case ISD::LOAD:
2870 case ISD::STORE:
2871 case ISD::ATOMIC_LOAD:
2872 case ISD::ATOMIC_STORE:
2873 case ISD::ATOMIC_CMP_SWAP:
2874 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2875 case ISD::ATOMIC_SWAP:
2876 case ISD::ATOMIC_LOAD_ADD:
2877 case ISD::ATOMIC_LOAD_SUB:
2878 case ISD::ATOMIC_LOAD_AND:
2879 case ISD::ATOMIC_LOAD_OR:
2880 case ISD::ATOMIC_LOAD_XOR:
2881 case ISD::ATOMIC_LOAD_NAND:
2882 case ISD::ATOMIC_LOAD_MIN:
2883 case ISD::ATOMIC_LOAD_MAX:
2884 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002885 case ISD::ATOMIC_LOAD_UMAX:
2886 case AMDGPUISD::ATOMIC_INC:
2887 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002888 if (DCI.isBeforeLegalize())
2889 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002890
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002891 MemSDNode *MemNode = cast<MemSDNode>(N);
2892 SDValue Ptr = MemNode->getBasePtr();
2893
2894 // TODO: We could also do this for multiplies.
2895 unsigned AS = MemNode->getAddressSpace();
2896 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
2897 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
2898 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002899 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002900
2901 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
2902 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
2903 }
2904 }
2905 break;
2906 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002907 case ISD::AND:
2908 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002909 case ISD::OR:
2910 return performOrCombine(N, DCI);
2911 case AMDGPUISD::FP_CLASS:
2912 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00002913 case ISD::FCANONICALIZE:
2914 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002915 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002916 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00002917}
Christian Konigd910b7d2013-02-26 17:52:16 +00002918
Christian Konigf82901a2013-02-26 17:52:23 +00002919/// \brief Analyze the possible immediate value Op
2920///
2921/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
2922/// and the immediate value if it's a literal immediate
2923int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
2924
Eric Christopher7792e322015-01-30 23:24:40 +00002925 const SIInstrInfo *TII =
2926 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00002927
Tom Stellardedbf1eb2013-04-05 23:31:20 +00002928 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00002929 if (TII->isInlineConstant(Node->getAPIntValue()))
2930 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00002931
Matt Arsenault11a4d672015-02-13 19:05:03 +00002932 uint64_t Val = Node->getZExtValue();
2933 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00002934 }
2935
2936 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
2937 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
2938 return 0;
2939
2940 if (Node->getValueType(0) == MVT::f32)
2941 return FloatToBits(Node->getValueAPF().convertToFloat());
2942
2943 return -1;
2944 }
2945
2946 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00002947}
2948
Christian Konig8e06e2a2013-04-10 08:39:08 +00002949/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00002950static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00002951 switch (Idx) {
2952 default: return 0;
2953 case AMDGPU::sub0: return 0;
2954 case AMDGPU::sub1: return 1;
2955 case AMDGPU::sub2: return 2;
2956 case AMDGPU::sub3: return 3;
2957 }
2958}
2959
2960/// \brief Adjust the writemask of MIMG instructions
2961void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
2962 SelectionDAG &DAG) const {
2963 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00002964 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002965 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
2966 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00002967 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002968
2969 // Try to figure out the used register components
2970 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
2971 I != E; ++I) {
2972
2973 // Abort if we can't understand the usage
2974 if (!I->isMachineOpcode() ||
2975 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
2976 return;
2977
Tom Stellard54774e52013-10-23 02:53:47 +00002978 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
2979 // Note that subregs are packed, i.e. Lane==0 is the first bit set
2980 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
2981 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00002982 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00002983
Tom Stellard54774e52013-10-23 02:53:47 +00002984 // Set which texture component corresponds to the lane.
2985 unsigned Comp;
2986 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
2987 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00002988 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00002989 Dmask &= ~(1 << Comp);
2990 }
2991
Christian Konig8e06e2a2013-04-10 08:39:08 +00002992 // Abort if we have more than one user per component
2993 if (Users[Lane])
2994 return;
2995
2996 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00002997 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002998 }
2999
Tom Stellard54774e52013-10-23 02:53:47 +00003000 // Abort if there's no change
3001 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00003002 return;
3003
3004 // Adjust the writemask in the node
3005 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003006 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003007 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003008 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00003009 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003010
Christian Konig8b1ed282013-04-10 08:39:16 +00003011 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00003012 // (if NewDmask has only one bit set...)
3013 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003014 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
3015 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00003016 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003017 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00003018 SDValue(Node, 0), RC);
3019 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
3020 return;
3021 }
3022
Christian Konig8e06e2a2013-04-10 08:39:08 +00003023 // Update the users of the node with the new indices
3024 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
3025
3026 SDNode *User = Users[i];
3027 if (!User)
3028 continue;
3029
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003030 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003031 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
3032
3033 switch (Idx) {
3034 default: break;
3035 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
3036 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
3037 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
3038 }
3039 }
3040}
3041
Tom Stellardc98ee202015-07-16 19:40:07 +00003042static bool isFrameIndexOp(SDValue Op) {
3043 if (Op.getOpcode() == ISD::AssertZext)
3044 Op = Op.getOperand(0);
3045
3046 return isa<FrameIndexSDNode>(Op);
3047}
3048
Tom Stellard3457a842014-10-09 19:06:00 +00003049/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
3050/// with frame index operands.
3051/// LLVM assumes that inputs are to these instructions are registers.
3052void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
3053 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003054
3055 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00003056 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00003057 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00003058 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003059 continue;
3060 }
3061
Tom Stellard3457a842014-10-09 19:06:00 +00003062 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003063 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00003064 Node->getOperand(i).getValueType(),
3065 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003066 }
3067
Tom Stellard3457a842014-10-09 19:06:00 +00003068 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003069}
3070
Matt Arsenault08d84942014-06-03 23:06:13 +00003071/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00003072SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
3073 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00003074 const SIInstrInfo *TII =
3075 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003076 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00003077
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003078 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore())
Christian Konig8e06e2a2013-04-10 08:39:08 +00003079 adjustWritemask(Node, DAG);
3080
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003081 if (Opcode == AMDGPU::INSERT_SUBREG ||
3082 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003083 legalizeTargetIndependentNode(Node, DAG);
3084 return Node;
3085 }
Tom Stellard654d6692015-01-08 15:08:17 +00003086 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003087}
Christian Konig8b1ed282013-04-10 08:39:16 +00003088
3089/// \brief Assign the register class depending on the number of
3090/// bits set in the writemask
3091void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
3092 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00003093 const SIInstrInfo *TII =
3094 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003095
Tom Stellarda99ada52014-11-21 22:31:44 +00003096 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003097
3098 if (TII->isVOP3(MI->getOpcode())) {
3099 // Make sure constant bus requirements are respected.
3100 TII->legalizeOperandsVOP3(MRI, MI);
3101 return;
3102 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00003103
Matt Arsenault3add6432015-10-20 04:35:43 +00003104 if (TII->isMIMG(*MI)) {
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003105 unsigned VReg = MI->getOperand(0).getReg();
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003106 unsigned DmaskIdx = MI->getNumOperands() == 12 ? 3 : 4;
3107 unsigned Writemask = MI->getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003108 unsigned BitsSet = 0;
3109 for (unsigned i = 0; i < 4; ++i)
3110 BitsSet += Writemask & (1 << i) ? 1 : 0;
3111
3112 const TargetRegisterClass *RC;
3113 switch (BitsSet) {
3114 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00003115 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003116 case 2: RC = &AMDGPU::VReg_64RegClass; break;
3117 case 3: RC = &AMDGPU::VReg_96RegClass; break;
3118 }
3119
3120 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
3121 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003122 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00003123 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00003124 }
3125
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003126 // Replace unused atomics with the no return version.
3127 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
3128 if (NoRetAtomicOp != -1) {
3129 if (!Node->hasAnyUseOfValue(0)) {
3130 MI->setDesc(TII->get(NoRetAtomicOp));
3131 MI->RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003132 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003133 }
3134
Tom Stellard354a43c2016-04-01 18:27:37 +00003135 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
3136 // instruction, because the return type of these instructions is a vec2 of
3137 // the memory type, so it can be tied to the input operand.
3138 // This means these instructions always have a use, so we need to add a
3139 // special case to check if the atomic has only one extract_subreg use,
3140 // which itself has no uses.
3141 if ((Node->hasNUsesOfValue(1, 0) &&
Nicolai Haehnle750082d2016-04-15 14:42:36 +00003142 Node->use_begin()->isMachineOpcode() &&
Tom Stellard354a43c2016-04-01 18:27:37 +00003143 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
3144 !Node->use_begin()->hasAnyUseOfValue(0))) {
3145 unsigned Def = MI->getOperand(0).getReg();
3146
3147 // Change this into a noret atomic.
3148 MI->setDesc(TII->get(NoRetAtomicOp));
3149 MI->RemoveOperand(0);
3150
3151 // If we only remove the def operand from the atomic instruction, the
3152 // extract_subreg will be left with a use of a vreg without a def.
3153 // So we need to insert an implicit_def to avoid machine verifier
3154 // errors.
3155 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
3156 TII->get(AMDGPU::IMPLICIT_DEF), Def);
3157 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003158 return;
3159 }
Christian Konig8b1ed282013-04-10 08:39:16 +00003160}
Tom Stellard0518ff82013-06-03 17:39:58 +00003161
Matt Arsenault485defe2014-11-05 19:01:17 +00003162static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003163 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00003164 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
3165}
3166
3167MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
3168 SDLoc DL,
3169 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00003170 const SIInstrInfo *TII =
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003171 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00003172
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003173 // Build the half of the subregister with the constants before building the
3174 // full 128-bit register. If we are building multiple resource descriptors,
3175 // this will allow CSEing of the 2-component register.
3176 const SDValue Ops0[] = {
3177 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
3178 buildSMovImm32(DAG, DL, 0),
3179 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
3180 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
3181 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
3182 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003183
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003184 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
3185 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00003186
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003187 // Combine the constants and the pointer.
3188 const SDValue Ops1[] = {
3189 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
3190 Ptr,
3191 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
3192 SubRegHi,
3193 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
3194 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003195
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003196 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00003197}
3198
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003199/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003200/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
3201/// of the resource descriptor) to create an offset, which is added to
3202/// the resource pointer.
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003203MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
3204 SDLoc DL,
3205 SDValue Ptr,
3206 uint32_t RsrcDword1,
3207 uint64_t RsrcDword2And3) const {
3208 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
3209 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
3210 if (RsrcDword1) {
3211 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003212 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
3213 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003214 }
3215
3216 SDValue DataLo = buildSMovImm32(DAG, DL,
3217 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
3218 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
3219
3220 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003221 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003222 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003223 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003224 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003225 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003226 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003227 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003228 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003229 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003230 };
3231
3232 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
3233}
3234
Tom Stellard94593ee2013-06-03 17:40:18 +00003235SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3236 const TargetRegisterClass *RC,
3237 unsigned Reg, EVT VT) const {
3238 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
3239
3240 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
3241 cast<RegisterSDNode>(VReg)->getReg(), VT);
3242}
Tom Stellardd7e6f132015-04-08 01:09:26 +00003243
3244//===----------------------------------------------------------------------===//
3245// SI Inline Assembly Support
3246//===----------------------------------------------------------------------===//
3247
3248std::pair<unsigned, const TargetRegisterClass *>
3249SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003250 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00003251 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003252
3253 if (Constraint.size() == 1) {
3254 switch (Constraint[0]) {
3255 case 's':
3256 case 'r':
3257 switch (VT.getSizeInBits()) {
3258 default:
3259 return std::make_pair(0U, nullptr);
3260 case 32:
Tom Stellardd7e6f132015-04-08 01:09:26 +00003261 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003262 case 64:
3263 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
3264 case 128:
3265 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
3266 case 256:
3267 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
3268 }
3269
3270 case 'v':
3271 switch (VT.getSizeInBits()) {
3272 default:
3273 return std::make_pair(0U, nullptr);
3274 case 32:
3275 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
3276 case 64:
3277 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
3278 case 96:
3279 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
3280 case 128:
3281 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
3282 case 256:
3283 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
3284 case 512:
3285 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
3286 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00003287 }
3288 }
3289
3290 if (Constraint.size() > 1) {
3291 const TargetRegisterClass *RC = nullptr;
3292 if (Constraint[1] == 'v') {
3293 RC = &AMDGPU::VGPR_32RegClass;
3294 } else if (Constraint[1] == 's') {
3295 RC = &AMDGPU::SGPR_32RegClass;
3296 }
3297
3298 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00003299 uint32_t Idx;
3300 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
3301 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00003302 return std::make_pair(RC->getRegister(Idx), RC);
3303 }
3304 }
3305 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
3306}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003307
3308SITargetLowering::ConstraintType
3309SITargetLowering::getConstraintType(StringRef Constraint) const {
3310 if (Constraint.size() == 1) {
3311 switch (Constraint[0]) {
3312 default: break;
3313 case 's':
3314 case 'v':
3315 return C_RegisterClass;
3316 }
3317 }
3318 return TargetLowering::getConstraintType(Constraint);
3319}