blob: a5d1ec259e08a4e60407fed26a06daa1097d17bb [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 Arsenault02cb0ff2014-09-29 14:59:34 +0000281 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000282 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000283 setTargetDAGCombine(ISD::FMINNUM);
284 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000285 setTargetDAGCombine(ISD::SMIN);
286 setTargetDAGCombine(ISD::SMAX);
287 setTargetDAGCombine(ISD::UMIN);
288 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000289 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000290 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000291 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000292 setTargetDAGCombine(ISD::UINT_TO_FP);
Matt Arsenault9cd90712016-04-14 01:42:16 +0000293 setTargetDAGCombine(ISD::FCANONICALIZE);
Matt Arsenault364a6742014-06-11 17:50:44 +0000294
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000295 // All memory operations. Some folding on the pointer operand is done to help
296 // matching the constant offsets in the addressing modes.
297 setTargetDAGCombine(ISD::LOAD);
298 setTargetDAGCombine(ISD::STORE);
299 setTargetDAGCombine(ISD::ATOMIC_LOAD);
300 setTargetDAGCombine(ISD::ATOMIC_STORE);
301 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
302 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
303 setTargetDAGCombine(ISD::ATOMIC_SWAP);
304 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
305 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
306 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
307 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
308 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
309 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
310 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
311 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
312 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
313 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
314
Christian Konigeecebd02013-03-26 14:04:02 +0000315 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000316}
317
Tom Stellard0125f2a2013-06-25 02:39:35 +0000318//===----------------------------------------------------------------------===//
319// TargetLowering queries
320//===----------------------------------------------------------------------===//
321
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000322bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
323 const CallInst &CI,
324 unsigned IntrID) const {
325 switch (IntrID) {
326 case Intrinsic::amdgcn_atomic_inc:
327 case Intrinsic::amdgcn_atomic_dec:
328 Info.opc = ISD::INTRINSIC_W_CHAIN;
329 Info.memVT = MVT::getVT(CI.getType());
330 Info.ptrVal = CI.getOperand(0);
331 Info.align = 0;
332 Info.vol = false;
333 Info.readMem = true;
334 Info.writeMem = true;
335 return true;
336 default:
337 return false;
338 }
339}
340
Matt Arsenaulte306a322014-10-21 16:25:08 +0000341bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
342 EVT) const {
343 // SI has some legal vector types, but no legal vector operations. Say no
344 // shuffles are legal in order to prefer scalarizing some vector operations.
345 return false;
346}
347
Tom Stellard70580f82015-07-20 14:28:41 +0000348bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
349 // Flat instructions do not have offsets, and only have the register
350 // address.
351 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
352}
353
Matt Arsenault711b3902015-08-07 20:18:34 +0000354bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
355 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
356 // additionally can do r + r + i with addr64. 32-bit has more addressing
357 // mode options. Depending on the resource constant, it can also do
358 // (i64 r0) + (i32 r1) * (i14 i).
359 //
360 // Private arrays end up using a scratch buffer most of the time, so also
361 // assume those use MUBUF instructions. Scratch loads / stores are currently
362 // implemented as mubuf instructions with offen bit set, so slightly
363 // different than the normal addr64.
364 if (!isUInt<12>(AM.BaseOffs))
365 return false;
366
367 // FIXME: Since we can split immediate into soffset and immediate offset,
368 // would it make sense to allow any immediate?
369
370 switch (AM.Scale) {
371 case 0: // r + i or just i, depending on HasBaseReg.
372 return true;
373 case 1:
374 return true; // We have r + r or r + i.
375 case 2:
376 if (AM.HasBaseReg) {
377 // Reject 2 * r + r.
378 return false;
379 }
380
381 // Allow 2 * r as r + r
382 // Or 2 * r + i is allowed as r + r + i.
383 return true;
384 default: // Don't allow n * r
385 return false;
386 }
387}
388
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000389bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
390 const AddrMode &AM, Type *Ty,
391 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000392 // No global is ever allowed as a base.
393 if (AM.BaseGV)
394 return false;
395
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000396 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000397 case AMDGPUAS::GLOBAL_ADDRESS: {
Tom Stellard70580f82015-07-20 14:28:41 +0000398 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
399 // Assume the we will use FLAT for all global memory accesses
400 // on VI.
401 // FIXME: This assumption is currently wrong. On VI we still use
402 // MUBUF instructions for the r + i addressing mode. As currently
403 // implemented, the MUBUF instructions only work on buffer < 4GB.
404 // It may be possible to support > 4GB buffers with MUBUF instructions,
405 // by setting the stride value in the resource descriptor which would
406 // increase the size limit to (stride * 4GB). However, this is risky,
407 // because it has never been validated.
408 return isLegalFlatAddressingMode(AM);
409 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000410
Matt Arsenault711b3902015-08-07 20:18:34 +0000411 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000412 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000413 case AMDGPUAS::CONSTANT_ADDRESS: {
414 // If the offset isn't a multiple of 4, it probably isn't going to be
415 // correctly aligned.
416 if (AM.BaseOffs % 4 != 0)
417 return isLegalMUBUFAddressingMode(AM);
418
419 // There are no SMRD extloads, so if we have to do a small type access we
420 // will use a MUBUF load.
421 // FIXME?: We also need to do this if unaligned, but we don't know the
422 // alignment here.
423 if (DL.getTypeStoreSize(Ty) < 4)
424 return isLegalMUBUFAddressingMode(AM);
425
426 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
427 // SMRD instructions have an 8-bit, dword offset on SI.
428 if (!isUInt<8>(AM.BaseOffs / 4))
429 return false;
430 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) {
431 // On CI+, this can also be a 32-bit literal constant offset. If it fits
432 // in 8-bits, it can use a smaller encoding.
433 if (!isUInt<32>(AM.BaseOffs / 4))
434 return false;
435 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) {
436 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
437 if (!isUInt<20>(AM.BaseOffs))
438 return false;
439 } else
440 llvm_unreachable("unhandled generation");
441
442 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
443 return true;
444
445 if (AM.Scale == 1 && AM.HasBaseReg)
446 return true;
447
448 return false;
449 }
450
451 case AMDGPUAS::PRIVATE_ADDRESS:
452 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
453 return isLegalMUBUFAddressingMode(AM);
454
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000455 case AMDGPUAS::LOCAL_ADDRESS:
456 case AMDGPUAS::REGION_ADDRESS: {
457 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
458 // field.
459 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
460 // an 8-bit dword offset but we don't know the alignment here.
461 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000462 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000463
464 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
465 return true;
466
467 if (AM.Scale == 1 && AM.HasBaseReg)
468 return true;
469
Matt Arsenault5015a892014-08-15 17:17:07 +0000470 return false;
471 }
Tom Stellard70580f82015-07-20 14:28:41 +0000472 case AMDGPUAS::FLAT_ADDRESS:
473 return isLegalFlatAddressingMode(AM);
474
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000475 default:
476 llvm_unreachable("unhandled address space");
477 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000478}
479
Matt Arsenaulte6986632015-01-14 01:35:22 +0000480bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000481 unsigned AddrSpace,
482 unsigned Align,
483 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000484 if (IsFast)
485 *IsFast = false;
486
Matt Arsenault1018c892014-04-24 17:08:26 +0000487 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
488 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000489 if (!VT.isSimple() || VT == MVT::Other)
490 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000491
Tom Stellardc6b299c2015-02-02 18:02:28 +0000492 // TODO - CI+ supports unaligned memory accesses, but this requires driver
493 // support.
Matt Arsenault1018c892014-04-24 17:08:26 +0000494
Matt Arsenault1018c892014-04-24 17:08:26 +0000495 // XXX - The only mention I see of this in the ISA manual is for LDS direct
496 // reads the "byte address and must be dword aligned". Is it also true for the
497 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000498 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
499 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
500 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
501 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000502 bool AlignedBy4 = (Align % 4 == 0);
503 if (IsFast)
504 *IsFast = AlignedBy4;
505 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000506 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000507
Tom Stellard33e64c62015-02-04 20:49:52 +0000508 // Smaller than dword value must be aligned.
509 // FIXME: This should be allowed on CI+
510 if (VT.bitsLT(MVT::i32))
511 return false;
512
Matt Arsenault1018c892014-04-24 17:08:26 +0000513 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
514 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000515 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000516 if (IsFast)
517 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000518
519 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000520}
521
Matt Arsenault46645fa2014-07-28 17:49:26 +0000522EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
523 unsigned SrcAlign, bool IsMemset,
524 bool ZeroMemset,
525 bool MemcpyStrSrc,
526 MachineFunction &MF) const {
527 // FIXME: Should account for address space here.
528
529 // The default fallback uses the private pointer size as a guess for a type to
530 // use. Make sure we switch these to 64-bit accesses.
531
532 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
533 return MVT::v4i32;
534
535 if (Size >= 8 && DstAlign >= 4)
536 return MVT::v2i32;
537
538 // Use the default.
539 return MVT::Other;
540}
541
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000542static bool isFlatGlobalAddrSpace(unsigned AS) {
543 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
544 AS == AMDGPUAS::FLAT_ADDRESS ||
545 AS == AMDGPUAS::CONSTANT_ADDRESS;
546}
547
548bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
549 unsigned DestAS) const {
550 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
551}
552
Tom Stellarda6f24c62015-12-15 20:55:55 +0000553
554bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
555 const MemSDNode *MemNode = cast<MemSDNode>(N);
556 const Value *Ptr = MemNode->getMemOperand()->getValue();
557
558 // UndefValue means this is a load of a kernel input. These are uniform.
559 // Sometimes LDS instructions have constant pointers
560 if (isa<UndefValue>(Ptr) || isa<Argument>(Ptr) || isa<Constant>(Ptr) ||
561 isa<GlobalValue>(Ptr))
562 return true;
563
564 const Instruction *I = dyn_cast_or_null<Instruction>(Ptr);
565 return I && I->getMetadata("amdgpu.uniform");
566}
567
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000568TargetLoweringBase::LegalizeTypeAction
569SITargetLowering::getPreferredVectorAction(EVT VT) const {
570 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
571 return TypeSplitVector;
572
573 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000574}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000575
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000576bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
577 Type *Ty) const {
Eric Christopher7792e322015-01-30 23:24:40 +0000578 const SIInstrInfo *TII =
579 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000580 return TII->isInlineConstant(Imm);
581}
582
Tom Stellard2e045bb2016-01-20 00:13:22 +0000583bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
584
585 // SimplifySetCC uses this function to determine whether or not it should
586 // create setcc with i1 operands. We don't have instructions for i1 setcc.
587 if (VT == MVT::i1 && Op == ISD::SETCC)
588 return false;
589
590 return TargetLowering::isTypeDesirableForOp(Op, VT);
591}
592
Tom Stellardaf775432013-10-23 00:44:32 +0000593SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000594 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000595 unsigned Offset, bool Signed) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000596 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000597 MachineFunction &MF = DAG.getMachineFunction();
598 const SIRegisterInfo *TRI =
599 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
Matt Arsenaultac234b62015-11-30 21:15:57 +0000600 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000601
Matt Arsenault86033ca2014-07-28 17:31:39 +0000602 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
603
604 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000605 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000606 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000607 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
608 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
609 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
610 DAG.getConstant(Offset, SL, PtrVT));
Mehdi Amini44ede332015-07-09 02:09:04 +0000611 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000612 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
613
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000614 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000615
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000616 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000617 if (MemVT.isFloatingPoint())
618 ExtTy = ISD::EXTLOAD;
619
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000620 return DAG.getLoad(ISD::UNINDEXED, ExtTy,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000621 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
622 false, // isVolatile
623 true, // isNonTemporal
624 true, // isInvariant
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000625 Align); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000626}
627
Christian Konig2c8f6d52013-03-07 09:03:52 +0000628SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000629 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
630 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
631 SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardec2e43c2014-09-22 15:35:29 +0000632 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000633 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000634
635 MachineFunction &MF = DAG.getMachineFunction();
636 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000637 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000638 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000639
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000640 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) {
Matt Arsenaultd48da142015-11-02 23:23:02 +0000641 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000642 DiagnosticInfoUnsupported NoGraphicsHSA(
643 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000644 DAG.getContext()->diagnose(NoGraphicsHSA);
645 return SDValue();
646 }
647
Christian Konig2c8f6d52013-03-07 09:03:52 +0000648 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000649 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000650
651 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000652 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000653
654 // First check if it's a PS input addr
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000655 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000656 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000657
Marek Olsakfccabaf2016-01-13 11:45:36 +0000658 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000659 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000660 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000661 ++PSInputNum;
662 continue;
663 }
664
Marek Olsakfccabaf2016-01-13 11:45:36 +0000665 Info->markPSInputAllocated(PSInputNum);
666 if (Arg.Used)
667 Info->PSInputEna |= 1 << PSInputNum;
668
669 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000670 }
671
672 // Second split vertices into their elements
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000673 if (AMDGPU::isShader(CallConv) &&
674 Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000675 ISD::InputArg NewArg = Arg;
676 NewArg.Flags.setSplit();
677 NewArg.VT = Arg.VT.getVectorElementType();
678
679 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
680 // three or five element vertex only needs three or five registers,
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000681 // NOT four or eight.
Andrew Trick05938a52015-02-16 18:10:47 +0000682 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000683 unsigned NumElements = ParamType->getVectorNumElements();
684
685 for (unsigned j = 0; j != NumElements; ++j) {
686 Splits.push_back(NewArg);
687 NewArg.PartOffset += NewArg.VT.getStoreSize();
688 }
689
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000690 } else if (AMDGPU::isShader(CallConv)) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000691 Splits.push_back(Arg);
692 }
693 }
694
695 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000696 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
697 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000698
Christian Konig99ee0f42013-03-07 09:04:14 +0000699 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000700 //
701 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
702 // PSInputAddr, the user wants to enable some bits after the compilation
703 // based on run-time states. Since we can't know what the final PSInputEna
704 // will look like, so we shouldn't do anything here and the user should take
705 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000706 //
707 // Otherwise, the following restrictions apply:
708 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
709 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
710 // enabled too.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000711 if (CallConv == CallingConv::AMDGPU_PS &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000712 ((Info->getPSInputAddr() & 0x7F) == 0 ||
713 ((Info->getPSInputAddr() & 0xF) == 0 &&
714 Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000715 CCInfo.AllocateReg(AMDGPU::VGPR0);
716 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000717 Info->markPSInputAllocated(0);
718 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000719 }
720
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000721 if (!AMDGPU::isShader(CallConv)) {
Tom Stellardaf775432013-10-23 00:44:32 +0000722 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
723 Splits);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000724
725 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
726 } else {
727 assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() &&
728 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() &&
729 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
730 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
731 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
732 !Info->hasWorkItemIDZ());
Tom Stellardaf775432013-10-23 00:44:32 +0000733 }
734
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000735 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
736 if (Info->hasPrivateSegmentBuffer()) {
737 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
738 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
739 CCInfo.AllocateReg(PrivateSegmentBufferReg);
740 }
741
742 if (Info->hasDispatchPtr()) {
743 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
744 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
745 CCInfo.AllocateReg(DispatchPtrReg);
746 }
747
Matt Arsenault48ab5262016-04-25 19:27:18 +0000748 if (Info->hasQueuePtr()) {
749 unsigned QueuePtrReg = Info->addQueuePtr(*TRI);
750 MF.addLiveIn(QueuePtrReg, &AMDGPU::SReg_64RegClass);
751 CCInfo.AllocateReg(QueuePtrReg);
752 }
753
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000754 if (Info->hasKernargSegmentPtr()) {
755 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
756 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
757 CCInfo.AllocateReg(InputPtrReg);
758 }
759
Matt Arsenault296b8492016-02-12 06:31:30 +0000760 if (Info->hasFlatScratchInit()) {
761 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
762 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass);
763 CCInfo.AllocateReg(FlatScratchInitReg);
764 }
765
Christian Konig2c8f6d52013-03-07 09:03:52 +0000766 AnalyzeFormalArguments(CCInfo, Splits);
767
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000768 SmallVector<SDValue, 16> Chains;
769
Christian Konig2c8f6d52013-03-07 09:03:52 +0000770 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
771
Christian Konigb7be72d2013-05-17 09:46:48 +0000772 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000773 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000774 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000775 continue;
776 }
777
Christian Konig2c8f6d52013-03-07 09:03:52 +0000778 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000779 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000780
781 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000782 VT = Ins[i].VT;
783 EVT MemVT = Splits[i].VT;
Tom Stellardb5798b02015-06-26 21:15:03 +0000784 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
785 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000786 // The first 36 bytes of the input buffer contains information about
787 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000788 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000789 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000790 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000791
Craig Toppere3dcce92015-08-01 22:20:21 +0000792 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000793 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000794 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
795 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
796 // On SI local pointers are just offsets into LDS, so they are always
797 // less than 16-bits. On CI and newer they could potentially be
798 // real pointers, so we can't guarantee their size.
799 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
800 DAG.getValueType(MVT::i16));
801 }
802
Tom Stellarded882c22013-06-03 17:40:11 +0000803 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000804 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000805 continue;
806 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000807 assert(VA.isRegLoc() && "Parameter must be in a register!");
808
809 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000810
811 if (VT == MVT::i64) {
812 // For now assume it is a pointer
813 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
814 &AMDGPU::SReg_64RegClass);
815 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000816 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
817 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000818 continue;
819 }
820
821 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
822
823 Reg = MF.addLiveIn(Reg, RC);
824 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
825
Christian Konig2c8f6d52013-03-07 09:03:52 +0000826 if (Arg.VT.isVector()) {
827
828 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000829 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000830 unsigned NumElements = ParamType->getVectorNumElements();
831
832 SmallVector<SDValue, 4> Regs;
833 Regs.push_back(Val);
834 for (unsigned j = 1; j != NumElements; ++j) {
835 Reg = ArgLocs[ArgIdx++].getLocReg();
836 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000837
838 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
839 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000840 }
841
842 // Fill up the missing vector elements
843 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000844 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000845
Craig Topper48d114b2014-04-26 18:35:24 +0000846 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000847 continue;
848 }
849
850 InVals.push_back(Val);
851 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000852
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000853 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
854 // these from the dispatch pointer.
855
856 // Start adding system SGPRs.
857 if (Info->hasWorkGroupIDX()) {
858 unsigned Reg = Info->addWorkGroupIDX();
859 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
860 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000861 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000862
863 if (Info->hasWorkGroupIDY()) {
864 unsigned Reg = Info->addWorkGroupIDY();
865 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
866 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000867 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000868
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000869 if (Info->hasWorkGroupIDZ()) {
870 unsigned Reg = Info->addWorkGroupIDZ();
871 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
872 CCInfo.AllocateReg(Reg);
873 }
874
875 if (Info->hasWorkGroupInfo()) {
876 unsigned Reg = Info->addWorkGroupInfo();
877 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
878 CCInfo.AllocateReg(Reg);
879 }
880
881 if (Info->hasPrivateSegmentWaveByteOffset()) {
882 // Scratch wave offset passed in system SGPR.
Tom Stellardf110f8f2016-04-14 16:27:03 +0000883 unsigned PrivateSegmentWaveByteOffsetReg;
884
885 if (AMDGPU::isShader(CallConv)) {
886 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
887 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
888 } else
889 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset();
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000890
891 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
892 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
893 }
894
895 // Now that we've figured out where the scratch register inputs are, see if
896 // should reserve the arguments and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000897 bool HasStackObjects = MF.getFrameInfo()->hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +0000898 // Record that we know we have non-spill stack objects so we don't need to
899 // check all stack objects later.
900 if (HasStackObjects)
901 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000902
903 if (ST.isAmdHsaOS()) {
904 // TODO: Assume we will spill without optimizations.
905 if (HasStackObjects) {
906 // If we have stack objects, we unquestionably need the private buffer
907 // resource. For the HSA ABI, this will be the first 4 user SGPR
908 // inputs. We can reserve those and use them directly.
909
910 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
911 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
912 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
913
914 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
915 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
916 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
917 } else {
918 unsigned ReservedBufferReg
919 = TRI->reservedPrivateSegmentBufferReg(MF);
920 unsigned ReservedOffsetReg
921 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
922
923 // We tentatively reserve the last registers (skipping the last two
924 // which may contain VCC). After register allocation, we'll replace
925 // these with the ones immediately after those which were really
926 // allocated. In the prologue copies will be inserted from the argument
927 // to these reserved registers.
928 Info->setScratchRSrcReg(ReservedBufferReg);
929 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
930 }
931 } else {
932 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
933
934 // Without HSA, relocations are used for the scratch pointer and the
935 // buffer resource setup is always inserted in the prologue. Scratch wave
936 // offset is still in an input SGPR.
937 Info->setScratchRSrcReg(ReservedBufferReg);
938
939 if (HasStackObjects) {
940 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
941 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
942 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
943 } else {
944 unsigned ReservedOffsetReg
945 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
946 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
947 }
948 }
949
950 if (Info->hasWorkItemIDX()) {
951 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
952 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
953 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000954 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000955
956 if (Info->hasWorkItemIDY()) {
957 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
958 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
959 CCInfo.AllocateReg(Reg);
960 }
961
962 if (Info->hasWorkItemIDZ()) {
963 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
964 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
965 CCInfo.AllocateReg(Reg);
966 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000967
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000968 if (Chains.empty())
969 return Chain;
970
971 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000972}
973
Marek Olsak8a0f3352016-01-13 17:23:04 +0000974SDValue SITargetLowering::LowerReturn(SDValue Chain,
975 CallingConv::ID CallConv,
976 bool isVarArg,
977 const SmallVectorImpl<ISD::OutputArg> &Outs,
978 const SmallVectorImpl<SDValue> &OutVals,
979 SDLoc DL, SelectionDAG &DAG) const {
980 MachineFunction &MF = DAG.getMachineFunction();
981 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
982
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000983 if (!AMDGPU::isShader(CallConv))
Marek Olsak8a0f3352016-01-13 17:23:04 +0000984 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
985 OutVals, DL, DAG);
986
Marek Olsak8e9cc632016-01-13 17:23:09 +0000987 Info->setIfReturnsVoid(Outs.size() == 0);
988
Marek Olsak8a0f3352016-01-13 17:23:04 +0000989 SmallVector<ISD::OutputArg, 48> Splits;
990 SmallVector<SDValue, 48> SplitVals;
991
992 // Split vectors into their elements.
993 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
994 const ISD::OutputArg &Out = Outs[i];
995
996 if (Out.VT.isVector()) {
997 MVT VT = Out.VT.getVectorElementType();
998 ISD::OutputArg NewOut = Out;
999 NewOut.Flags.setSplit();
1000 NewOut.VT = VT;
1001
1002 // We want the original number of vector elements here, e.g.
1003 // three or five, not four or eight.
1004 unsigned NumElements = Out.ArgVT.getVectorNumElements();
1005
1006 for (unsigned j = 0; j != NumElements; ++j) {
1007 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
1008 DAG.getConstant(j, DL, MVT::i32));
1009 SplitVals.push_back(Elem);
1010 Splits.push_back(NewOut);
1011 NewOut.PartOffset += NewOut.VT.getStoreSize();
1012 }
1013 } else {
1014 SplitVals.push_back(OutVals[i]);
1015 Splits.push_back(Out);
1016 }
1017 }
1018
1019 // CCValAssign - represent the assignment of the return value to a location.
1020 SmallVector<CCValAssign, 48> RVLocs;
1021
1022 // CCState - Info about the registers and stack slots.
1023 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1024 *DAG.getContext());
1025
1026 // Analyze outgoing return values.
1027 AnalyzeReturn(CCInfo, Splits);
1028
1029 SDValue Flag;
1030 SmallVector<SDValue, 48> RetOps;
1031 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1032
1033 // Copy the result values into the output registers.
1034 for (unsigned i = 0, realRVLocIdx = 0;
1035 i != RVLocs.size();
1036 ++i, ++realRVLocIdx) {
1037 CCValAssign &VA = RVLocs[i];
1038 assert(VA.isRegLoc() && "Can only return in registers!");
1039
1040 SDValue Arg = SplitVals[realRVLocIdx];
1041
1042 // Copied from other backends.
1043 switch (VA.getLocInfo()) {
1044 default: llvm_unreachable("Unknown loc info!");
1045 case CCValAssign::Full:
1046 break;
1047 case CCValAssign::BCvt:
1048 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1049 break;
1050 }
1051
1052 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
1053 Flag = Chain.getValue(1);
1054 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1055 }
1056
1057 // Update chain and glue.
1058 RetOps[0] = Chain;
1059 if (Flag.getNode())
1060 RetOps.push_back(Flag);
1061
1062 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, RetOps);
1063}
1064
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001065unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1066 SelectionDAG &DAG) const {
1067 unsigned Reg = StringSwitch<unsigned>(RegName)
1068 .Case("m0", AMDGPU::M0)
1069 .Case("exec", AMDGPU::EXEC)
1070 .Case("exec_lo", AMDGPU::EXEC_LO)
1071 .Case("exec_hi", AMDGPU::EXEC_HI)
1072 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1073 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1074 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1075 .Default(AMDGPU::NoRegister);
1076
1077 if (Reg == AMDGPU::NoRegister) {
1078 report_fatal_error(Twine("invalid register name \""
1079 + StringRef(RegName) + "\"."));
1080
1081 }
1082
1083 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
1084 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1085 report_fatal_error(Twine("invalid register \""
1086 + StringRef(RegName) + "\" for subtarget."));
1087 }
1088
1089 switch (Reg) {
1090 case AMDGPU::M0:
1091 case AMDGPU::EXEC_LO:
1092 case AMDGPU::EXEC_HI:
1093 case AMDGPU::FLAT_SCR_LO:
1094 case AMDGPU::FLAT_SCR_HI:
1095 if (VT.getSizeInBits() == 32)
1096 return Reg;
1097 break;
1098 case AMDGPU::EXEC:
1099 case AMDGPU::FLAT_SCR:
1100 if (VT.getSizeInBits() == 64)
1101 return Reg;
1102 break;
1103 default:
1104 llvm_unreachable("missing register type checking");
1105 }
1106
1107 report_fatal_error(Twine("invalid type for register \""
1108 + StringRef(RegName) + "\"."));
1109}
1110
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001111MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
1112 MachineInstr *MI, MachineBasicBlock *BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +00001113 switch (MI->getOpcode()) {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001114 case AMDGPU::SI_INIT_M0: {
1115 const SIInstrInfo *TII =
1116 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
1117 BuildMI(*BB, MI->getIterator(), MI->getDebugLoc(),
1118 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1119 .addOperand(MI->getOperand(0));
1120 MI->eraseFromParent();
1121 break;
1122 }
Matt Arsenault20711b72015-02-20 22:10:45 +00001123 case AMDGPU::BRANCH:
1124 return BB;
Changpeng Fang01f60622016-03-15 17:28:44 +00001125 case AMDGPU::GET_GROUPSTATICSIZE: {
1126 const SIInstrInfo *TII =
1127 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
1128 MachineFunction *MF = BB->getParent();
1129 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1130 DebugLoc DL = MI->getDebugLoc();
1131 BuildMI (*BB, MI, DL, TII->get(AMDGPU::S_MOVK_I32))
1132 .addOperand(MI->getOperand(0))
1133 .addImm(MFI->LDSSize);
1134 MI->eraseFromParent();
1135 return BB;
1136 }
1137 default:
1138 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001139 }
1140 return BB;
1141}
1142
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001143bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1144 // This currently forces unfolding various combinations of fsub into fma with
1145 // free fneg'd operands. As long as we have fast FMA (controlled by
1146 // isFMAFasterThanFMulAndFAdd), we should perform these.
1147
1148 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1149 // most of these combines appear to be cycle neutral but save on instruction
1150 // count / code size.
1151 return true;
1152}
1153
Mehdi Amini44ede332015-07-09 02:09:04 +00001154EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1155 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001156 if (!VT.isVector()) {
1157 return MVT::i1;
1158 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001159 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001160}
1161
Mehdi Aminieaabc512015-07-09 15:12:23 +00001162MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001163 return MVT::i32;
1164}
1165
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001166// Answering this is somewhat tricky and depends on the specific device which
1167// have different rates for fma or all f64 operations.
1168//
1169// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1170// regardless of which device (although the number of cycles differs between
1171// devices), so it is always profitable for f64.
1172//
1173// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1174// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1175// which we can always do even without fused FP ops since it returns the same
1176// result as the separate operations and since it is always full
1177// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1178// however does not support denormals, so we do report fma as faster if we have
1179// a fast fma device and require denormals.
1180//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001181bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1182 VT = VT.getScalarType();
1183
1184 if (!VT.isSimple())
1185 return false;
1186
1187 switch (VT.getSimpleVT().SimpleTy) {
1188 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001189 // This is as fast on some subtargets. However, we always have full rate f32
1190 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001191 // which we should prefer over fma. We can't use this if we want to support
1192 // denormals, so only report this in these cases.
1193 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001194 case MVT::f64:
1195 return true;
1196 default:
1197 break;
1198 }
1199
1200 return false;
1201}
1202
Tom Stellard75aadc22012-12-11 21:25:42 +00001203//===----------------------------------------------------------------------===//
1204// Custom DAG Lowering Operations
1205//===----------------------------------------------------------------------===//
1206
1207SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1208 switch (Op.getOpcode()) {
1209 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001210 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001211 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001212 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001213 SDValue Result = LowerLOAD(Op, DAG);
1214 assert((!Result.getNode() ||
1215 Result.getNode()->getNumValues() == 2) &&
1216 "Load should return a value and a chain");
1217 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001218 }
Tom Stellardaf775432013-10-23 00:44:32 +00001219
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001220 case ISD::FSIN:
1221 case ISD::FCOS:
1222 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001223 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001224 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001225 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001226 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001227 case ISD::GlobalAddress: {
1228 MachineFunction &MF = DAG.getMachineFunction();
1229 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1230 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001231 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001232 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001233 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001234 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001235 }
1236 return SDValue();
1237}
1238
Tom Stellardf8794352012-12-19 22:10:31 +00001239/// \brief Helper function for LowerBRCOND
1240static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001241
Tom Stellardf8794352012-12-19 22:10:31 +00001242 SDNode *Parent = Value.getNode();
1243 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1244 I != E; ++I) {
1245
1246 if (I.getUse().get() != Value)
1247 continue;
1248
1249 if (I->getOpcode() == Opcode)
1250 return *I;
1251 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001252 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001253}
1254
Tom Stellardb02094e2014-07-21 15:45:01 +00001255SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
1256
Tom Stellardc98ee202015-07-16 19:40:07 +00001257 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +00001258 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
1259 unsigned FrameIndex = FINode->getIndex();
1260
Matt Arsenault3a619852016-02-27 20:26:57 +00001261 // A FrameIndex node represents a 32-bit offset into scratch memory. If the
1262 // high bit of a frame index offset were to be set, this would mean that it
1263 // represented an offset of ~2GB * 64 = ~128GB from the start of the scratch
1264 // buffer, with 64 being the number of threads per wave.
Tom Stellardc98ee202015-07-16 19:40:07 +00001265 //
Matt Arsenault3a619852016-02-27 20:26:57 +00001266 // The maximum private allocation for the entire GPU is 4G, and we are
1267 // concerned with the largest the index could ever be for an individual
1268 // workitem. This will occur with the minmum dispatch size. If a program
1269 // requires more, the dispatch size will be reduced.
1270 //
1271 // With this limit, we can mark the high bit of the FrameIndex node as known
1272 // zero, which is important, because it means in most situations we can prove
1273 // that values derived from FrameIndex nodes are non-negative. This enables us
1274 // to take advantage of more addressing modes when accessing scratch buffers,
1275 // since for scratch reads/writes, the register offset must always be
1276 // positive.
1277
1278 uint64_t MaxGPUAlloc = UINT64_C(4) * 1024 * 1024 * 1024;
1279
1280 // XXX - It is unclear if partial dispatch works. Assume it works at half wave
1281 // granularity. It is probably a full wave.
1282 uint64_t MinGranularity = 32;
1283
1284 unsigned KnownBits = Log2_64(MaxGPUAlloc / MinGranularity);
1285 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), KnownBits);
Tom Stellardc98ee202015-07-16 19:40:07 +00001286
1287 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
Tom Stellardc98ee202015-07-16 19:40:07 +00001288 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
Matt Arsenault3a619852016-02-27 20:26:57 +00001289 DAG.getValueType(ExtVT));
Tom Stellardb02094e2014-07-21 15:45:01 +00001290}
1291
Tom Stellardbc4497b2016-02-12 23:45:29 +00001292bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault16f48d72016-02-13 00:36:10 +00001293 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN)
Tom Stellardbc4497b2016-02-12 23:45:29 +00001294 return false;
1295
1296 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1297 default: return false;
1298 case AMDGPUIntrinsic::amdgcn_if:
1299 case AMDGPUIntrinsic::amdgcn_else:
1300 case AMDGPUIntrinsic::amdgcn_break:
1301 case AMDGPUIntrinsic::amdgcn_if_break:
1302 case AMDGPUIntrinsic::amdgcn_else_break:
1303 case AMDGPUIntrinsic::amdgcn_loop:
1304 case AMDGPUIntrinsic::amdgcn_end_cf:
1305 return true;
1306 }
1307}
1308
Tom Stellardf8794352012-12-19 22:10:31 +00001309/// This transforms the control flow intrinsics to get the branch destination as
1310/// last parameter, also switches branch target with BR if the need arise
1311SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1312 SelectionDAG &DAG) const {
1313
Andrew Trickef9de2a2013-05-25 02:42:55 +00001314 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001315
1316 SDNode *Intr = BRCOND.getOperand(1).getNode();
1317 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001318 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001319 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001320
1321 if (Intr->getOpcode() == ISD::SETCC) {
1322 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001323 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001324 Intr = SetCC->getOperand(0).getNode();
1325
1326 } else {
1327 // Get the target from BR if we don't negate the condition
1328 BR = findUser(BRCOND, ISD::BR);
1329 Target = BR->getOperand(1);
1330 }
1331
Tom Stellardbc4497b2016-02-12 23:45:29 +00001332 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN) {
1333 // This is a uniform branch so we don't need to legalize.
1334 return BRCOND;
1335 }
1336
1337 assert(!SetCC ||
1338 (SetCC->getConstantOperandVal(1) == 1 &&
1339 isCFIntrinsic(Intr) &&
1340 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1341 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001342
1343 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001344 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001345
1346 // operands of the new intrinsic call
1347 SmallVector<SDValue, 4> Ops;
1348 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001349 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001350 Ops.push_back(Target);
1351
1352 // build the new intrinsic call
1353 SDNode *Result = DAG.getNode(
1354 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001355 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001356
1357 if (BR) {
1358 // Give the branch instruction our target
1359 SDValue Ops[] = {
1360 BR->getOperand(0),
1361 BRCOND.getOperand(2)
1362 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001363 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1364 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1365 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001366 }
1367
1368 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1369
1370 // Copy the intrinsic results to registers
1371 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1372 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1373 if (!CopyToReg)
1374 continue;
1375
1376 Chain = DAG.getCopyToReg(
1377 Chain, DL,
1378 CopyToReg->getOperand(1),
1379 SDValue(Result, i - 1),
1380 SDValue());
1381
1382 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1383 }
1384
1385 // Remove the old intrinsic from the chain
1386 DAG.ReplaceAllUsesOfValueWith(
1387 SDValue(Intr, Intr->getNumValues() - 1),
1388 Intr->getOperand(0));
1389
1390 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001391}
1392
Tom Stellard067c8152014-07-21 14:01:14 +00001393SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1394 SDValue Op,
1395 SelectionDAG &DAG) const {
1396 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1397
1398 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
1399 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1400
1401 SDLoc DL(GSD);
1402 const GlobalValue *GV = GSD->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001403 MVT PtrVT = getPointerTy(DAG.getDataLayout(), GSD->getAddressSpace());
Tom Stellard067c8152014-07-21 14:01:14 +00001404
Tom Stellard067c8152014-07-21 14:01:14 +00001405 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
Tom Stellardc93fc112015-12-10 02:13:01 +00001406 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT, GA);
Tom Stellard067c8152014-07-21 14:01:14 +00001407}
1408
Tom Stellardfc92e772015-05-12 14:18:14 +00001409SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL,
1410 SDValue V) const {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001411 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
1412 // the destination register.
1413 //
Tom Stellardfc92e772015-05-12 14:18:14 +00001414 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1415 // so we will end up with redundant moves to m0.
1416 //
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001417 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
1418
1419 // A Null SDValue creates a glue result.
1420 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
1421 V, Chain);
1422 return SDValue(M0, 0);
Tom Stellardfc92e772015-05-12 14:18:14 +00001423}
1424
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001425SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1426 SDValue Op,
1427 MVT VT,
1428 unsigned Offset) const {
1429 SDLoc SL(Op);
1430 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1431 DAG.getEntryNode(), Offset, false);
1432 // The local size values will have the hi 16-bits as zero.
1433 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1434 DAG.getValueType(VT));
1435}
1436
Matt Arsenaulte0132462016-01-30 05:19:45 +00001437static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, EVT VT) {
1438 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
1439 "non-hsa intrinsic with hsa target");
1440 DAG.getContext()->diagnose(BadIntrin);
1441 return DAG.getUNDEF(VT);
1442}
1443
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001444SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1445 SelectionDAG &DAG) const {
1446 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001447 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardec2e43c2014-09-22 15:35:29 +00001448 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +00001449 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001450
1451 EVT VT = Op.getValueType();
1452 SDLoc DL(Op);
1453 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1454
Sanjay Patela2607012015-09-16 16:31:21 +00001455 // TODO: Should this propagate fast-math-flags?
1456
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001457 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001458 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault48ab5262016-04-25 19:27:18 +00001459 case Intrinsic::amdgcn_queue_ptr: {
Matt Arsenault800fecf2016-01-11 21:18:33 +00001460 if (!Subtarget->isAmdHsaOS()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00001461 DiagnosticInfoUnsupported BadIntrin(
1462 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
1463 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00001464 DAG.getContext()->diagnose(BadIntrin);
1465 return DAG.getUNDEF(VT);
1466 }
1467
Matt Arsenault48ab5262016-04-25 19:27:18 +00001468 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
1469 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00001470 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
Matt Arsenault48ab5262016-04-25 19:27:18 +00001471 TRI->getPreloadedValue(MF, Reg), VT);
1472 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001473 case Intrinsic::amdgcn_rcp:
1474 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
1475 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001476 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001477 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenault79963e82016-02-13 01:03:00 +00001478 case Intrinsic::amdgcn_rsq_clamp:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001479 case AMDGPUIntrinsic::AMDGPU_rsq_clamped: { // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001480 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00001481 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00001482
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001483 Type *Type = VT.getTypeForEVT(*DAG.getContext());
1484 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
1485 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
1486
1487 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
1488 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
1489 DAG.getConstantFP(Max, DL, VT));
1490 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
1491 DAG.getConstantFP(Min, DL, VT));
1492 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001493 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001494 if (Subtarget->isAmdHsaOS())
1495 return emitNonHSAIntrinsicError(DAG, VT);
1496
Tom Stellardec2e43c2014-09-22 15:35:29 +00001497 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1498 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001499 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001500 if (Subtarget->isAmdHsaOS())
1501 return emitNonHSAIntrinsicError(DAG, VT);
1502
Tom Stellardec2e43c2014-09-22 15:35:29 +00001503 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1504 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001505 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001506 if (Subtarget->isAmdHsaOS())
1507 return emitNonHSAIntrinsicError(DAG, VT);
1508
Tom Stellardec2e43c2014-09-22 15:35:29 +00001509 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1510 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001511 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001512 if (Subtarget->isAmdHsaOS())
1513 return emitNonHSAIntrinsicError(DAG, VT);
1514
Tom Stellardec2e43c2014-09-22 15:35:29 +00001515 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1516 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001517 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001518 if (Subtarget->isAmdHsaOS())
1519 return emitNonHSAIntrinsicError(DAG, VT);
1520
Tom Stellardec2e43c2014-09-22 15:35:29 +00001521 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1522 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001523 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001524 if (Subtarget->isAmdHsaOS())
1525 return emitNonHSAIntrinsicError(DAG, VT);
1526
Tom Stellardec2e43c2014-09-22 15:35:29 +00001527 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1528 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001529 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001530 if (Subtarget->isAmdHsaOS())
1531 return emitNonHSAIntrinsicError(DAG, VT);
1532
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001533 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1534 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001535 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001536 if (Subtarget->isAmdHsaOS())
1537 return emitNonHSAIntrinsicError(DAG, VT);
1538
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001539 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1540 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001541 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001542 if (Subtarget->isAmdHsaOS())
1543 return emitNonHSAIntrinsicError(DAG, VT);
1544
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001545 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1546 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenaultbef34e22016-01-22 21:30:34 +00001547 case Intrinsic::amdgcn_read_workdim:
1548 case AMDGPUIntrinsic::AMDGPU_read_workdim: // Legacy name.
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001549 // Really only 2 bits.
1550 return lowerImplicitZextParam(DAG, Op, MVT::i8,
1551 getImplicitParameterOffset(MFI, GRID_DIM));
Matt Arsenault43976df2016-01-30 04:25:19 +00001552 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001553 case Intrinsic::r600_read_tgid_x:
1554 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001555 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001556 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001557 case Intrinsic::r600_read_tgid_y:
1558 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001559 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001560 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001561 case Intrinsic::r600_read_tgid_z:
1562 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001563 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001564 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001565 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001566 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001567 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001568 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001569 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001570 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001571 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001572 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001573 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001574 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001575 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001576 case AMDGPUIntrinsic::SI_load_const: {
1577 SDValue Ops[] = {
1578 Op.getOperand(1),
1579 Op.getOperand(2)
1580 };
1581
1582 MachineMemOperand *MMO = MF.getMachineMemOperand(
1583 MachinePointerInfo(),
1584 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
1585 VT.getStoreSize(), 4);
1586 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
1587 Op->getVTList(), Ops, VT, MMO);
1588 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001589 case AMDGPUIntrinsic::SI_vs_load_input:
1590 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1591 Op.getOperand(1),
1592 Op.getOperand(2),
1593 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00001594
Tom Stellard2a9d9472015-05-12 15:00:46 +00001595 case AMDGPUIntrinsic::SI_fs_constant: {
1596 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1597 SDValue Glue = M0.getValue(1);
1598 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
1599 DAG.getConstant(2, DL, MVT::i32), // P0
1600 Op.getOperand(1), Op.getOperand(2), Glue);
1601 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00001602 case AMDGPUIntrinsic::SI_packf16:
1603 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
1604 return DAG.getUNDEF(MVT::i32);
1605 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00001606 case AMDGPUIntrinsic::SI_fs_interp: {
1607 SDValue IJ = Op.getOperand(4);
1608 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1609 DAG.getConstant(0, DL, MVT::i32));
1610 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1611 DAG.getConstant(1, DL, MVT::i32));
1612 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1613 SDValue Glue = M0.getValue(1);
1614 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
1615 DAG.getVTList(MVT::f32, MVT::Glue),
1616 I, Op.getOperand(1), Op.getOperand(2), Glue);
1617 Glue = SDValue(P1.getNode(), 1);
1618 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
1619 Op.getOperand(1), Op.getOperand(2), Glue);
1620 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00001621 case Intrinsic::amdgcn_interp_p1: {
1622 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
1623 SDValue Glue = M0.getValue(1);
1624 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
1625 Op.getOperand(2), Op.getOperand(3), Glue);
1626 }
1627 case Intrinsic::amdgcn_interp_p2: {
1628 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
1629 SDValue Glue = SDValue(M0.getNode(), 1);
1630 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
1631 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
1632 Glue);
1633 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00001634 case Intrinsic::amdgcn_sin:
1635 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
1636
1637 case Intrinsic::amdgcn_cos:
1638 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
1639
1640 case Intrinsic::amdgcn_log_clamp: {
1641 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
1642 return SDValue();
1643
1644 DiagnosticInfoUnsupported BadIntrin(
1645 *MF.getFunction(), "intrinsic not supported on subtarget",
1646 DL.getDebugLoc());
1647 DAG.getContext()->diagnose(BadIntrin);
1648 return DAG.getUNDEF(VT);
1649 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001650 case Intrinsic::amdgcn_ldexp:
1651 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
1652 Op.getOperand(1), Op.getOperand(2));
1653 case Intrinsic::amdgcn_class:
1654 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
1655 Op.getOperand(1), Op.getOperand(2));
1656 case Intrinsic::amdgcn_div_fmas:
1657 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
1658 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
1659 Op.getOperand(4));
1660
1661 case Intrinsic::amdgcn_div_fixup:
1662 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
1663 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
1664
1665 case Intrinsic::amdgcn_trig_preop:
1666 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
1667 Op.getOperand(1), Op.getOperand(2));
1668 case Intrinsic::amdgcn_div_scale: {
1669 // 3rd parameter required to be a constant.
1670 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1671 if (!Param)
1672 return DAG.getUNDEF(VT);
1673
1674 // Translate to the operands expected by the machine instruction. The
1675 // first parameter must be the same as the first instruction.
1676 SDValue Numerator = Op.getOperand(1);
1677 SDValue Denominator = Op.getOperand(2);
1678
1679 // Note this order is opposite of the machine instruction's operations,
1680 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
1681 // intrinsic has the numerator as the first operand to match a normal
1682 // division operation.
1683
1684 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
1685
1686 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
1687 Denominator, Numerator);
1688 }
1689 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte0:
1690 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Op.getOperand(1));
1691 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte1:
1692 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE1, DL, VT, Op.getOperand(1));
1693 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte2:
1694 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE2, DL, VT, Op.getOperand(1));
1695 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte3:
1696 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE3, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001697 default:
1698 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1699 }
1700}
1701
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001702SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
1703 SelectionDAG &DAG) const {
1704 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1705 switch (IntrID) {
1706 case Intrinsic::amdgcn_atomic_inc:
1707 case Intrinsic::amdgcn_atomic_dec: {
1708 MemSDNode *M = cast<MemSDNode>(Op);
1709 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
1710 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
1711 SDValue Ops[] = {
1712 M->getOperand(0), // Chain
1713 M->getOperand(2), // Ptr
1714 M->getOperand(3) // Value
1715 };
1716
1717 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
1718 M->getMemoryVT(), M->getMemOperand());
1719 }
1720 default:
1721 return SDValue();
1722 }
1723}
1724
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001725SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1726 SelectionDAG &DAG) const {
1727 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00001728 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001729 SDValue Chain = Op.getOperand(0);
1730 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1731
1732 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00001733 case AMDGPUIntrinsic::SI_sendmsg: {
1734 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1735 SDValue Glue = Chain.getValue(1);
1736 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1737 Op.getOperand(2), Glue);
1738 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001739 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001740 SDValue Ops[] = {
1741 Chain,
1742 Op.getOperand(2),
1743 Op.getOperand(3),
1744 Op.getOperand(4),
1745 Op.getOperand(5),
1746 Op.getOperand(6),
1747 Op.getOperand(7),
1748 Op.getOperand(8),
1749 Op.getOperand(9),
1750 Op.getOperand(10),
1751 Op.getOperand(11),
1752 Op.getOperand(12),
1753 Op.getOperand(13),
1754 Op.getOperand(14)
1755 };
1756
1757 EVT VT = Op.getOperand(3).getValueType();
1758
1759 MachineMemOperand *MMO = MF.getMachineMemOperand(
1760 MachinePointerInfo(),
1761 MachineMemOperand::MOStore,
1762 VT.getStoreSize(), 4);
1763 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1764 Op->getVTList(), Ops, VT, MMO);
1765 }
1766 default:
1767 return SDValue();
1768 }
1769}
1770
Tom Stellard81d871d2013-11-13 23:36:50 +00001771SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1772 SDLoc DL(Op);
1773 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001774 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00001775 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00001776
Matt Arsenaulta1436412016-02-10 18:21:45 +00001777 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
1778 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected");
Matt Arsenault6dfda962016-02-10 18:21:39 +00001779 // FIXME: Copied from PPC
1780 // First, load into 32 bits, then truncate to 1 bit.
1781
1782 SDValue Chain = Load->getChain();
1783 SDValue BasePtr = Load->getBasePtr();
1784 MachineMemOperand *MMO = Load->getMemOperand();
1785
1786 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
1787 BasePtr, MVT::i8, MMO);
1788
1789 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00001790 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00001791 NewLD.getValue(1)
1792 };
1793
1794 return DAG.getMergeValues(Ops, DL);
1795 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001796
Matt Arsenaulta1436412016-02-10 18:21:45 +00001797 if (!MemVT.isVector())
1798 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001799
Matt Arsenaulta1436412016-02-10 18:21:45 +00001800 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1801 "Custom lowering for non-i32 vectors hasn't been implemented.");
1802 unsigned NumElements = MemVT.getVectorNumElements();
1803 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001804
Matt Arsenaulta1436412016-02-10 18:21:45 +00001805 switch (Load->getAddressSpace()) {
1806 case AMDGPUAS::CONSTANT_ADDRESS:
1807 if (isMemOpUniform(Load))
1808 return SDValue();
1809 // Non-uniform loads will be selected to MUBUF instructions, so they
1810 // have the same legalization requires ments as global and private
1811 // loads.
1812 //
1813 // Fall-through
1814 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001815 case AMDGPUAS::FLAT_ADDRESS:
1816 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00001817 return SplitVectorLoad(Op, DAG);
1818 // v4 loads are supported for private and global memory.
1819 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001820 case AMDGPUAS::PRIVATE_ADDRESS: {
1821 // Depending on the setting of the private_element_size field in the
1822 // resource descriptor, we can only make private accesses up to a certain
1823 // size.
1824 switch (Subtarget->getMaxPrivateElementSize()) {
1825 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00001826 return scalarizeVectorLoad(Load, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001827 case 8:
1828 if (NumElements > 2)
1829 return SplitVectorLoad(Op, DAG);
1830 return SDValue();
1831 case 16:
1832 // Same as global/flat
1833 if (NumElements > 4)
1834 return SplitVectorLoad(Op, DAG);
1835 return SDValue();
1836 default:
1837 llvm_unreachable("unsupported private_element_size");
1838 }
1839 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00001840 case AMDGPUAS::LOCAL_ADDRESS:
1841 // If properly aligned, if we split we might be able to use ds_read_b64.
1842 return SplitVectorLoad(Op, DAG);
1843 default:
1844 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00001845 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001846}
1847
Tom Stellard0ec134f2014-02-04 17:18:40 +00001848SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1849 if (Op.getValueType() != MVT::i64)
1850 return SDValue();
1851
1852 SDLoc DL(Op);
1853 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001854
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001855 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1856 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001857
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001858 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1859 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1860
1861 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1862 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001863
1864 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1865
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001866 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1867 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001868
1869 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1870
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001871 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1872 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001873}
1874
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001875// Catch division cases where we can use shortcuts with rcp and rsq
1876// instructions.
1877SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001878 SDLoc SL(Op);
1879 SDValue LHS = Op.getOperand(0);
1880 SDValue RHS = Op.getOperand(1);
1881 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001882 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001883
1884 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001885 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1886 CLHS->isExactlyValue(1.0)) {
1887 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1888 // the CI documentation has a worst case error of 1 ulp.
1889 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1890 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001891
1892 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001893 //
1894 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1895 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001896 if (RHS.getOpcode() == ISD::FSQRT)
1897 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1898
1899 // 1.0 / x -> rcp(x)
1900 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1901 }
1902 }
1903
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001904 if (Unsafe) {
1905 // Turn into multiply by the reciprocal.
1906 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00001907 SDNodeFlags Flags;
1908 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001909 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00001910 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001911 }
1912
1913 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001914}
1915
1916SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001917 if (SDValue FastLowered = LowerFastFDIV(Op, DAG))
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001918 return FastLowered;
1919
1920 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1921 // selection error for now rather than do something incorrect.
1922 if (Subtarget->hasFP32Denormals())
1923 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001924
1925 SDLoc SL(Op);
1926 SDValue LHS = Op.getOperand(0);
1927 SDValue RHS = Op.getOperand(1);
1928
1929 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1930
1931 const APFloat K0Val(BitsToFloat(0x6f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001932 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001933
1934 const APFloat K1Val(BitsToFloat(0x2f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001935 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001936
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001937 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001938
Mehdi Amini44ede332015-07-09 02:09:04 +00001939 EVT SetCCVT =
1940 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001941
1942 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1943
1944 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1945
Sanjay Patela2607012015-09-16 16:31:21 +00001946 // TODO: Should this propagate fast-math-flags?
1947
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001948 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1949
1950 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1951
1952 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1953
1954 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1955}
1956
1957SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001958 if (DAG.getTarget().Options.UnsafeFPMath)
1959 return LowerFastFDIV(Op, DAG);
1960
1961 SDLoc SL(Op);
1962 SDValue X = Op.getOperand(0);
1963 SDValue Y = Op.getOperand(1);
1964
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001965 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001966
1967 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
1968
1969 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
1970
1971 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
1972
1973 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
1974
1975 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
1976
1977 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
1978
1979 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
1980
1981 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
1982
1983 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
1984 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
1985
1986 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
1987 NegDivScale0, Mul, DivScale1);
1988
1989 SDValue Scale;
1990
1991 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1992 // Workaround a hardware bug on SI where the condition output from div_scale
1993 // is not usable.
1994
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001995 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001996
1997 // Figure out if the scale to use for div_fmas.
1998 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1999 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
2000 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
2001 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
2002
2003 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
2004 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
2005
2006 SDValue Scale0Hi
2007 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
2008 SDValue Scale1Hi
2009 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
2010
2011 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
2012 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
2013 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
2014 } else {
2015 Scale = DivScale1.getValue(1);
2016 }
2017
2018 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
2019 Fma4, Fma3, Mul, Scale);
2020
2021 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002022}
2023
2024SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
2025 EVT VT = Op.getValueType();
2026
2027 if (VT == MVT::f32)
2028 return LowerFDIV32(Op, DAG);
2029
2030 if (VT == MVT::f64)
2031 return LowerFDIV64(Op, DAG);
2032
2033 llvm_unreachable("Unexpected type for fdiv");
2034}
2035
Tom Stellard81d871d2013-11-13 23:36:50 +00002036SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2037 SDLoc DL(Op);
2038 StoreSDNode *Store = cast<StoreSDNode>(Op);
2039 EVT VT = Store->getMemoryVT();
2040
Matt Arsenault95245662016-02-11 05:32:46 +00002041 if (VT == MVT::i1) {
2042 return DAG.getTruncStore(Store->getChain(), DL,
2043 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
2044 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00002045 }
2046
Matt Arsenault95245662016-02-11 05:32:46 +00002047 assert(Store->getValue().getValueType().getScalarType() == MVT::i32);
Tom Stellard81d871d2013-11-13 23:36:50 +00002048
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002049 unsigned NumElements = VT.getVectorNumElements();
2050 switch (Store->getAddressSpace()) {
2051 case AMDGPUAS::GLOBAL_ADDRESS:
2052 case AMDGPUAS::FLAT_ADDRESS:
2053 if (NumElements > 4)
2054 return SplitVectorStore(Op, DAG);
2055 return SDValue();
2056 case AMDGPUAS::PRIVATE_ADDRESS: {
2057 switch (Subtarget->getMaxPrivateElementSize()) {
2058 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002059 return scalarizeVectorStore(Store, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002060 case 8:
2061 if (NumElements > 2)
2062 return SplitVectorStore(Op, DAG);
2063 return SDValue();
2064 case 16:
2065 if (NumElements > 4)
2066 return SplitVectorStore(Op, DAG);
2067 return SDValue();
2068 default:
2069 llvm_unreachable("unsupported private_element_size");
2070 }
2071 }
2072 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenault95245662016-02-11 05:32:46 +00002073 // If properly aligned, if we split we might be able to use ds_write_b64.
2074 return SplitVectorStore(Op, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002075 default:
2076 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00002077 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002078}
2079
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002080SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002081 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002082 EVT VT = Op.getValueType();
2083 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00002084 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002085 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
2086 DAG.getNode(ISD::FMUL, DL, VT, Arg,
2087 DAG.getConstantFP(0.5/M_PI, DL,
2088 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002089
2090 switch (Op.getOpcode()) {
2091 case ISD::FCOS:
2092 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
2093 case ISD::FSIN:
2094 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
2095 default:
2096 llvm_unreachable("Wrong trig opcode");
2097 }
2098}
2099
Tom Stellard354a43c2016-04-01 18:27:37 +00002100SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
2101 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
2102 assert(AtomicNode->isCompareAndSwap());
2103 unsigned AS = AtomicNode->getAddressSpace();
2104
2105 // No custom lowering required for local address space
2106 if (!isFlatGlobalAddrSpace(AS))
2107 return Op;
2108
2109 // Non-local address space requires custom lowering for atomic compare
2110 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
2111 SDLoc DL(Op);
2112 SDValue ChainIn = Op.getOperand(0);
2113 SDValue Addr = Op.getOperand(1);
2114 SDValue Old = Op.getOperand(2);
2115 SDValue New = Op.getOperand(3);
2116 EVT VT = Op.getValueType();
2117 MVT SimpleVT = VT.getSimpleVT();
2118 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
2119
2120 SDValue NewOld = DAG.getNode(ISD::BUILD_VECTOR, DL, VecType,
2121 New, Old);
2122 SDValue Ops[] = { ChainIn, Addr, NewOld };
2123 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2124 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL,
2125 VTList, Ops, VT, AtomicNode->getMemOperand());
2126}
2127
Tom Stellard75aadc22012-12-11 21:25:42 +00002128//===----------------------------------------------------------------------===//
2129// Custom DAG optimizations
2130//===----------------------------------------------------------------------===//
2131
Matt Arsenault364a6742014-06-11 17:50:44 +00002132SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00002133 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00002134 EVT VT = N->getValueType(0);
2135 EVT ScalarVT = VT.getScalarType();
2136 if (ScalarVT != MVT::f32)
2137 return SDValue();
2138
2139 SelectionDAG &DAG = DCI.DAG;
2140 SDLoc DL(N);
2141
2142 SDValue Src = N->getOperand(0);
2143 EVT SrcVT = Src.getValueType();
2144
2145 // TODO: We could try to match extracting the higher bytes, which would be
2146 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
2147 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
2148 // about in practice.
2149 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
2150 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
2151 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
2152 DCI.AddToWorklist(Cvt.getNode());
2153 return Cvt;
2154 }
2155 }
2156
2157 // We are primarily trying to catch operations on illegal vector types
2158 // before they are expanded.
2159 // For scalars, we can use the more flexible method of checking masked bits
2160 // after legalization.
2161 if (!DCI.isBeforeLegalize() ||
2162 !SrcVT.isVector() ||
2163 SrcVT.getVectorElementType() != MVT::i8) {
2164 return SDValue();
2165 }
2166
2167 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
2168
2169 // Weird sized vectors are a pain to handle, but we know 3 is really the same
2170 // size as 4.
2171 unsigned NElts = SrcVT.getVectorNumElements();
2172 if (!SrcVT.isSimple() && NElts != 3)
2173 return SDValue();
2174
2175 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
2176 // prevent a mess from expanding to v4i32 and repacking.
2177 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
2178 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
2179 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
2180 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00002181 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00002182
2183 unsigned AS = Load->getAddressSpace();
2184 unsigned Align = Load->getAlignment();
2185 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +00002186 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
Matt Arsenaulte6986632015-01-14 01:35:22 +00002187
2188 // Don't try to replace the load if we have to expand it due to alignment
2189 // problems. Otherwise we will end up scalarizing the load, and trying to
2190 // repack into the vector for no real reason.
2191 if (Align < ABIAlignment &&
2192 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
2193 return SDValue();
2194 }
2195
Matt Arsenault364a6742014-06-11 17:50:44 +00002196 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
2197 Load->getChain(),
2198 Load->getBasePtr(),
2199 LoadVT,
2200 Load->getMemOperand());
2201
2202 // Make sure successors of the original load stay after it by updating
2203 // them to use the new Chain.
2204 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
2205
2206 SmallVector<SDValue, 4> Elts;
2207 if (RegVT.isVector())
2208 DAG.ExtractVectorElements(NewLoad, Elts);
2209 else
2210 Elts.push_back(NewLoad);
2211
2212 SmallVector<SDValue, 4> Ops;
2213
2214 unsigned EltIdx = 0;
2215 for (SDValue Elt : Elts) {
2216 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
2217 for (unsigned I = 0; I < ComponentsInElt; ++I) {
2218 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
2219 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
2220 DCI.AddToWorklist(Cvt.getNode());
2221 Ops.push_back(Cvt);
2222 }
2223
2224 ++EltIdx;
2225 }
2226
2227 assert(Ops.size() == NElts);
2228
2229 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
2230 }
2231
2232 return SDValue();
2233}
2234
Eric Christopher6c5b5112015-03-11 18:43:21 +00002235/// \brief Return true if the given offset Size in bytes can be folded into
2236/// the immediate offsets of a memory instruction for the given address space.
2237static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
2238 const AMDGPUSubtarget &STI) {
2239 switch (AS) {
2240 case AMDGPUAS::GLOBAL_ADDRESS: {
2241 // MUBUF instructions a 12-bit offset in bytes.
2242 return isUInt<12>(OffsetSize);
2243 }
2244 case AMDGPUAS::CONSTANT_ADDRESS: {
2245 // SMRD instructions have an 8-bit offset in dwords on SI and
2246 // a 20-bit offset in bytes on VI.
2247 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
2248 return isUInt<20>(OffsetSize);
2249 else
2250 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
2251 }
2252 case AMDGPUAS::LOCAL_ADDRESS:
2253 case AMDGPUAS::REGION_ADDRESS: {
2254 // The single offset versions have a 16-bit offset in bytes.
2255 return isUInt<16>(OffsetSize);
2256 }
2257 case AMDGPUAS::PRIVATE_ADDRESS:
2258 // Indirect register addressing does not use any offsets.
2259 default:
2260 return 0;
2261 }
2262}
2263
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002264// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
2265
2266// This is a variant of
2267// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
2268//
2269// The normal DAG combiner will do this, but only if the add has one use since
2270// that would increase the number of instructions.
2271//
2272// This prevents us from seeing a constant offset that can be folded into a
2273// memory instruction's addressing mode. If we know the resulting add offset of
2274// a pointer can be folded into an addressing offset, we can replace the pointer
2275// operand with the add of new constant offset. This eliminates one of the uses,
2276// and may allow the remaining use to also be simplified.
2277//
2278SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
2279 unsigned AddrSpace,
2280 DAGCombinerInfo &DCI) const {
2281 SDValue N0 = N->getOperand(0);
2282 SDValue N1 = N->getOperand(1);
2283
2284 if (N0.getOpcode() != ISD::ADD)
2285 return SDValue();
2286
2287 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
2288 if (!CN1)
2289 return SDValue();
2290
2291 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2292 if (!CAdd)
2293 return SDValue();
2294
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002295 // If the resulting offset is too large, we can't fold it into the addressing
2296 // mode offset.
2297 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Eric Christopher6c5b5112015-03-11 18:43:21 +00002298 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002299 return SDValue();
2300
2301 SelectionDAG &DAG = DCI.DAG;
2302 SDLoc SL(N);
2303 EVT VT = N->getValueType(0);
2304
2305 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002306 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002307
2308 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
2309}
2310
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002311SDValue SITargetLowering::performAndCombine(SDNode *N,
2312 DAGCombinerInfo &DCI) const {
2313 if (DCI.isBeforeLegalize())
2314 return SDValue();
2315
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002316 if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI))
2317 return Base;
2318
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002319 SelectionDAG &DAG = DCI.DAG;
2320
2321 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
2322 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
2323 SDValue LHS = N->getOperand(0);
2324 SDValue RHS = N->getOperand(1);
2325
2326 if (LHS.getOpcode() == ISD::SETCC &&
2327 RHS.getOpcode() == ISD::SETCC) {
2328 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2329 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2330
2331 SDValue X = LHS.getOperand(0);
2332 SDValue Y = RHS.getOperand(0);
2333 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2334 return SDValue();
2335
2336 if (LCC == ISD::SETO) {
2337 if (X != LHS.getOperand(1))
2338 return SDValue();
2339
2340 if (RCC == ISD::SETUNE) {
2341 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2342 if (!C1 || !C1->isInfinity() || C1->isNegative())
2343 return SDValue();
2344
2345 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2346 SIInstrFlags::N_SUBNORMAL |
2347 SIInstrFlags::N_ZERO |
2348 SIInstrFlags::P_ZERO |
2349 SIInstrFlags::P_SUBNORMAL |
2350 SIInstrFlags::P_NORMAL;
2351
2352 static_assert(((~(SIInstrFlags::S_NAN |
2353 SIInstrFlags::Q_NAN |
2354 SIInstrFlags::N_INFINITY |
2355 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2356 "mask not equal");
2357
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002358 SDLoc DL(N);
2359 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2360 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002361 }
2362 }
2363 }
2364
2365 return SDValue();
2366}
2367
Matt Arsenaultf2290332015-01-06 23:00:39 +00002368SDValue SITargetLowering::performOrCombine(SDNode *N,
2369 DAGCombinerInfo &DCI) const {
2370 SelectionDAG &DAG = DCI.DAG;
2371 SDValue LHS = N->getOperand(0);
2372 SDValue RHS = N->getOperand(1);
2373
Matt Arsenault3b082382016-04-12 18:24:38 +00002374 EVT VT = N->getValueType(0);
2375 if (VT == MVT::i64) {
2376 // TODO: This could be a generic combine with a predicate for extracting the
2377 // high half of an integer being free.
2378
2379 // (or i64:x, (zero_extend i32:y)) ->
2380 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
2381 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
2382 RHS.getOpcode() != ISD::ZERO_EXTEND)
2383 std::swap(LHS, RHS);
2384
2385 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
2386 SDValue ExtSrc = RHS.getOperand(0);
2387 EVT SrcVT = ExtSrc.getValueType();
2388 if (SrcVT == MVT::i32) {
2389 SDLoc SL(N);
2390 SDValue LowLHS, HiBits;
2391 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
2392 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
2393
2394 DCI.AddToWorklist(LowOr.getNode());
2395 DCI.AddToWorklist(HiBits.getNode());
2396
2397 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
2398 LowOr, HiBits);
2399 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2400 }
2401 }
2402 }
2403
Matt Arsenaultf2290332015-01-06 23:00:39 +00002404 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
2405 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
2406 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
2407 SDValue Src = LHS.getOperand(0);
2408 if (Src != RHS.getOperand(0))
2409 return SDValue();
2410
2411 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
2412 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
2413 if (!CLHS || !CRHS)
2414 return SDValue();
2415
2416 // Only 10 bits are used.
2417 static const uint32_t MaxMask = 0x3ff;
2418
2419 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002420 SDLoc DL(N);
2421 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2422 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00002423 }
2424
2425 return SDValue();
2426}
2427
2428SDValue SITargetLowering::performClassCombine(SDNode *N,
2429 DAGCombinerInfo &DCI) const {
2430 SelectionDAG &DAG = DCI.DAG;
2431 SDValue Mask = N->getOperand(1);
2432
2433 // fp_class x, 0 -> false
2434 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
2435 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002436 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002437 }
2438
2439 return SDValue();
2440}
2441
Matt Arsenault9cd90712016-04-14 01:42:16 +00002442// Constant fold canonicalize.
2443SDValue SITargetLowering::performFCanonicalizeCombine(
2444 SDNode *N,
2445 DAGCombinerInfo &DCI) const {
2446 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
2447 if (!CFP)
2448 return SDValue();
2449
2450 SelectionDAG &DAG = DCI.DAG;
2451 const APFloat &C = CFP->getValueAPF();
2452
2453 // Flush denormals to 0 if not enabled.
2454 if (C.isDenormal()) {
2455 EVT VT = N->getValueType(0);
2456 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
2457 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2458
2459 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
2460 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2461 }
2462
2463 if (C.isNaN()) {
2464 EVT VT = N->getValueType(0);
2465 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
2466 if (C.isSignaling()) {
2467 // Quiet a signaling NaN.
2468 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
2469 }
2470
2471 // Make sure it is the canonical NaN bitpattern.
2472 //
2473 // TODO: Can we use -1 as the canonical NaN value since it's an inline
2474 // immediate?
2475 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
2476 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
2477 }
2478
2479 return SDValue(CFP, 0);
2480}
2481
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002482static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
2483 switch (Opc) {
2484 case ISD::FMAXNUM:
2485 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002486 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002487 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002488 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002489 return AMDGPUISD::UMAX3;
2490 case ISD::FMINNUM:
2491 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002492 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002493 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002494 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002495 return AMDGPUISD::UMIN3;
2496 default:
2497 llvm_unreachable("Not a min/max opcode");
2498 }
2499}
2500
Matt Arsenaultf639c322016-01-28 20:53:42 +00002501static SDValue performIntMed3ImmCombine(SelectionDAG &DAG,
2502 SDLoc SL,
2503 SDValue Op0,
2504 SDValue Op1,
2505 bool Signed) {
2506 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
2507 if (!K1)
2508 return SDValue();
2509
2510 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
2511 if (!K0)
2512 return SDValue();
2513
2514
2515 if (Signed) {
2516 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
2517 return SDValue();
2518 } else {
2519 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
2520 return SDValue();
2521 }
2522
2523 EVT VT = K0->getValueType(0);
2524 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
2525 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
2526}
2527
2528static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
2529 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
2530 return true;
2531
2532 return DAG.isKnownNeverNaN(Op);
2533}
2534
2535static SDValue performFPMed3ImmCombine(SelectionDAG &DAG,
2536 SDLoc SL,
2537 SDValue Op0,
2538 SDValue Op1) {
2539 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
2540 if (!K1)
2541 return SDValue();
2542
2543 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
2544 if (!K0)
2545 return SDValue();
2546
2547 // Ordered >= (although NaN inputs should have folded away by now).
2548 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
2549 if (Cmp == APFloat::cmpGreaterThan)
2550 return SDValue();
2551
2552 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
2553 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
2554 // give the other result, which is different from med3 with a NaN input.
2555 SDValue Var = Op0.getOperand(0);
2556 if (!isKnownNeverSNan(DAG, Var))
2557 return SDValue();
2558
2559 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
2560 Var, SDValue(K0, 0), SDValue(K1, 0));
2561}
2562
2563SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
2564 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002565 SelectionDAG &DAG = DCI.DAG;
2566
2567 unsigned Opc = N->getOpcode();
2568 SDValue Op0 = N->getOperand(0);
2569 SDValue Op1 = N->getOperand(1);
2570
2571 // Only do this if the inner op has one use since this will just increases
2572 // register pressure for no benefit.
2573
Matt Arsenault5b39b342016-01-28 20:53:48 +00002574 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
2575 // max(max(a, b), c) -> max3(a, b, c)
2576 // min(min(a, b), c) -> min3(a, b, c)
2577 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
2578 SDLoc DL(N);
2579 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2580 DL,
2581 N->getValueType(0),
2582 Op0.getOperand(0),
2583 Op0.getOperand(1),
2584 Op1);
2585 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002586
Matt Arsenault5b39b342016-01-28 20:53:48 +00002587 // Try commuted.
2588 // max(a, max(b, c)) -> max3(a, b, c)
2589 // min(a, min(b, c)) -> min3(a, b, c)
2590 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
2591 SDLoc DL(N);
2592 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2593 DL,
2594 N->getValueType(0),
2595 Op0,
2596 Op1.getOperand(0),
2597 Op1.getOperand(1));
2598 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002599 }
2600
Matt Arsenaultf639c322016-01-28 20:53:42 +00002601 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
2602 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
2603 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
2604 return Med3;
2605 }
2606
2607 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
2608 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
2609 return Med3;
2610 }
2611
2612 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00002613 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
2614 (Opc == AMDGPUISD::FMIN_LEGACY &&
2615 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00002616 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
2617 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
2618 return Res;
2619 }
2620
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002621 return SDValue();
2622}
2623
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002624SDValue SITargetLowering::performSetCCCombine(SDNode *N,
2625 DAGCombinerInfo &DCI) const {
2626 SelectionDAG &DAG = DCI.DAG;
2627 SDLoc SL(N);
2628
2629 SDValue LHS = N->getOperand(0);
2630 SDValue RHS = N->getOperand(1);
2631 EVT VT = LHS.getValueType();
2632
2633 if (VT != MVT::f32 && VT != MVT::f64)
2634 return SDValue();
2635
2636 // Match isinf pattern
2637 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
2638 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
2639 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
2640 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2641 if (!CRHS)
2642 return SDValue();
2643
2644 const APFloat &APF = CRHS->getValueAPF();
2645 if (APF.isInfinity() && !APF.isNegative()) {
2646 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002647 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
2648 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002649 }
2650 }
2651
2652 return SDValue();
2653}
2654
Tom Stellard75aadc22012-12-11 21:25:42 +00002655SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
2656 DAGCombinerInfo &DCI) const {
2657 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002658 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00002659
2660 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00002661 default:
2662 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002663 case ISD::SETCC:
2664 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00002665 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002666 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002667 case ISD::SMAX:
2668 case ISD::SMIN:
2669 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00002670 case ISD::UMIN:
2671 case AMDGPUISD::FMIN_LEGACY:
2672 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002673 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00002674 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002675 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00002676 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002677 break;
2678 }
Matt Arsenault364a6742014-06-11 17:50:44 +00002679
2680 case AMDGPUISD::CVT_F32_UBYTE0:
2681 case AMDGPUISD::CVT_F32_UBYTE1:
2682 case AMDGPUISD::CVT_F32_UBYTE2:
2683 case AMDGPUISD::CVT_F32_UBYTE3: {
2684 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
2685
2686 SDValue Src = N->getOperand(0);
2687 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
2688
2689 APInt KnownZero, KnownOne;
2690 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2691 !DCI.isBeforeLegalizeOps());
2692 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2693 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
2694 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
2695 DCI.CommitTargetLoweringOpt(TLO);
2696 }
2697
2698 break;
2699 }
2700
2701 case ISD::UINT_TO_FP: {
2702 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002703 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002704 case ISD::FADD: {
2705 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2706 break;
2707
2708 EVT VT = N->getValueType(0);
2709 if (VT != MVT::f32)
2710 break;
2711
Matt Arsenault8d630032015-02-20 22:10:41 +00002712 // Only do this if we are not trying to support denormals. v_mad_f32 does
2713 // not support denormals ever.
2714 if (Subtarget->hasFP32Denormals())
2715 break;
2716
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002717 SDValue LHS = N->getOperand(0);
2718 SDValue RHS = N->getOperand(1);
2719
2720 // These should really be instruction patterns, but writing patterns with
2721 // source modiifiers is a pain.
2722
2723 // fadd (fadd (a, a), b) -> mad 2.0, a, b
2724 if (LHS.getOpcode() == ISD::FADD) {
2725 SDValue A = LHS.getOperand(0);
2726 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002727 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002728 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002729 }
2730 }
2731
2732 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
2733 if (RHS.getOpcode() == ISD::FADD) {
2734 SDValue A = RHS.getOperand(0);
2735 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002736 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002737 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002738 }
2739 }
2740
Matt Arsenault8d630032015-02-20 22:10:41 +00002741 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002742 }
Matt Arsenault8675db12014-08-29 16:01:14 +00002743 case ISD::FSUB: {
2744 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2745 break;
2746
2747 EVT VT = N->getValueType(0);
2748
2749 // Try to get the fneg to fold into the source modifier. This undoes generic
2750 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00002751 //
2752 // Only do this if we are not trying to support denormals. v_mad_f32 does
2753 // not support denormals ever.
2754 if (VT == MVT::f32 &&
2755 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00002756 SDValue LHS = N->getOperand(0);
2757 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002758 if (LHS.getOpcode() == ISD::FADD) {
2759 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
2760
2761 SDValue A = LHS.getOperand(0);
2762 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002763 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002764 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
2765
Matt Arsenault8d630032015-02-20 22:10:41 +00002766 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002767 }
2768 }
2769
2770 if (RHS.getOpcode() == ISD::FADD) {
2771 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
2772
2773 SDValue A = RHS.getOperand(0);
2774 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002775 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002776 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002777 }
2778 }
Matt Arsenault8d630032015-02-20 22:10:41 +00002779
2780 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00002781 }
2782
2783 break;
2784 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002785 case ISD::LOAD:
2786 case ISD::STORE:
2787 case ISD::ATOMIC_LOAD:
2788 case ISD::ATOMIC_STORE:
2789 case ISD::ATOMIC_CMP_SWAP:
2790 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2791 case ISD::ATOMIC_SWAP:
2792 case ISD::ATOMIC_LOAD_ADD:
2793 case ISD::ATOMIC_LOAD_SUB:
2794 case ISD::ATOMIC_LOAD_AND:
2795 case ISD::ATOMIC_LOAD_OR:
2796 case ISD::ATOMIC_LOAD_XOR:
2797 case ISD::ATOMIC_LOAD_NAND:
2798 case ISD::ATOMIC_LOAD_MIN:
2799 case ISD::ATOMIC_LOAD_MAX:
2800 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002801 case ISD::ATOMIC_LOAD_UMAX:
2802 case AMDGPUISD::ATOMIC_INC:
2803 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002804 if (DCI.isBeforeLegalize())
2805 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002806
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002807 MemSDNode *MemNode = cast<MemSDNode>(N);
2808 SDValue Ptr = MemNode->getBasePtr();
2809
2810 // TODO: We could also do this for multiplies.
2811 unsigned AS = MemNode->getAddressSpace();
2812 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
2813 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
2814 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002815 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002816
2817 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
2818 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
2819 }
2820 }
2821 break;
2822 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002823 case ISD::AND:
2824 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002825 case ISD::OR:
2826 return performOrCombine(N, DCI);
2827 case AMDGPUISD::FP_CLASS:
2828 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00002829 case ISD::FCANONICALIZE:
2830 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002831 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002832 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00002833}
Christian Konigd910b7d2013-02-26 17:52:16 +00002834
Christian Konigf82901a2013-02-26 17:52:23 +00002835/// \brief Analyze the possible immediate value Op
2836///
2837/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
2838/// and the immediate value if it's a literal immediate
2839int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
2840
Eric Christopher7792e322015-01-30 23:24:40 +00002841 const SIInstrInfo *TII =
2842 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00002843
Tom Stellardedbf1eb2013-04-05 23:31:20 +00002844 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00002845 if (TII->isInlineConstant(Node->getAPIntValue()))
2846 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00002847
Matt Arsenault11a4d672015-02-13 19:05:03 +00002848 uint64_t Val = Node->getZExtValue();
2849 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00002850 }
2851
2852 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
2853 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
2854 return 0;
2855
2856 if (Node->getValueType(0) == MVT::f32)
2857 return FloatToBits(Node->getValueAPF().convertToFloat());
2858
2859 return -1;
2860 }
2861
2862 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00002863}
2864
Christian Konig8e06e2a2013-04-10 08:39:08 +00002865/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00002866static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00002867 switch (Idx) {
2868 default: return 0;
2869 case AMDGPU::sub0: return 0;
2870 case AMDGPU::sub1: return 1;
2871 case AMDGPU::sub2: return 2;
2872 case AMDGPU::sub3: return 3;
2873 }
2874}
2875
2876/// \brief Adjust the writemask of MIMG instructions
2877void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
2878 SelectionDAG &DAG) const {
2879 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00002880 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002881 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
2882 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00002883 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002884
2885 // Try to figure out the used register components
2886 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
2887 I != E; ++I) {
2888
2889 // Abort if we can't understand the usage
2890 if (!I->isMachineOpcode() ||
2891 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
2892 return;
2893
Tom Stellard54774e52013-10-23 02:53:47 +00002894 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
2895 // Note that subregs are packed, i.e. Lane==0 is the first bit set
2896 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
2897 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00002898 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00002899
Tom Stellard54774e52013-10-23 02:53:47 +00002900 // Set which texture component corresponds to the lane.
2901 unsigned Comp;
2902 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
2903 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00002904 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00002905 Dmask &= ~(1 << Comp);
2906 }
2907
Christian Konig8e06e2a2013-04-10 08:39:08 +00002908 // Abort if we have more than one user per component
2909 if (Users[Lane])
2910 return;
2911
2912 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00002913 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002914 }
2915
Tom Stellard54774e52013-10-23 02:53:47 +00002916 // Abort if there's no change
2917 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00002918 return;
2919
2920 // Adjust the writemask in the node
2921 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002922 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002923 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002924 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00002925 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002926
Christian Konig8b1ed282013-04-10 08:39:16 +00002927 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00002928 // (if NewDmask has only one bit set...)
2929 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002930 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
2931 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00002932 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002933 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00002934 SDValue(Node, 0), RC);
2935 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
2936 return;
2937 }
2938
Christian Konig8e06e2a2013-04-10 08:39:08 +00002939 // Update the users of the node with the new indices
2940 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
2941
2942 SDNode *User = Users[i];
2943 if (!User)
2944 continue;
2945
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002946 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002947 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
2948
2949 switch (Idx) {
2950 default: break;
2951 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
2952 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
2953 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
2954 }
2955 }
2956}
2957
Tom Stellardc98ee202015-07-16 19:40:07 +00002958static bool isFrameIndexOp(SDValue Op) {
2959 if (Op.getOpcode() == ISD::AssertZext)
2960 Op = Op.getOperand(0);
2961
2962 return isa<FrameIndexSDNode>(Op);
2963}
2964
Tom Stellard3457a842014-10-09 19:06:00 +00002965/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
2966/// with frame index operands.
2967/// LLVM assumes that inputs are to these instructions are registers.
2968void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
2969 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002970
2971 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00002972 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00002973 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00002974 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002975 continue;
2976 }
2977
Tom Stellard3457a842014-10-09 19:06:00 +00002978 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002979 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00002980 Node->getOperand(i).getValueType(),
2981 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002982 }
2983
Tom Stellard3457a842014-10-09 19:06:00 +00002984 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002985}
2986
Matt Arsenault08d84942014-06-03 23:06:13 +00002987/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00002988SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
2989 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002990 const SIInstrInfo *TII =
2991 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00002992 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00002993
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00002994 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore())
Christian Konig8e06e2a2013-04-10 08:39:08 +00002995 adjustWritemask(Node, DAG);
2996
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00002997 if (Opcode == AMDGPU::INSERT_SUBREG ||
2998 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002999 legalizeTargetIndependentNode(Node, DAG);
3000 return Node;
3001 }
Tom Stellard654d6692015-01-08 15:08:17 +00003002 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003003}
Christian Konig8b1ed282013-04-10 08:39:16 +00003004
3005/// \brief Assign the register class depending on the number of
3006/// bits set in the writemask
3007void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
3008 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00003009 const SIInstrInfo *TII =
3010 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003011
Tom Stellarda99ada52014-11-21 22:31:44 +00003012 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003013
3014 if (TII->isVOP3(MI->getOpcode())) {
3015 // Make sure constant bus requirements are respected.
3016 TII->legalizeOperandsVOP3(MRI, MI);
3017 return;
3018 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00003019
Matt Arsenault3add6432015-10-20 04:35:43 +00003020 if (TII->isMIMG(*MI)) {
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003021 unsigned VReg = MI->getOperand(0).getReg();
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003022 unsigned DmaskIdx = MI->getNumOperands() == 12 ? 3 : 4;
3023 unsigned Writemask = MI->getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003024 unsigned BitsSet = 0;
3025 for (unsigned i = 0; i < 4; ++i)
3026 BitsSet += Writemask & (1 << i) ? 1 : 0;
3027
3028 const TargetRegisterClass *RC;
3029 switch (BitsSet) {
3030 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00003031 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003032 case 2: RC = &AMDGPU::VReg_64RegClass; break;
3033 case 3: RC = &AMDGPU::VReg_96RegClass; break;
3034 }
3035
3036 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
3037 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003038 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00003039 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00003040 }
3041
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003042 // Replace unused atomics with the no return version.
3043 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
3044 if (NoRetAtomicOp != -1) {
3045 if (!Node->hasAnyUseOfValue(0)) {
3046 MI->setDesc(TII->get(NoRetAtomicOp));
3047 MI->RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003048 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003049 }
3050
Tom Stellard354a43c2016-04-01 18:27:37 +00003051 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
3052 // instruction, because the return type of these instructions is a vec2 of
3053 // the memory type, so it can be tied to the input operand.
3054 // This means these instructions always have a use, so we need to add a
3055 // special case to check if the atomic has only one extract_subreg use,
3056 // which itself has no uses.
3057 if ((Node->hasNUsesOfValue(1, 0) &&
Nicolai Haehnle750082d2016-04-15 14:42:36 +00003058 Node->use_begin()->isMachineOpcode() &&
Tom Stellard354a43c2016-04-01 18:27:37 +00003059 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
3060 !Node->use_begin()->hasAnyUseOfValue(0))) {
3061 unsigned Def = MI->getOperand(0).getReg();
3062
3063 // Change this into a noret atomic.
3064 MI->setDesc(TII->get(NoRetAtomicOp));
3065 MI->RemoveOperand(0);
3066
3067 // If we only remove the def operand from the atomic instruction, the
3068 // extract_subreg will be left with a use of a vreg without a def.
3069 // So we need to insert an implicit_def to avoid machine verifier
3070 // errors.
3071 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
3072 TII->get(AMDGPU::IMPLICIT_DEF), Def);
3073 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003074 return;
3075 }
Christian Konig8b1ed282013-04-10 08:39:16 +00003076}
Tom Stellard0518ff82013-06-03 17:39:58 +00003077
Matt Arsenault485defe2014-11-05 19:01:17 +00003078static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003079 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00003080 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
3081}
3082
3083MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
3084 SDLoc DL,
3085 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00003086 const SIInstrInfo *TII =
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003087 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00003088
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003089 // Build the half of the subregister with the constants before building the
3090 // full 128-bit register. If we are building multiple resource descriptors,
3091 // this will allow CSEing of the 2-component register.
3092 const SDValue Ops0[] = {
3093 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
3094 buildSMovImm32(DAG, DL, 0),
3095 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
3096 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
3097 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
3098 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003099
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003100 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
3101 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00003102
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003103 // Combine the constants and the pointer.
3104 const SDValue Ops1[] = {
3105 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
3106 Ptr,
3107 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
3108 SubRegHi,
3109 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
3110 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003111
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003112 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00003113}
3114
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003115/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003116/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
3117/// of the resource descriptor) to create an offset, which is added to
3118/// the resource pointer.
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003119MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
3120 SDLoc DL,
3121 SDValue Ptr,
3122 uint32_t RsrcDword1,
3123 uint64_t RsrcDword2And3) const {
3124 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
3125 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
3126 if (RsrcDword1) {
3127 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003128 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
3129 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003130 }
3131
3132 SDValue DataLo = buildSMovImm32(DAG, DL,
3133 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
3134 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
3135
3136 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003137 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003138 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003139 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003140 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003141 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003142 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003143 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003144 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003145 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003146 };
3147
3148 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
3149}
3150
Tom Stellard94593ee2013-06-03 17:40:18 +00003151SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3152 const TargetRegisterClass *RC,
3153 unsigned Reg, EVT VT) const {
3154 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
3155
3156 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
3157 cast<RegisterSDNode>(VReg)->getReg(), VT);
3158}
Tom Stellardd7e6f132015-04-08 01:09:26 +00003159
3160//===----------------------------------------------------------------------===//
3161// SI Inline Assembly Support
3162//===----------------------------------------------------------------------===//
3163
3164std::pair<unsigned, const TargetRegisterClass *>
3165SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003166 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00003167 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003168
3169 if (Constraint.size() == 1) {
3170 switch (Constraint[0]) {
3171 case 's':
3172 case 'r':
3173 switch (VT.getSizeInBits()) {
3174 default:
3175 return std::make_pair(0U, nullptr);
3176 case 32:
Tom Stellardd7e6f132015-04-08 01:09:26 +00003177 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003178 case 64:
3179 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
3180 case 128:
3181 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
3182 case 256:
3183 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
3184 }
3185
3186 case 'v':
3187 switch (VT.getSizeInBits()) {
3188 default:
3189 return std::make_pair(0U, nullptr);
3190 case 32:
3191 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
3192 case 64:
3193 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
3194 case 96:
3195 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
3196 case 128:
3197 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
3198 case 256:
3199 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
3200 case 512:
3201 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
3202 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00003203 }
3204 }
3205
3206 if (Constraint.size() > 1) {
3207 const TargetRegisterClass *RC = nullptr;
3208 if (Constraint[1] == 'v') {
3209 RC = &AMDGPU::VGPR_32RegClass;
3210 } else if (Constraint[1] == 's') {
3211 RC = &AMDGPU::SGPR_32RegClass;
3212 }
3213
3214 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00003215 uint32_t Idx;
3216 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
3217 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00003218 return std::make_pair(RC->getRegister(Idx), RC);
3219 }
3220 }
3221 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
3222}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003223
3224SITargetLowering::ConstraintType
3225SITargetLowering::getConstraintType(StringRef Constraint) const {
3226 if (Constraint.size() == 1) {
3227 switch (Constraint[0]) {
3228 default: break;
3229 case 's':
3230 case 'v':
3231 return C_RegisterClass;
3232 }
3233 }
3234 return TargetLowering::getConstraintType(Constraint);
3235}