blob: 544867513d9c541720560cae67e978e4eca156e6 [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
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "SIISelLowering.h"
Christian Konig99ee0f42013-03-07 09:04:14 +000022#include "AMDGPU.h"
Matt Arsenaultd48da142015-11-02 23:23:02 +000023#include "AMDGPUDiagnosticInfoUnsupported.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000024#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000025#include "AMDGPUSubtarget.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000026#include "SIInstrInfo.h"
27#include "SIMachineFunctionInfo.h"
28#include "SIRegisterInfo.h"
Alexey Samsonova253bf92014-08-27 19:36:53 +000029#include "llvm/ADT/BitVector.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"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000034#include "llvm/IR/Function.h"
Matt Arsenault364a6742014-06-11 17:50:44 +000035#include "llvm/ADT/SmallString.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000036
37using namespace llvm;
38
Eric Christopher7792e322015-01-30 23:24:40 +000039SITargetLowering::SITargetLowering(TargetMachine &TM,
40 const AMDGPUSubtarget &STI)
41 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000042 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000043 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000044
Christian Konig2214f142013-03-07 09:03:38 +000045 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
46 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
47
Tom Stellard334b29c2014-04-17 21:00:09 +000048 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000049 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000050
Tom Stellard436780b2014-05-15 14:41:57 +000051 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
52 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
53 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000054
Matt Arsenault61001bb2015-11-25 19:58:34 +000055 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
56 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
57
Tom Stellard436780b2014-05-15 14:41:57 +000058 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
59 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000060
Tom Stellardf0a21072014-11-18 20:39:39 +000061 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000062 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
63
Tom Stellardf0a21072014-11-18 20:39:39 +000064 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000065 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000066
Eric Christopher23a3a7c2015-02-26 00:00:24 +000067 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000068
Christian Konig2989ffc2013-03-18 11:34:16 +000069 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
70 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
71 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
72 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
73
Tom Stellard75aadc22012-12-11 21:25:42 +000074 setOperationAction(ISD::ADD, MVT::i32, Legal);
Matt Arsenaulte8d21462013-11-18 20:09:40 +000075 setOperationAction(ISD::ADDC, MVT::i32, Legal);
76 setOperationAction(ISD::ADDE, MVT::i32, Legal);
Matt Arsenaultb8b51532014-06-23 18:00:38 +000077 setOperationAction(ISD::SUBC, MVT::i32, Legal);
78 setOperationAction(ISD::SUBE, MVT::i32, Legal);
Aaron Watrydaabb202013-06-25 13:55:52 +000079
Matt Arsenaultad14ce82014-07-19 18:44:39 +000080 setOperationAction(ISD::FSIN, MVT::f32, Custom);
81 setOperationAction(ISD::FCOS, MVT::f32, Custom);
82
Matt Arsenault7c936902014-10-21 23:01:01 +000083 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
84 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
85
Tom Stellard35bb18c2013-08-26 15:06:04 +000086 // We need to custom lower vector stores from local memory
Tom Stellard35bb18c2013-08-26 15:06:04 +000087 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000088 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
89 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
90
91 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
92 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000093
Tom Stellard1c8788e2014-03-07 20:12:33 +000094 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000095 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
96
Tom Stellard0ec134f2014-02-04 17:18:40 +000097 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +000098 setOperationAction(ISD::SELECT, MVT::f64, Promote);
99 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000100
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000101 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
102 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
103 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
104 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000105
Tom Stellard83747202013-07-18 21:43:53 +0000106 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
107 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
108
Matt Arsenaulte306a322014-10-21 16:25:08 +0000109 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
Matt Arsenaultd0792852015-12-14 17:25:38 +0000110 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000111
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
114 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
115
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000116 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
119
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
123
Matt Arsenault94812212014-11-14 18:18:16 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000125 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
126
Tom Stellard94593ee2013-06-03 17:40:18 +0000127 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000128 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
129 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
130 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000131
Tom Stellardafcf12f2013-09-12 02:55:14 +0000132 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000133 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000134
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000135 for (MVT VT : MVT::integer_valuetypes()) {
Matt Arsenaultbd223422015-01-14 01:35:17 +0000136 if (VT == MVT::i64)
137 continue;
138
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000139 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000140 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
141 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000142 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000143
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000144 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000145 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
146 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000147 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000148
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000149 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000150 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
151 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000152 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
153 }
154
155 for (MVT VT : MVT::integer_vector_valuetypes()) {
156 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
157 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
158 }
159
160 for (MVT VT : MVT::fp_valuetypes())
161 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000162
Matt Arsenault61001bb2015-11-25 19:58:34 +0000163 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
164 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
165
Matt Arsenault6f243792013-09-05 19:41:10 +0000166 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000167 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
Matt Arsenaulte1ce3442015-07-31 04:12:04 +0000168 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000169 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000170
Matt Arsenault61001bb2015-11-25 19:58:34 +0000171
172 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
173
174 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
175 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
176
Matt Arsenault470acd82014-04-15 22:28:39 +0000177 setOperationAction(ISD::LOAD, MVT::i1, Custom);
178
Matt Arsenault61001bb2015-11-25 19:58:34 +0000179 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
180 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
181
182 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
183 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
184
185 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
186
Tom Stellardfd155822013-08-26 15:05:36 +0000187 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000188 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000189 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000190
Tom Stellard5f337882014-04-29 23:12:43 +0000191 // These should use UDIVREM, so set them to expand
192 setOperationAction(ISD::UDIV, MVT::i64, Expand);
193 setOperationAction(ISD::UREM, MVT::i64, Expand);
194
Matt Arsenault0d89e842014-07-15 21:44:37 +0000195 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
196 setOperationAction(ISD::SELECT, MVT::i1, Promote);
197
Matt Arsenault61001bb2015-11-25 19:58:34 +0000198 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
199
200
201 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
202
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000203 // We only support LOAD/STORE and vector manipulation ops for vectors
204 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000205 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000206 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
207 switch(Op) {
208 case ISD::LOAD:
209 case ISD::STORE:
210 case ISD::BUILD_VECTOR:
211 case ISD::BITCAST:
212 case ISD::EXTRACT_VECTOR_ELT:
213 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000214 case ISD::INSERT_SUBVECTOR:
215 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000216 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000217 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000218 case ISD::CONCAT_VECTORS:
219 setOperationAction(Op, VT, Custom);
220 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000221 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000222 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000223 break;
224 }
225 }
226 }
227
Matt Arsenault61001bb2015-11-25 19:58:34 +0000228 // Most operations are naturally 32-bit vector operations. We only support
229 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
230 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
231 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
232 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
233
234 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
235 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
236
237 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
238 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
239
240 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
241 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
242 }
243
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000244 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
245 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
246 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000247 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000248 }
249
Marek Olsak7d777282015-03-24 13:40:15 +0000250 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000251 setOperationAction(ISD::FDIV, MVT::f32, Custom);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +0000252 setOperationAction(ISD::FDIV, MVT::f64, Custom);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000253
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000254 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000255 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000256 setTargetDAGCombine(ISD::FMINNUM);
257 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000258 setTargetDAGCombine(ISD::SMIN);
259 setTargetDAGCombine(ISD::SMAX);
260 setTargetDAGCombine(ISD::UMIN);
261 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000262 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000263 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000264 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000265 setTargetDAGCombine(ISD::UINT_TO_FP);
266
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000267 // All memory operations. Some folding on the pointer operand is done to help
268 // matching the constant offsets in the addressing modes.
269 setTargetDAGCombine(ISD::LOAD);
270 setTargetDAGCombine(ISD::STORE);
271 setTargetDAGCombine(ISD::ATOMIC_LOAD);
272 setTargetDAGCombine(ISD::ATOMIC_STORE);
273 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
274 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
275 setTargetDAGCombine(ISD::ATOMIC_SWAP);
276 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
277 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
278 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
279 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
280 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
281 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
282 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
283 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
284 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
285 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
286
Christian Konigeecebd02013-03-26 14:04:02 +0000287 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000288}
289
Tom Stellard0125f2a2013-06-25 02:39:35 +0000290//===----------------------------------------------------------------------===//
291// TargetLowering queries
292//===----------------------------------------------------------------------===//
293
Matt Arsenaulte306a322014-10-21 16:25:08 +0000294bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
295 EVT) const {
296 // SI has some legal vector types, but no legal vector operations. Say no
297 // shuffles are legal in order to prefer scalarizing some vector operations.
298 return false;
299}
300
Tom Stellard70580f82015-07-20 14:28:41 +0000301bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
302 // Flat instructions do not have offsets, and only have the register
303 // address.
304 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
305}
306
Matt Arsenault711b3902015-08-07 20:18:34 +0000307bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
308 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
309 // additionally can do r + r + i with addr64. 32-bit has more addressing
310 // mode options. Depending on the resource constant, it can also do
311 // (i64 r0) + (i32 r1) * (i14 i).
312 //
313 // Private arrays end up using a scratch buffer most of the time, so also
314 // assume those use MUBUF instructions. Scratch loads / stores are currently
315 // implemented as mubuf instructions with offen bit set, so slightly
316 // different than the normal addr64.
317 if (!isUInt<12>(AM.BaseOffs))
318 return false;
319
320 // FIXME: Since we can split immediate into soffset and immediate offset,
321 // would it make sense to allow any immediate?
322
323 switch (AM.Scale) {
324 case 0: // r + i or just i, depending on HasBaseReg.
325 return true;
326 case 1:
327 return true; // We have r + r or r + i.
328 case 2:
329 if (AM.HasBaseReg) {
330 // Reject 2 * r + r.
331 return false;
332 }
333
334 // Allow 2 * r as r + r
335 // Or 2 * r + i is allowed as r + r + i.
336 return true;
337 default: // Don't allow n * r
338 return false;
339 }
340}
341
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000342bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
343 const AddrMode &AM, Type *Ty,
344 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000345 // No global is ever allowed as a base.
346 if (AM.BaseGV)
347 return false;
348
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000349 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000350 case AMDGPUAS::GLOBAL_ADDRESS: {
Tom Stellard70580f82015-07-20 14:28:41 +0000351 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
352 // Assume the we will use FLAT for all global memory accesses
353 // on VI.
354 // FIXME: This assumption is currently wrong. On VI we still use
355 // MUBUF instructions for the r + i addressing mode. As currently
356 // implemented, the MUBUF instructions only work on buffer < 4GB.
357 // It may be possible to support > 4GB buffers with MUBUF instructions,
358 // by setting the stride value in the resource descriptor which would
359 // increase the size limit to (stride * 4GB). However, this is risky,
360 // because it has never been validated.
361 return isLegalFlatAddressingMode(AM);
362 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000363
Matt Arsenault711b3902015-08-07 20:18:34 +0000364 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000365 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000366 case AMDGPUAS::CONSTANT_ADDRESS: {
367 // If the offset isn't a multiple of 4, it probably isn't going to be
368 // correctly aligned.
369 if (AM.BaseOffs % 4 != 0)
370 return isLegalMUBUFAddressingMode(AM);
371
372 // There are no SMRD extloads, so if we have to do a small type access we
373 // will use a MUBUF load.
374 // FIXME?: We also need to do this if unaligned, but we don't know the
375 // alignment here.
376 if (DL.getTypeStoreSize(Ty) < 4)
377 return isLegalMUBUFAddressingMode(AM);
378
379 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
380 // SMRD instructions have an 8-bit, dword offset on SI.
381 if (!isUInt<8>(AM.BaseOffs / 4))
382 return false;
383 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) {
384 // On CI+, this can also be a 32-bit literal constant offset. If it fits
385 // in 8-bits, it can use a smaller encoding.
386 if (!isUInt<32>(AM.BaseOffs / 4))
387 return false;
388 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) {
389 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
390 if (!isUInt<20>(AM.BaseOffs))
391 return false;
392 } else
393 llvm_unreachable("unhandled generation");
394
395 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
396 return true;
397
398 if (AM.Scale == 1 && AM.HasBaseReg)
399 return true;
400
401 return false;
402 }
403
404 case AMDGPUAS::PRIVATE_ADDRESS:
405 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
406 return isLegalMUBUFAddressingMode(AM);
407
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000408 case AMDGPUAS::LOCAL_ADDRESS:
409 case AMDGPUAS::REGION_ADDRESS: {
410 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
411 // field.
412 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
413 // an 8-bit dword offset but we don't know the alignment here.
414 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000415 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000416
417 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
418 return true;
419
420 if (AM.Scale == 1 && AM.HasBaseReg)
421 return true;
422
Matt Arsenault5015a892014-08-15 17:17:07 +0000423 return false;
424 }
Tom Stellard70580f82015-07-20 14:28:41 +0000425 case AMDGPUAS::FLAT_ADDRESS:
426 return isLegalFlatAddressingMode(AM);
427
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000428 default:
429 llvm_unreachable("unhandled address space");
430 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000431}
432
Matt Arsenaulte6986632015-01-14 01:35:22 +0000433bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000434 unsigned AddrSpace,
435 unsigned Align,
436 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000437 if (IsFast)
438 *IsFast = false;
439
Matt Arsenault1018c892014-04-24 17:08:26 +0000440 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
441 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000442 if (!VT.isSimple() || VT == MVT::Other)
443 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000444
Tom Stellardc6b299c2015-02-02 18:02:28 +0000445 // TODO - CI+ supports unaligned memory accesses, but this requires driver
446 // support.
Matt Arsenault1018c892014-04-24 17:08:26 +0000447
Matt Arsenault1018c892014-04-24 17:08:26 +0000448 // XXX - The only mention I see of this in the ISA manual is for LDS direct
449 // reads the "byte address and must be dword aligned". Is it also true for the
450 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000451 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
452 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
453 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
454 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000455 bool AlignedBy4 = (Align % 4 == 0);
456 if (IsFast)
457 *IsFast = AlignedBy4;
458 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000459 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000460
Tom Stellard33e64c62015-02-04 20:49:52 +0000461 // Smaller than dword value must be aligned.
462 // FIXME: This should be allowed on CI+
463 if (VT.bitsLT(MVT::i32))
464 return false;
465
Matt Arsenault1018c892014-04-24 17:08:26 +0000466 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
467 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000468 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000469 if (IsFast)
470 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000471
472 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000473}
474
Matt Arsenault46645fa2014-07-28 17:49:26 +0000475EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
476 unsigned SrcAlign, bool IsMemset,
477 bool ZeroMemset,
478 bool MemcpyStrSrc,
479 MachineFunction &MF) const {
480 // FIXME: Should account for address space here.
481
482 // The default fallback uses the private pointer size as a guess for a type to
483 // use. Make sure we switch these to 64-bit accesses.
484
485 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
486 return MVT::v4i32;
487
488 if (Size >= 8 && DstAlign >= 4)
489 return MVT::v2i32;
490
491 // Use the default.
492 return MVT::Other;
493}
494
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000495static bool isFlatGlobalAddrSpace(unsigned AS) {
496 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
497 AS == AMDGPUAS::FLAT_ADDRESS ||
498 AS == AMDGPUAS::CONSTANT_ADDRESS;
499}
500
501bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
502 unsigned DestAS) const {
503 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
504}
505
Tom Stellarda6f24c62015-12-15 20:55:55 +0000506
507bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
508 const MemSDNode *MemNode = cast<MemSDNode>(N);
509 const Value *Ptr = MemNode->getMemOperand()->getValue();
510
511 // UndefValue means this is a load of a kernel input. These are uniform.
512 // Sometimes LDS instructions have constant pointers
513 if (isa<UndefValue>(Ptr) || isa<Argument>(Ptr) || isa<Constant>(Ptr) ||
514 isa<GlobalValue>(Ptr))
515 return true;
516
517 const Instruction *I = dyn_cast_or_null<Instruction>(Ptr);
518 return I && I->getMetadata("amdgpu.uniform");
519}
520
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000521TargetLoweringBase::LegalizeTypeAction
522SITargetLowering::getPreferredVectorAction(EVT VT) const {
523 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
524 return TypeSplitVector;
525
526 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000527}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000528
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000529bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
530 Type *Ty) const {
Eric Christopher7792e322015-01-30 23:24:40 +0000531 const SIInstrInfo *TII =
532 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000533 return TII->isInlineConstant(Imm);
534}
535
Tom Stellardaf775432013-10-23 00:44:32 +0000536SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000537 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000538 unsigned Offset, bool Signed) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000539 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000540 MachineFunction &MF = DAG.getMachineFunction();
541 const SIRegisterInfo *TRI =
542 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
Matt Arsenaultac234b62015-11-30 21:15:57 +0000543 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000544
Matt Arsenault86033ca2014-07-28 17:31:39 +0000545 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
546
547 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000548 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000549 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000550 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
551 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
552 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
553 DAG.getConstant(Offset, SL, PtrVT));
Mehdi Amini44ede332015-07-09 02:09:04 +0000554 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000555 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
556
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000557 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000558
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000559 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000560 if (MemVT.isFloatingPoint())
561 ExtTy = ISD::EXTLOAD;
562
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000563 return DAG.getLoad(ISD::UNINDEXED, ExtTy,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000564 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
565 false, // isVolatile
566 true, // isNonTemporal
567 true, // isInvariant
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000568 Align); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000569}
570
Christian Konig2c8f6d52013-03-07 09:03:52 +0000571SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000572 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
573 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
574 SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardec2e43c2014-09-22 15:35:29 +0000575 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000576 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000577
578 MachineFunction &MF = DAG.getMachineFunction();
579 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000580 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000581 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000582
Matt Arsenaultd48da142015-11-02 23:23:02 +0000583 if (Subtarget->isAmdHsaOS() && Info->getShaderType() != ShaderType::COMPUTE) {
584 const Function *Fn = MF.getFunction();
585 DiagnosticInfoUnsupported NoGraphicsHSA(*Fn, "non-compute shaders with HSA");
586 DAG.getContext()->diagnose(NoGraphicsHSA);
587 return SDValue();
588 }
589
Tom Stellard0fbf8992015-10-06 21:16:34 +0000590 // FIXME: We currently assume all calling conventions are kernels.
Christian Konig2c8f6d52013-03-07 09:03:52 +0000591
592 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000593 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000594
595 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000596 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000597
598 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000599 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000600 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000601
Marek Olsakfccabaf2016-01-13 11:45:36 +0000602 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000603 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000604 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000605 ++PSInputNum;
606 continue;
607 }
608
Marek Olsakfccabaf2016-01-13 11:45:36 +0000609 Info->markPSInputAllocated(PSInputNum);
610 if (Arg.Used)
611 Info->PSInputEna |= 1 << PSInputNum;
612
613 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000614 }
615
616 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000617 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000618 ISD::InputArg NewArg = Arg;
619 NewArg.Flags.setSplit();
620 NewArg.VT = Arg.VT.getVectorElementType();
621
622 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
623 // three or five element vertex only needs three or five registers,
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000624 // NOT four or eight.
Andrew Trick05938a52015-02-16 18:10:47 +0000625 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000626 unsigned NumElements = ParamType->getVectorNumElements();
627
628 for (unsigned j = 0; j != NumElements; ++j) {
629 Splits.push_back(NewArg);
630 NewArg.PartOffset += NewArg.VT.getStoreSize();
631 }
632
Matt Arsenault762af962014-07-13 03:06:39 +0000633 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000634 Splits.push_back(Arg);
635 }
636 }
637
638 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000639 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
640 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000641
Christian Konig99ee0f42013-03-07 09:04:14 +0000642 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000643 //
644 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
645 // PSInputAddr, the user wants to enable some bits after the compilation
646 // based on run-time states. Since we can't know what the final PSInputEna
647 // will look like, so we shouldn't do anything here and the user should take
648 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000649 //
650 // Otherwise, the following restrictions apply:
651 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
652 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
653 // enabled too.
Matt Arsenault762af962014-07-13 03:06:39 +0000654 if (Info->getShaderType() == ShaderType::PIXEL &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000655 ((Info->getPSInputAddr() & 0x7F) == 0 ||
656 ((Info->getPSInputAddr() & 0xF) == 0 &&
657 Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000658 CCInfo.AllocateReg(AMDGPU::VGPR0);
659 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000660 Info->markPSInputAllocated(0);
661 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000662 }
663
Matt Arsenault762af962014-07-13 03:06:39 +0000664 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000665 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
666 Splits);
667 }
668
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000669 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
670 if (Info->hasPrivateSegmentBuffer()) {
671 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
672 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
673 CCInfo.AllocateReg(PrivateSegmentBufferReg);
674 }
675
676 if (Info->hasDispatchPtr()) {
677 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
678 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
679 CCInfo.AllocateReg(DispatchPtrReg);
680 }
681
682 if (Info->hasKernargSegmentPtr()) {
683 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
684 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
685 CCInfo.AllocateReg(InputPtrReg);
686 }
687
Christian Konig2c8f6d52013-03-07 09:03:52 +0000688 AnalyzeFormalArguments(CCInfo, Splits);
689
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000690 SmallVector<SDValue, 16> Chains;
691
Christian Konig2c8f6d52013-03-07 09:03:52 +0000692 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
693
Christian Konigb7be72d2013-05-17 09:46:48 +0000694 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000695 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000696 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000697 continue;
698 }
699
Christian Konig2c8f6d52013-03-07 09:03:52 +0000700 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000701 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000702
703 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000704 VT = Ins[i].VT;
705 EVT MemVT = Splits[i].VT;
Tom Stellardb5798b02015-06-26 21:15:03 +0000706 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
707 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000708 // The first 36 bytes of the input buffer contains information about
709 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000710 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000711 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000712 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000713
Craig Toppere3dcce92015-08-01 22:20:21 +0000714 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000715 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000716 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
717 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
718 // On SI local pointers are just offsets into LDS, so they are always
719 // less than 16-bits. On CI and newer they could potentially be
720 // real pointers, so we can't guarantee their size.
721 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
722 DAG.getValueType(MVT::i16));
723 }
724
Tom Stellarded882c22013-06-03 17:40:11 +0000725 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000726 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000727 continue;
728 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000729 assert(VA.isRegLoc() && "Parameter must be in a register!");
730
731 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000732
733 if (VT == MVT::i64) {
734 // For now assume it is a pointer
735 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
736 &AMDGPU::SReg_64RegClass);
737 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000738 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
739 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000740 continue;
741 }
742
743 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
744
745 Reg = MF.addLiveIn(Reg, RC);
746 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
747
Christian Konig2c8f6d52013-03-07 09:03:52 +0000748 if (Arg.VT.isVector()) {
749
750 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000751 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000752 unsigned NumElements = ParamType->getVectorNumElements();
753
754 SmallVector<SDValue, 4> Regs;
755 Regs.push_back(Val);
756 for (unsigned j = 1; j != NumElements; ++j) {
757 Reg = ArgLocs[ArgIdx++].getLocReg();
758 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000759
760 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
761 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000762 }
763
764 // Fill up the missing vector elements
765 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000766 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000767
Craig Topper48d114b2014-04-26 18:35:24 +0000768 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000769 continue;
770 }
771
772 InVals.push_back(Val);
773 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000774
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000775 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
776 // these from the dispatch pointer.
777
778 // Start adding system SGPRs.
779 if (Info->hasWorkGroupIDX()) {
780 unsigned Reg = Info->addWorkGroupIDX();
781 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
782 CCInfo.AllocateReg(Reg);
783 } else
784 llvm_unreachable("work group id x is always enabled");
785
786 if (Info->hasWorkGroupIDY()) {
787 unsigned Reg = Info->addWorkGroupIDY();
788 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
789 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000790 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000791
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000792 if (Info->hasWorkGroupIDZ()) {
793 unsigned Reg = Info->addWorkGroupIDZ();
794 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
795 CCInfo.AllocateReg(Reg);
796 }
797
798 if (Info->hasWorkGroupInfo()) {
799 unsigned Reg = Info->addWorkGroupInfo();
800 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
801 CCInfo.AllocateReg(Reg);
802 }
803
804 if (Info->hasPrivateSegmentWaveByteOffset()) {
805 // Scratch wave offset passed in system SGPR.
806 unsigned PrivateSegmentWaveByteOffsetReg
807 = Info->addPrivateSegmentWaveByteOffset();
808
809 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
810 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
811 }
812
813 // Now that we've figured out where the scratch register inputs are, see if
814 // should reserve the arguments and use them directly.
815
816 bool HasStackObjects = MF.getFrameInfo()->hasStackObjects();
817
818 if (ST.isAmdHsaOS()) {
819 // TODO: Assume we will spill without optimizations.
820 if (HasStackObjects) {
821 // If we have stack objects, we unquestionably need the private buffer
822 // resource. For the HSA ABI, this will be the first 4 user SGPR
823 // inputs. We can reserve those and use them directly.
824
825 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
826 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
827 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
828
829 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
830 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
831 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
832 } else {
833 unsigned ReservedBufferReg
834 = TRI->reservedPrivateSegmentBufferReg(MF);
835 unsigned ReservedOffsetReg
836 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
837
838 // We tentatively reserve the last registers (skipping the last two
839 // which may contain VCC). After register allocation, we'll replace
840 // these with the ones immediately after those which were really
841 // allocated. In the prologue copies will be inserted from the argument
842 // to these reserved registers.
843 Info->setScratchRSrcReg(ReservedBufferReg);
844 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
845 }
846 } else {
847 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
848
849 // Without HSA, relocations are used for the scratch pointer and the
850 // buffer resource setup is always inserted in the prologue. Scratch wave
851 // offset is still in an input SGPR.
852 Info->setScratchRSrcReg(ReservedBufferReg);
853
854 if (HasStackObjects) {
855 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
856 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
857 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
858 } else {
859 unsigned ReservedOffsetReg
860 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
861 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
862 }
863 }
864
865 if (Info->hasWorkItemIDX()) {
866 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
867 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
868 CCInfo.AllocateReg(Reg);
869 } else
870 llvm_unreachable("workitem id x should always be enabled");
871
872 if (Info->hasWorkItemIDY()) {
873 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
874 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
875 CCInfo.AllocateReg(Reg);
876 }
877
878 if (Info->hasWorkItemIDZ()) {
879 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
880 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
881 CCInfo.AllocateReg(Reg);
882 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000883
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000884 if (Chains.empty())
885 return Chain;
886
887 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000888}
889
Marek Olsak8a0f3352016-01-13 17:23:04 +0000890SDValue SITargetLowering::LowerReturn(SDValue Chain,
891 CallingConv::ID CallConv,
892 bool isVarArg,
893 const SmallVectorImpl<ISD::OutputArg> &Outs,
894 const SmallVectorImpl<SDValue> &OutVals,
895 SDLoc DL, SelectionDAG &DAG) const {
896 MachineFunction &MF = DAG.getMachineFunction();
897 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
898
899 if (Info->getShaderType() == ShaderType::COMPUTE)
900 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
901 OutVals, DL, DAG);
902
Marek Olsak8e9cc632016-01-13 17:23:09 +0000903 Info->setIfReturnsVoid(Outs.size() == 0);
904
Marek Olsak8a0f3352016-01-13 17:23:04 +0000905 SmallVector<ISD::OutputArg, 48> Splits;
906 SmallVector<SDValue, 48> SplitVals;
907
908 // Split vectors into their elements.
909 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
910 const ISD::OutputArg &Out = Outs[i];
911
912 if (Out.VT.isVector()) {
913 MVT VT = Out.VT.getVectorElementType();
914 ISD::OutputArg NewOut = Out;
915 NewOut.Flags.setSplit();
916 NewOut.VT = VT;
917
918 // We want the original number of vector elements here, e.g.
919 // three or five, not four or eight.
920 unsigned NumElements = Out.ArgVT.getVectorNumElements();
921
922 for (unsigned j = 0; j != NumElements; ++j) {
923 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
924 DAG.getConstant(j, DL, MVT::i32));
925 SplitVals.push_back(Elem);
926 Splits.push_back(NewOut);
927 NewOut.PartOffset += NewOut.VT.getStoreSize();
928 }
929 } else {
930 SplitVals.push_back(OutVals[i]);
931 Splits.push_back(Out);
932 }
933 }
934
935 // CCValAssign - represent the assignment of the return value to a location.
936 SmallVector<CCValAssign, 48> RVLocs;
937
938 // CCState - Info about the registers and stack slots.
939 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
940 *DAG.getContext());
941
942 // Analyze outgoing return values.
943 AnalyzeReturn(CCInfo, Splits);
944
945 SDValue Flag;
946 SmallVector<SDValue, 48> RetOps;
947 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
948
949 // Copy the result values into the output registers.
950 for (unsigned i = 0, realRVLocIdx = 0;
951 i != RVLocs.size();
952 ++i, ++realRVLocIdx) {
953 CCValAssign &VA = RVLocs[i];
954 assert(VA.isRegLoc() && "Can only return in registers!");
955
956 SDValue Arg = SplitVals[realRVLocIdx];
957
958 // Copied from other backends.
959 switch (VA.getLocInfo()) {
960 default: llvm_unreachable("Unknown loc info!");
961 case CCValAssign::Full:
962 break;
963 case CCValAssign::BCvt:
964 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
965 break;
966 }
967
968 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
969 Flag = Chain.getValue(1);
970 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
971 }
972
973 // Update chain and glue.
974 RetOps[0] = Chain;
975 if (Flag.getNode())
976 RetOps.push_back(Flag);
977
978 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, RetOps);
979}
980
Tom Stellard75aadc22012-12-11 21:25:42 +0000981MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
982 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000983
Tom Stellard75aadc22012-12-11 21:25:42 +0000984 switch (MI->getOpcode()) {
985 default:
986 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Matt Arsenault20711b72015-02-20 22:10:45 +0000987 case AMDGPU::BRANCH:
988 return BB;
Tom Stellard75aadc22012-12-11 21:25:42 +0000989 }
990 return BB;
991}
992
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000993bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
994 // This currently forces unfolding various combinations of fsub into fma with
995 // free fneg'd operands. As long as we have fast FMA (controlled by
996 // isFMAFasterThanFMulAndFAdd), we should perform these.
997
998 // When fma is quarter rate, for f64 where add / sub are at best half rate,
999 // most of these combines appear to be cycle neutral but save on instruction
1000 // count / code size.
1001 return true;
1002}
1003
Mehdi Amini44ede332015-07-09 02:09:04 +00001004EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1005 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001006 if (!VT.isVector()) {
1007 return MVT::i1;
1008 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001009 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001010}
1011
Mehdi Aminieaabc512015-07-09 15:12:23 +00001012MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001013 return MVT::i32;
1014}
1015
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001016// Answering this is somewhat tricky and depends on the specific device which
1017// have different rates for fma or all f64 operations.
1018//
1019// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1020// regardless of which device (although the number of cycles differs between
1021// devices), so it is always profitable for f64.
1022//
1023// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1024// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1025// which we can always do even without fused FP ops since it returns the same
1026// result as the separate operations and since it is always full
1027// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1028// however does not support denormals, so we do report fma as faster if we have
1029// a fast fma device and require denormals.
1030//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001031bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1032 VT = VT.getScalarType();
1033
1034 if (!VT.isSimple())
1035 return false;
1036
1037 switch (VT.getSimpleVT().SimpleTy) {
1038 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001039 // This is as fast on some subtargets. However, we always have full rate f32
1040 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001041 // which we should prefer over fma. We can't use this if we want to support
1042 // denormals, so only report this in these cases.
1043 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001044 case MVT::f64:
1045 return true;
1046 default:
1047 break;
1048 }
1049
1050 return false;
1051}
1052
Tom Stellard75aadc22012-12-11 21:25:42 +00001053//===----------------------------------------------------------------------===//
1054// Custom DAG Lowering Operations
1055//===----------------------------------------------------------------------===//
1056
1057SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1058 switch (Op.getOpcode()) {
1059 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001060 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001061 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001062 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001063 SDValue Result = LowerLOAD(Op, DAG);
1064 assert((!Result.getNode() ||
1065 Result.getNode()->getNumValues() == 2) &&
1066 "Load should return a value and a chain");
1067 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001068 }
Tom Stellardaf775432013-10-23 00:44:32 +00001069
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001070 case ISD::FSIN:
1071 case ISD::FCOS:
1072 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001073 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001074 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001075 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001076 case ISD::GlobalAddress: {
1077 MachineFunction &MF = DAG.getMachineFunction();
1078 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1079 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001080 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001081 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
1082 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001083 }
1084 return SDValue();
1085}
1086
Tom Stellardf8794352012-12-19 22:10:31 +00001087/// \brief Helper function for LowerBRCOND
1088static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001089
Tom Stellardf8794352012-12-19 22:10:31 +00001090 SDNode *Parent = Value.getNode();
1091 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1092 I != E; ++I) {
1093
1094 if (I.getUse().get() != Value)
1095 continue;
1096
1097 if (I->getOpcode() == Opcode)
1098 return *I;
1099 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001100 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001101}
1102
Tom Stellardb02094e2014-07-21 15:45:01 +00001103SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
1104
Tom Stellardc98ee202015-07-16 19:40:07 +00001105 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +00001106 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
1107 unsigned FrameIndex = FINode->getIndex();
1108
Tom Stellardc98ee202015-07-16 19:40:07 +00001109 // A FrameIndex node represents a 32-bit offset into scratch memory. If
1110 // the high bit of a frame index offset were to be set, this would mean
1111 // that it represented an offset of ~2GB * 64 = ~128GB from the start of the
1112 // scratch buffer, with 64 being the number of threads per wave.
1113 //
1114 // If we know the machine uses less than 128GB of scratch, then we can
1115 // amrk the high bit of the FrameIndex node as known zero,
1116 // which is important, because it means in most situations we can
1117 // prove that values derived from FrameIndex nodes are non-negative.
1118 // This enables us to take advantage of more addressing modes when
1119 // accessing scratch buffers, since for scratch reads/writes, the register
1120 // offset must always be positive.
1121
1122 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
1123 if (Subtarget->enableHugeScratchBuffer())
1124 return TFI;
1125
1126 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
1127 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), 31)));
Tom Stellardb02094e2014-07-21 15:45:01 +00001128}
1129
Tom Stellardf8794352012-12-19 22:10:31 +00001130/// This transforms the control flow intrinsics to get the branch destination as
1131/// last parameter, also switches branch target with BR if the need arise
1132SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1133 SelectionDAG &DAG) const {
1134
Andrew Trickef9de2a2013-05-25 02:42:55 +00001135 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001136
1137 SDNode *Intr = BRCOND.getOperand(1).getNode();
1138 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001139 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001140
1141 if (Intr->getOpcode() == ISD::SETCC) {
1142 // As long as we negate the condition everything is fine
1143 SDNode *SetCC = Intr;
1144 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +00001145 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1146 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +00001147 Intr = SetCC->getOperand(0).getNode();
1148
1149 } else {
1150 // Get the target from BR if we don't negate the condition
1151 BR = findUser(BRCOND, ISD::BR);
1152 Target = BR->getOperand(1);
1153 }
1154
1155 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
1156
1157 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001158 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001159
1160 // operands of the new intrinsic call
1161 SmallVector<SDValue, 4> Ops;
1162 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001163 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001164 Ops.push_back(Target);
1165
1166 // build the new intrinsic call
1167 SDNode *Result = DAG.getNode(
1168 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001169 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001170
1171 if (BR) {
1172 // Give the branch instruction our target
1173 SDValue Ops[] = {
1174 BR->getOperand(0),
1175 BRCOND.getOperand(2)
1176 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001177 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1178 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1179 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001180 }
1181
1182 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1183
1184 // Copy the intrinsic results to registers
1185 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1186 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1187 if (!CopyToReg)
1188 continue;
1189
1190 Chain = DAG.getCopyToReg(
1191 Chain, DL,
1192 CopyToReg->getOperand(1),
1193 SDValue(Result, i - 1),
1194 SDValue());
1195
1196 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1197 }
1198
1199 // Remove the old intrinsic from the chain
1200 DAG.ReplaceAllUsesOfValueWith(
1201 SDValue(Intr, Intr->getNumValues() - 1),
1202 Intr->getOperand(0));
1203
1204 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001205}
1206
Tom Stellard067c8152014-07-21 14:01:14 +00001207SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1208 SDValue Op,
1209 SelectionDAG &DAG) const {
1210 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1211
1212 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
1213 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1214
1215 SDLoc DL(GSD);
1216 const GlobalValue *GV = GSD->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001217 MVT PtrVT = getPointerTy(DAG.getDataLayout(), GSD->getAddressSpace());
Tom Stellard067c8152014-07-21 14:01:14 +00001218
Tom Stellard067c8152014-07-21 14:01:14 +00001219 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
Tom Stellardc93fc112015-12-10 02:13:01 +00001220 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT, GA);
Tom Stellard067c8152014-07-21 14:01:14 +00001221}
1222
Tom Stellardfc92e772015-05-12 14:18:14 +00001223SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL,
1224 SDValue V) const {
1225 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1226 // so we will end up with redundant moves to m0.
1227 //
1228 // We can't use S_MOV_B32, because there is no way to specify m0 as the
1229 // destination register.
1230 //
1231 // We have to use them both. Machine cse will combine all the S_MOV_B32
1232 // instructions and the register coalescer eliminate the extra copies.
1233 SDNode *M0 = DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, V.getValueType(), V);
1234 return DAG.getCopyToReg(Chain, DL, DAG.getRegister(AMDGPU::M0, MVT::i32),
1235 SDValue(M0, 0), SDValue()); // Glue
1236 // A Null SDValue creates
1237 // a glue result.
1238}
1239
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001240SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1241 SDValue Op,
1242 MVT VT,
1243 unsigned Offset) const {
1244 SDLoc SL(Op);
1245 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1246 DAG.getEntryNode(), Offset, false);
1247 // The local size values will have the hi 16-bits as zero.
1248 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1249 DAG.getValueType(VT));
1250}
1251
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001252SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1253 SelectionDAG &DAG) const {
1254 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001255 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardec2e43c2014-09-22 15:35:29 +00001256 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +00001257 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001258
1259 EVT VT = Op.getValueType();
1260 SDLoc DL(Op);
1261 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1262
Sanjay Patela2607012015-09-16 16:31:21 +00001263 // TODO: Should this propagate fast-math-flags?
1264
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001265 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001266 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault800fecf2016-01-11 21:18:33 +00001267 if (!Subtarget->isAmdHsaOS()) {
1268 DiagnosticInfoUnsupported BadIntrin(*MF.getFunction(),
1269 "hsa intrinsic without hsa target");
1270 DAG.getContext()->diagnose(BadIntrin);
1271 return DAG.getUNDEF(VT);
1272 }
1273
Tom Stellard48f29f22015-11-26 00:43:29 +00001274 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
1275 TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_PTR), VT);
1276
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001277 case Intrinsic::r600_read_ngroups_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001278 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1279 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001280 case Intrinsic::r600_read_ngroups_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001281 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1282 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001283 case Intrinsic::r600_read_ngroups_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001284 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1285 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001286 case Intrinsic::r600_read_global_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001287 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1288 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001289 case Intrinsic::r600_read_global_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001290 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1291 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001292 case Intrinsic::r600_read_global_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001293 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1294 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001295 case Intrinsic::r600_read_local_size_x:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001296 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1297 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001298 case Intrinsic::r600_read_local_size_y:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001299 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1300 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001301 case Intrinsic::r600_read_local_size_z:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001302 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1303 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Jan Veselye5121f32014-10-14 20:05:26 +00001304 case Intrinsic::AMDGPU_read_workdim:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001305 // Really only 2 bits.
1306 return lowerImplicitZextParam(DAG, Op, MVT::i8,
1307 getImplicitParameterOffset(MFI, GRID_DIM));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001308 case Intrinsic::r600_read_tgid_x:
1309 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001310 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001311 case Intrinsic::r600_read_tgid_y:
1312 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001313 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001314 case Intrinsic::r600_read_tgid_z:
1315 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001316 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001317 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001318 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001319 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001320 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001321 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001322 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001323 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001324 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001325 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001326 case AMDGPUIntrinsic::SI_load_const: {
1327 SDValue Ops[] = {
1328 Op.getOperand(1),
1329 Op.getOperand(2)
1330 };
1331
1332 MachineMemOperand *MMO = MF.getMachineMemOperand(
1333 MachinePointerInfo(),
1334 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
1335 VT.getStoreSize(), 4);
1336 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
1337 Op->getVTList(), Ops, VT, MMO);
1338 }
1339 case AMDGPUIntrinsic::SI_sample:
1340 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
1341 case AMDGPUIntrinsic::SI_sampleb:
1342 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
1343 case AMDGPUIntrinsic::SI_sampled:
1344 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
1345 case AMDGPUIntrinsic::SI_samplel:
1346 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
1347 case AMDGPUIntrinsic::SI_vs_load_input:
1348 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1349 Op.getOperand(1),
1350 Op.getOperand(2),
1351 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00001352
1353 case AMDGPUIntrinsic::AMDGPU_fract:
1354 case AMDGPUIntrinsic::AMDIL_fraction: // Legacy name.
1355 return DAG.getNode(ISD::FSUB, DL, VT, Op.getOperand(1),
1356 DAG.getNode(ISD::FFLOOR, DL, VT, Op.getOperand(1)));
Tom Stellard2a9d9472015-05-12 15:00:46 +00001357 case AMDGPUIntrinsic::SI_fs_constant: {
1358 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1359 SDValue Glue = M0.getValue(1);
1360 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
1361 DAG.getConstant(2, DL, MVT::i32), // P0
1362 Op.getOperand(1), Op.getOperand(2), Glue);
1363 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00001364 case AMDGPUIntrinsic::SI_packf16:
1365 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
1366 return DAG.getUNDEF(MVT::i32);
1367 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00001368 case AMDGPUIntrinsic::SI_fs_interp: {
1369 SDValue IJ = Op.getOperand(4);
1370 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1371 DAG.getConstant(0, DL, MVT::i32));
1372 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1373 DAG.getConstant(1, DL, MVT::i32));
1374 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1375 SDValue Glue = M0.getValue(1);
1376 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
1377 DAG.getVTList(MVT::f32, MVT::Glue),
1378 I, Op.getOperand(1), Op.getOperand(2), Glue);
1379 Glue = SDValue(P1.getNode(), 1);
1380 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
1381 Op.getOperand(1), Op.getOperand(2), Glue);
1382 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00001383 case Intrinsic::amdgcn_interp_p1: {
1384 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
1385 SDValue Glue = M0.getValue(1);
1386 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
1387 Op.getOperand(2), Op.getOperand(3), Glue);
1388 }
1389 case Intrinsic::amdgcn_interp_p2: {
1390 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
1391 SDValue Glue = SDValue(M0.getNode(), 1);
1392 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
1393 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
1394 Glue);
1395 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001396 default:
1397 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1398 }
1399}
1400
1401SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1402 SelectionDAG &DAG) const {
1403 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00001404 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001405 SDValue Chain = Op.getOperand(0);
1406 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1407
1408 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00001409 case AMDGPUIntrinsic::SI_sendmsg: {
1410 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1411 SDValue Glue = Chain.getValue(1);
1412 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1413 Op.getOperand(2), Glue);
1414 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001415 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001416 SDValue Ops[] = {
1417 Chain,
1418 Op.getOperand(2),
1419 Op.getOperand(3),
1420 Op.getOperand(4),
1421 Op.getOperand(5),
1422 Op.getOperand(6),
1423 Op.getOperand(7),
1424 Op.getOperand(8),
1425 Op.getOperand(9),
1426 Op.getOperand(10),
1427 Op.getOperand(11),
1428 Op.getOperand(12),
1429 Op.getOperand(13),
1430 Op.getOperand(14)
1431 };
1432
1433 EVT VT = Op.getOperand(3).getValueType();
1434
1435 MachineMemOperand *MMO = MF.getMachineMemOperand(
1436 MachinePointerInfo(),
1437 MachineMemOperand::MOStore,
1438 VT.getStoreSize(), 4);
1439 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1440 Op->getVTList(), Ops, VT, MMO);
1441 }
1442 default:
1443 return SDValue();
1444 }
1445}
1446
Tom Stellard81d871d2013-11-13 23:36:50 +00001447SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1448 SDLoc DL(Op);
1449 LoadSDNode *Load = cast<LoadSDNode>(Op);
1450
Tom Stellarde812f2f2014-07-21 15:45:06 +00001451 if (Op.getValueType().isVector()) {
1452 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1453 "Custom lowering for non-i32 vectors hasn't been implemented.");
1454 unsigned NumElements = Op.getValueType().getVectorNumElements();
1455 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001456
Tom Stellarde812f2f2014-07-21 15:45:06 +00001457 switch (Load->getAddressSpace()) {
1458 default: break;
Tom Stellarda6f24c62015-12-15 20:55:55 +00001459 case AMDGPUAS::CONSTANT_ADDRESS:
1460 if (isMemOpUniform(Load))
1461 break;
1462 // Non-uniform loads will be selected to MUBUF instructions, so they
1463 // have the same legalization requires ments as global and private
1464 // loads.
1465 //
1466 // Fall-through
Tom Stellarde812f2f2014-07-21 15:45:06 +00001467 case AMDGPUAS::GLOBAL_ADDRESS:
1468 case AMDGPUAS::PRIVATE_ADDRESS:
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001469 if (NumElements >= 8)
1470 return SplitVectorLoad(Op, DAG);
1471
Tom Stellarde812f2f2014-07-21 15:45:06 +00001472 // v4 loads are supported for private and global memory.
1473 if (NumElements <= 4)
1474 break;
1475 // fall-through
1476 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenaultff05da82015-11-24 12:18:54 +00001477 // If properly aligned, if we split we might be able to use ds_read_b64.
1478 return SplitVectorLoad(Op, DAG);
Tom Stellarde812f2f2014-07-21 15:45:06 +00001479 }
Tom Stellarde9373602014-01-22 19:24:14 +00001480 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001481
Tom Stellarde812f2f2014-07-21 15:45:06 +00001482 return AMDGPUTargetLowering::LowerLOAD(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001483}
1484
Tom Stellard9fa17912013-08-14 23:24:45 +00001485SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
1486 const SDValue &Op,
1487 SelectionDAG &DAG) const {
1488 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
1489 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +00001490 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +00001491 Op.getOperand(4));
1492}
1493
Tom Stellard0ec134f2014-02-04 17:18:40 +00001494SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1495 if (Op.getValueType() != MVT::i64)
1496 return SDValue();
1497
1498 SDLoc DL(Op);
1499 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001500
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001501 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1502 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001503
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001504 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1505 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1506
1507 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1508 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001509
1510 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1511
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001512 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1513 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001514
1515 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1516
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001517 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1518 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001519}
1520
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001521// Catch division cases where we can use shortcuts with rcp and rsq
1522// instructions.
1523SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001524 SDLoc SL(Op);
1525 SDValue LHS = Op.getOperand(0);
1526 SDValue RHS = Op.getOperand(1);
1527 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001528 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001529
1530 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001531 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1532 CLHS->isExactlyValue(1.0)) {
1533 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1534 // the CI documentation has a worst case error of 1 ulp.
1535 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1536 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001537
1538 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001539 //
1540 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1541 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001542 if (RHS.getOpcode() == ISD::FSQRT)
1543 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1544
1545 // 1.0 / x -> rcp(x)
1546 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1547 }
1548 }
1549
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001550 if (Unsafe) {
1551 // Turn into multiply by the reciprocal.
1552 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00001553 SDNodeFlags Flags;
1554 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001555 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00001556 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001557 }
1558
1559 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001560}
1561
1562SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001563 SDValue FastLowered = LowerFastFDIV(Op, DAG);
1564 if (FastLowered.getNode())
1565 return FastLowered;
1566
1567 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1568 // selection error for now rather than do something incorrect.
1569 if (Subtarget->hasFP32Denormals())
1570 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001571
1572 SDLoc SL(Op);
1573 SDValue LHS = Op.getOperand(0);
1574 SDValue RHS = Op.getOperand(1);
1575
1576 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1577
1578 const APFloat K0Val(BitsToFloat(0x6f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001579 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001580
1581 const APFloat K1Val(BitsToFloat(0x2f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001582 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001583
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001584 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001585
Mehdi Amini44ede332015-07-09 02:09:04 +00001586 EVT SetCCVT =
1587 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001588
1589 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1590
1591 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1592
Sanjay Patela2607012015-09-16 16:31:21 +00001593 // TODO: Should this propagate fast-math-flags?
1594
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001595 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1596
1597 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1598
1599 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1600
1601 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1602}
1603
1604SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001605 if (DAG.getTarget().Options.UnsafeFPMath)
1606 return LowerFastFDIV(Op, DAG);
1607
1608 SDLoc SL(Op);
1609 SDValue X = Op.getOperand(0);
1610 SDValue Y = Op.getOperand(1);
1611
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001612 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001613
1614 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
1615
1616 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
1617
1618 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
1619
1620 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
1621
1622 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
1623
1624 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
1625
1626 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
1627
1628 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
1629
1630 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
1631 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
1632
1633 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
1634 NegDivScale0, Mul, DivScale1);
1635
1636 SDValue Scale;
1637
1638 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1639 // Workaround a hardware bug on SI where the condition output from div_scale
1640 // is not usable.
1641
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001642 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001643
1644 // Figure out if the scale to use for div_fmas.
1645 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1646 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
1647 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
1648 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
1649
1650 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
1651 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
1652
1653 SDValue Scale0Hi
1654 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
1655 SDValue Scale1Hi
1656 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
1657
1658 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
1659 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
1660 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
1661 } else {
1662 Scale = DivScale1.getValue(1);
1663 }
1664
1665 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
1666 Fma4, Fma3, Mul, Scale);
1667
1668 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001669}
1670
1671SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1672 EVT VT = Op.getValueType();
1673
1674 if (VT == MVT::f32)
1675 return LowerFDIV32(Op, DAG);
1676
1677 if (VT == MVT::f64)
1678 return LowerFDIV64(Op, DAG);
1679
1680 llvm_unreachable("Unexpected type for fdiv");
1681}
1682
Tom Stellard81d871d2013-11-13 23:36:50 +00001683SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1684 SDLoc DL(Op);
1685 StoreSDNode *Store = cast<StoreSDNode>(Op);
1686 EVT VT = Store->getMemoryVT();
1687
Tom Stellard9b3816b2014-06-24 23:33:04 +00001688 // These stores are legal.
Tom Stellardb02094e2014-07-21 15:45:01 +00001689 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1690 if (VT.isVector() && VT.getVectorNumElements() > 4)
Matt Arsenault83e60582014-07-24 17:10:35 +00001691 return ScalarizeVectorStore(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001692 return SDValue();
1693 }
1694
Tom Stellard81d871d2013-11-13 23:36:50 +00001695 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1696 if (Ret.getNode())
1697 return Ret;
1698
1699 if (VT.isVector() && VT.getVectorNumElements() >= 8)
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001700 return SplitVectorStore(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001701
Tom Stellard1c8788e2014-03-07 20:12:33 +00001702 if (VT == MVT::i1)
1703 return DAG.getTruncStore(Store->getChain(), DL,
1704 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1705 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1706
Tom Stellarde812f2f2014-07-21 15:45:06 +00001707 return SDValue();
Tom Stellard81d871d2013-11-13 23:36:50 +00001708}
1709
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001710SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001711 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001712 EVT VT = Op.getValueType();
1713 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00001714 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001715 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
1716 DAG.getNode(ISD::FMUL, DL, VT, Arg,
1717 DAG.getConstantFP(0.5/M_PI, DL,
1718 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001719
1720 switch (Op.getOpcode()) {
1721 case ISD::FCOS:
1722 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1723 case ISD::FSIN:
1724 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1725 default:
1726 llvm_unreachable("Wrong trig opcode");
1727 }
1728}
1729
Tom Stellard75aadc22012-12-11 21:25:42 +00001730//===----------------------------------------------------------------------===//
1731// Custom DAG optimizations
1732//===----------------------------------------------------------------------===//
1733
Matt Arsenault364a6742014-06-11 17:50:44 +00001734SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00001735 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00001736 EVT VT = N->getValueType(0);
1737 EVT ScalarVT = VT.getScalarType();
1738 if (ScalarVT != MVT::f32)
1739 return SDValue();
1740
1741 SelectionDAG &DAG = DCI.DAG;
1742 SDLoc DL(N);
1743
1744 SDValue Src = N->getOperand(0);
1745 EVT SrcVT = Src.getValueType();
1746
1747 // TODO: We could try to match extracting the higher bytes, which would be
1748 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1749 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1750 // about in practice.
1751 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1752 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1753 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1754 DCI.AddToWorklist(Cvt.getNode());
1755 return Cvt;
1756 }
1757 }
1758
1759 // We are primarily trying to catch operations on illegal vector types
1760 // before they are expanded.
1761 // For scalars, we can use the more flexible method of checking masked bits
1762 // after legalization.
1763 if (!DCI.isBeforeLegalize() ||
1764 !SrcVT.isVector() ||
1765 SrcVT.getVectorElementType() != MVT::i8) {
1766 return SDValue();
1767 }
1768
1769 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1770
1771 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1772 // size as 4.
1773 unsigned NElts = SrcVT.getVectorNumElements();
1774 if (!SrcVT.isSimple() && NElts != 3)
1775 return SDValue();
1776
1777 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1778 // prevent a mess from expanding to v4i32 and repacking.
1779 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1780 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1781 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1782 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00001783 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001784
1785 unsigned AS = Load->getAddressSpace();
1786 unsigned Align = Load->getAlignment();
1787 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001788 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001789
1790 // Don't try to replace the load if we have to expand it due to alignment
1791 // problems. Otherwise we will end up scalarizing the load, and trying to
1792 // repack into the vector for no real reason.
1793 if (Align < ABIAlignment &&
1794 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
1795 return SDValue();
1796 }
1797
Matt Arsenault364a6742014-06-11 17:50:44 +00001798 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1799 Load->getChain(),
1800 Load->getBasePtr(),
1801 LoadVT,
1802 Load->getMemOperand());
1803
1804 // Make sure successors of the original load stay after it by updating
1805 // them to use the new Chain.
1806 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1807
1808 SmallVector<SDValue, 4> Elts;
1809 if (RegVT.isVector())
1810 DAG.ExtractVectorElements(NewLoad, Elts);
1811 else
1812 Elts.push_back(NewLoad);
1813
1814 SmallVector<SDValue, 4> Ops;
1815
1816 unsigned EltIdx = 0;
1817 for (SDValue Elt : Elts) {
1818 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1819 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1820 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1821 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1822 DCI.AddToWorklist(Cvt.getNode());
1823 Ops.push_back(Cvt);
1824 }
1825
1826 ++EltIdx;
1827 }
1828
1829 assert(Ops.size() == NElts);
1830
1831 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1832 }
1833
1834 return SDValue();
1835}
1836
Eric Christopher6c5b5112015-03-11 18:43:21 +00001837/// \brief Return true if the given offset Size in bytes can be folded into
1838/// the immediate offsets of a memory instruction for the given address space.
1839static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
1840 const AMDGPUSubtarget &STI) {
1841 switch (AS) {
1842 case AMDGPUAS::GLOBAL_ADDRESS: {
1843 // MUBUF instructions a 12-bit offset in bytes.
1844 return isUInt<12>(OffsetSize);
1845 }
1846 case AMDGPUAS::CONSTANT_ADDRESS: {
1847 // SMRD instructions have an 8-bit offset in dwords on SI and
1848 // a 20-bit offset in bytes on VI.
1849 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
1850 return isUInt<20>(OffsetSize);
1851 else
1852 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
1853 }
1854 case AMDGPUAS::LOCAL_ADDRESS:
1855 case AMDGPUAS::REGION_ADDRESS: {
1856 // The single offset versions have a 16-bit offset in bytes.
1857 return isUInt<16>(OffsetSize);
1858 }
1859 case AMDGPUAS::PRIVATE_ADDRESS:
1860 // Indirect register addressing does not use any offsets.
1861 default:
1862 return 0;
1863 }
1864}
1865
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001866// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
1867
1868// This is a variant of
1869// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
1870//
1871// The normal DAG combiner will do this, but only if the add has one use since
1872// that would increase the number of instructions.
1873//
1874// This prevents us from seeing a constant offset that can be folded into a
1875// memory instruction's addressing mode. If we know the resulting add offset of
1876// a pointer can be folded into an addressing offset, we can replace the pointer
1877// operand with the add of new constant offset. This eliminates one of the uses,
1878// and may allow the remaining use to also be simplified.
1879//
1880SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
1881 unsigned AddrSpace,
1882 DAGCombinerInfo &DCI) const {
1883 SDValue N0 = N->getOperand(0);
1884 SDValue N1 = N->getOperand(1);
1885
1886 if (N0.getOpcode() != ISD::ADD)
1887 return SDValue();
1888
1889 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
1890 if (!CN1)
1891 return SDValue();
1892
1893 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1894 if (!CAdd)
1895 return SDValue();
1896
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001897 // If the resulting offset is too large, we can't fold it into the addressing
1898 // mode offset.
1899 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Eric Christopher6c5b5112015-03-11 18:43:21 +00001900 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001901 return SDValue();
1902
1903 SelectionDAG &DAG = DCI.DAG;
1904 SDLoc SL(N);
1905 EVT VT = N->getValueType(0);
1906
1907 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001908 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001909
1910 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
1911}
1912
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001913SDValue SITargetLowering::performAndCombine(SDNode *N,
1914 DAGCombinerInfo &DCI) const {
1915 if (DCI.isBeforeLegalize())
1916 return SDValue();
1917
1918 SelectionDAG &DAG = DCI.DAG;
1919
1920 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
1921 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
1922 SDValue LHS = N->getOperand(0);
1923 SDValue RHS = N->getOperand(1);
1924
1925 if (LHS.getOpcode() == ISD::SETCC &&
1926 RHS.getOpcode() == ISD::SETCC) {
1927 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
1928 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
1929
1930 SDValue X = LHS.getOperand(0);
1931 SDValue Y = RHS.getOperand(0);
1932 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
1933 return SDValue();
1934
1935 if (LCC == ISD::SETO) {
1936 if (X != LHS.getOperand(1))
1937 return SDValue();
1938
1939 if (RCC == ISD::SETUNE) {
1940 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
1941 if (!C1 || !C1->isInfinity() || C1->isNegative())
1942 return SDValue();
1943
1944 const uint32_t Mask = SIInstrFlags::N_NORMAL |
1945 SIInstrFlags::N_SUBNORMAL |
1946 SIInstrFlags::N_ZERO |
1947 SIInstrFlags::P_ZERO |
1948 SIInstrFlags::P_SUBNORMAL |
1949 SIInstrFlags::P_NORMAL;
1950
1951 static_assert(((~(SIInstrFlags::S_NAN |
1952 SIInstrFlags::Q_NAN |
1953 SIInstrFlags::N_INFINITY |
1954 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
1955 "mask not equal");
1956
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001957 SDLoc DL(N);
1958 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
1959 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001960 }
1961 }
1962 }
1963
1964 return SDValue();
1965}
1966
Matt Arsenaultf2290332015-01-06 23:00:39 +00001967SDValue SITargetLowering::performOrCombine(SDNode *N,
1968 DAGCombinerInfo &DCI) const {
1969 SelectionDAG &DAG = DCI.DAG;
1970 SDValue LHS = N->getOperand(0);
1971 SDValue RHS = N->getOperand(1);
1972
1973 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
1974 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
1975 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
1976 SDValue Src = LHS.getOperand(0);
1977 if (Src != RHS.getOperand(0))
1978 return SDValue();
1979
1980 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
1981 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
1982 if (!CLHS || !CRHS)
1983 return SDValue();
1984
1985 // Only 10 bits are used.
1986 static const uint32_t MaxMask = 0x3ff;
1987
1988 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001989 SDLoc DL(N);
1990 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
1991 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00001992 }
1993
1994 return SDValue();
1995}
1996
1997SDValue SITargetLowering::performClassCombine(SDNode *N,
1998 DAGCombinerInfo &DCI) const {
1999 SelectionDAG &DAG = DCI.DAG;
2000 SDValue Mask = N->getOperand(1);
2001
2002 // fp_class x, 0 -> false
2003 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
2004 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002005 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002006 }
2007
2008 return SDValue();
2009}
2010
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002011static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
2012 switch (Opc) {
2013 case ISD::FMAXNUM:
2014 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002015 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002016 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002017 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002018 return AMDGPUISD::UMAX3;
2019 case ISD::FMINNUM:
2020 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002021 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002022 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002023 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002024 return AMDGPUISD::UMIN3;
2025 default:
2026 llvm_unreachable("Not a min/max opcode");
2027 }
2028}
2029
2030SDValue SITargetLowering::performMin3Max3Combine(SDNode *N,
2031 DAGCombinerInfo &DCI) const {
2032 SelectionDAG &DAG = DCI.DAG;
2033
2034 unsigned Opc = N->getOpcode();
2035 SDValue Op0 = N->getOperand(0);
2036 SDValue Op1 = N->getOperand(1);
2037
2038 // Only do this if the inner op has one use since this will just increases
2039 // register pressure for no benefit.
2040
2041 // max(max(a, b), c)
2042 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
2043 SDLoc DL(N);
2044 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2045 DL,
2046 N->getValueType(0),
2047 Op0.getOperand(0),
2048 Op0.getOperand(1),
2049 Op1);
2050 }
2051
2052 // max(a, max(b, c))
2053 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
2054 SDLoc DL(N);
2055 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2056 DL,
2057 N->getValueType(0),
2058 Op0,
2059 Op1.getOperand(0),
2060 Op1.getOperand(1));
2061 }
2062
2063 return SDValue();
2064}
2065
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002066SDValue SITargetLowering::performSetCCCombine(SDNode *N,
2067 DAGCombinerInfo &DCI) const {
2068 SelectionDAG &DAG = DCI.DAG;
2069 SDLoc SL(N);
2070
2071 SDValue LHS = N->getOperand(0);
2072 SDValue RHS = N->getOperand(1);
2073 EVT VT = LHS.getValueType();
2074
2075 if (VT != MVT::f32 && VT != MVT::f64)
2076 return SDValue();
2077
2078 // Match isinf pattern
2079 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
2080 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
2081 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
2082 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2083 if (!CRHS)
2084 return SDValue();
2085
2086 const APFloat &APF = CRHS->getValueAPF();
2087 if (APF.isInfinity() && !APF.isNegative()) {
2088 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002089 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
2090 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002091 }
2092 }
2093
2094 return SDValue();
2095}
2096
Tom Stellard75aadc22012-12-11 21:25:42 +00002097SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
2098 DAGCombinerInfo &DCI) const {
2099 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002100 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00002101
2102 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00002103 default:
2104 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002105 case ISD::SETCC:
2106 return performSetCCCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002107 case ISD::FMAXNUM: // TODO: What about fmax_legacy?
2108 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002109 case ISD::SMAX:
2110 case ISD::SMIN:
2111 case ISD::UMAX:
2112 case ISD::UMIN: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002113 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00002114 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002115 getTargetMachine().getOptLevel() > CodeGenOpt::None)
2116 return performMin3Max3Combine(N, DCI);
2117 break;
2118 }
Matt Arsenault364a6742014-06-11 17:50:44 +00002119
2120 case AMDGPUISD::CVT_F32_UBYTE0:
2121 case AMDGPUISD::CVT_F32_UBYTE1:
2122 case AMDGPUISD::CVT_F32_UBYTE2:
2123 case AMDGPUISD::CVT_F32_UBYTE3: {
2124 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
2125
2126 SDValue Src = N->getOperand(0);
2127 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
2128
2129 APInt KnownZero, KnownOne;
2130 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2131 !DCI.isBeforeLegalizeOps());
2132 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2133 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
2134 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
2135 DCI.CommitTargetLoweringOpt(TLO);
2136 }
2137
2138 break;
2139 }
2140
2141 case ISD::UINT_TO_FP: {
2142 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002143 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002144 case ISD::FADD: {
2145 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2146 break;
2147
2148 EVT VT = N->getValueType(0);
2149 if (VT != MVT::f32)
2150 break;
2151
Matt Arsenault8d630032015-02-20 22:10:41 +00002152 // Only do this if we are not trying to support denormals. v_mad_f32 does
2153 // not support denormals ever.
2154 if (Subtarget->hasFP32Denormals())
2155 break;
2156
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002157 SDValue LHS = N->getOperand(0);
2158 SDValue RHS = N->getOperand(1);
2159
2160 // These should really be instruction patterns, but writing patterns with
2161 // source modiifiers is a pain.
2162
2163 // fadd (fadd (a, a), b) -> mad 2.0, a, b
2164 if (LHS.getOpcode() == ISD::FADD) {
2165 SDValue A = LHS.getOperand(0);
2166 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002167 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002168 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002169 }
2170 }
2171
2172 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
2173 if (RHS.getOpcode() == ISD::FADD) {
2174 SDValue A = RHS.getOperand(0);
2175 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002176 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002177 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002178 }
2179 }
2180
Matt Arsenault8d630032015-02-20 22:10:41 +00002181 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002182 }
Matt Arsenault8675db12014-08-29 16:01:14 +00002183 case ISD::FSUB: {
2184 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2185 break;
2186
2187 EVT VT = N->getValueType(0);
2188
2189 // Try to get the fneg to fold into the source modifier. This undoes generic
2190 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00002191 //
2192 // Only do this if we are not trying to support denormals. v_mad_f32 does
2193 // not support denormals ever.
2194 if (VT == MVT::f32 &&
2195 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00002196 SDValue LHS = N->getOperand(0);
2197 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002198 if (LHS.getOpcode() == ISD::FADD) {
2199 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
2200
2201 SDValue A = LHS.getOperand(0);
2202 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002203 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002204 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
2205
Matt Arsenault8d630032015-02-20 22:10:41 +00002206 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002207 }
2208 }
2209
2210 if (RHS.getOpcode() == ISD::FADD) {
2211 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
2212
2213 SDValue A = RHS.getOperand(0);
2214 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002215 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002216 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002217 }
2218 }
Matt Arsenault8d630032015-02-20 22:10:41 +00002219
2220 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00002221 }
2222
2223 break;
2224 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002225 case ISD::LOAD:
2226 case ISD::STORE:
2227 case ISD::ATOMIC_LOAD:
2228 case ISD::ATOMIC_STORE:
2229 case ISD::ATOMIC_CMP_SWAP:
2230 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2231 case ISD::ATOMIC_SWAP:
2232 case ISD::ATOMIC_LOAD_ADD:
2233 case ISD::ATOMIC_LOAD_SUB:
2234 case ISD::ATOMIC_LOAD_AND:
2235 case ISD::ATOMIC_LOAD_OR:
2236 case ISD::ATOMIC_LOAD_XOR:
2237 case ISD::ATOMIC_LOAD_NAND:
2238 case ISD::ATOMIC_LOAD_MIN:
2239 case ISD::ATOMIC_LOAD_MAX:
2240 case ISD::ATOMIC_LOAD_UMIN:
2241 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
2242 if (DCI.isBeforeLegalize())
2243 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002244
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002245 MemSDNode *MemNode = cast<MemSDNode>(N);
2246 SDValue Ptr = MemNode->getBasePtr();
2247
2248 // TODO: We could also do this for multiplies.
2249 unsigned AS = MemNode->getAddressSpace();
2250 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
2251 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
2252 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002253 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002254
2255 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
2256 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
2257 }
2258 }
2259 break;
2260 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002261 case ISD::AND:
2262 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002263 case ISD::OR:
2264 return performOrCombine(N, DCI);
2265 case AMDGPUISD::FP_CLASS:
2266 return performClassCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002267 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002268 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00002269}
Christian Konigd910b7d2013-02-26 17:52:16 +00002270
Christian Konigf82901a2013-02-26 17:52:23 +00002271/// \brief Analyze the possible immediate value Op
2272///
2273/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
2274/// and the immediate value if it's a literal immediate
2275int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
2276
Eric Christopher7792e322015-01-30 23:24:40 +00002277 const SIInstrInfo *TII =
2278 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00002279
Tom Stellardedbf1eb2013-04-05 23:31:20 +00002280 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00002281 if (TII->isInlineConstant(Node->getAPIntValue()))
2282 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00002283
Matt Arsenault11a4d672015-02-13 19:05:03 +00002284 uint64_t Val = Node->getZExtValue();
2285 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00002286 }
2287
2288 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
2289 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
2290 return 0;
2291
2292 if (Node->getValueType(0) == MVT::f32)
2293 return FloatToBits(Node->getValueAPF().convertToFloat());
2294
2295 return -1;
2296 }
2297
2298 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00002299}
2300
Christian Konig8e06e2a2013-04-10 08:39:08 +00002301/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00002302static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00002303 switch (Idx) {
2304 default: return 0;
2305 case AMDGPU::sub0: return 0;
2306 case AMDGPU::sub1: return 1;
2307 case AMDGPU::sub2: return 2;
2308 case AMDGPU::sub3: return 3;
2309 }
2310}
2311
2312/// \brief Adjust the writemask of MIMG instructions
2313void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
2314 SelectionDAG &DAG) const {
2315 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00002316 unsigned Lane = 0;
2317 unsigned OldDmask = Node->getConstantOperandVal(0);
2318 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002319
2320 // Try to figure out the used register components
2321 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
2322 I != E; ++I) {
2323
2324 // Abort if we can't understand the usage
2325 if (!I->isMachineOpcode() ||
2326 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
2327 return;
2328
Tom Stellard54774e52013-10-23 02:53:47 +00002329 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
2330 // Note that subregs are packed, i.e. Lane==0 is the first bit set
2331 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
2332 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00002333 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00002334
Tom Stellard54774e52013-10-23 02:53:47 +00002335 // Set which texture component corresponds to the lane.
2336 unsigned Comp;
2337 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
2338 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00002339 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00002340 Dmask &= ~(1 << Comp);
2341 }
2342
Christian Konig8e06e2a2013-04-10 08:39:08 +00002343 // Abort if we have more than one user per component
2344 if (Users[Lane])
2345 return;
2346
2347 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00002348 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002349 }
2350
Tom Stellard54774e52013-10-23 02:53:47 +00002351 // Abort if there's no change
2352 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00002353 return;
2354
2355 // Adjust the writemask in the node
2356 std::vector<SDValue> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002357 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002358 Ops.insert(Ops.end(), Node->op_begin() + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00002359 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002360
Christian Konig8b1ed282013-04-10 08:39:16 +00002361 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00002362 // (if NewDmask has only one bit set...)
2363 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002364 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
2365 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00002366 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002367 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00002368 SDValue(Node, 0), RC);
2369 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
2370 return;
2371 }
2372
Christian Konig8e06e2a2013-04-10 08:39:08 +00002373 // Update the users of the node with the new indices
2374 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
2375
2376 SDNode *User = Users[i];
2377 if (!User)
2378 continue;
2379
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002380 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002381 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
2382
2383 switch (Idx) {
2384 default: break;
2385 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
2386 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
2387 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
2388 }
2389 }
2390}
2391
Tom Stellardc98ee202015-07-16 19:40:07 +00002392static bool isFrameIndexOp(SDValue Op) {
2393 if (Op.getOpcode() == ISD::AssertZext)
2394 Op = Op.getOperand(0);
2395
2396 return isa<FrameIndexSDNode>(Op);
2397}
2398
Tom Stellard3457a842014-10-09 19:06:00 +00002399/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
2400/// with frame index operands.
2401/// LLVM assumes that inputs are to these instructions are registers.
2402void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
2403 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002404
2405 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00002406 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00002407 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00002408 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002409 continue;
2410 }
2411
Tom Stellard3457a842014-10-09 19:06:00 +00002412 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002413 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00002414 Node->getOperand(i).getValueType(),
2415 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002416 }
2417
Tom Stellard3457a842014-10-09 19:06:00 +00002418 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002419}
2420
Matt Arsenault08d84942014-06-03 23:06:13 +00002421/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00002422SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
2423 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002424 const SIInstrInfo *TII =
2425 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konig8e06e2a2013-04-10 08:39:08 +00002426
Tom Stellard16a9a202013-08-14 23:24:17 +00002427 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00002428 adjustWritemask(Node, DAG);
2429
Matt Arsenault7d858d82014-11-02 23:46:54 +00002430 if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG ||
2431 Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002432 legalizeTargetIndependentNode(Node, DAG);
2433 return Node;
2434 }
Tom Stellard654d6692015-01-08 15:08:17 +00002435 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002436}
Christian Konig8b1ed282013-04-10 08:39:16 +00002437
2438/// \brief Assign the register class depending on the number of
2439/// bits set in the writemask
2440void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
2441 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002442 const SIInstrInfo *TII =
2443 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002444
Tom Stellarda99ada52014-11-21 22:31:44 +00002445 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00002446
2447 if (TII->isVOP3(MI->getOpcode())) {
2448 // Make sure constant bus requirements are respected.
2449 TII->legalizeOperandsVOP3(MRI, MI);
2450 return;
2451 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00002452
Matt Arsenault3add6432015-10-20 04:35:43 +00002453 if (TII->isMIMG(*MI)) {
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002454 unsigned VReg = MI->getOperand(0).getReg();
2455 unsigned Writemask = MI->getOperand(1).getImm();
2456 unsigned BitsSet = 0;
2457 for (unsigned i = 0; i < 4; ++i)
2458 BitsSet += Writemask & (1 << i) ? 1 : 0;
2459
2460 const TargetRegisterClass *RC;
2461 switch (BitsSet) {
2462 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002463 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002464 case 2: RC = &AMDGPU::VReg_64RegClass; break;
2465 case 3: RC = &AMDGPU::VReg_96RegClass; break;
2466 }
2467
2468 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
2469 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002470 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00002471 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00002472 }
2473
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002474 // Replace unused atomics with the no return version.
2475 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
2476 if (NoRetAtomicOp != -1) {
2477 if (!Node->hasAnyUseOfValue(0)) {
2478 MI->setDesc(TII->get(NoRetAtomicOp));
2479 MI->RemoveOperand(0);
2480 }
2481
2482 return;
2483 }
Christian Konig8b1ed282013-04-10 08:39:16 +00002484}
Tom Stellard0518ff82013-06-03 17:39:58 +00002485
Matt Arsenault485defe2014-11-05 19:01:17 +00002486static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002487 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00002488 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
2489}
2490
2491MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
2492 SDLoc DL,
2493 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002494 const SIInstrInfo *TII =
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002495 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00002496
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002497 // Build the half of the subregister with the constants before building the
2498 // full 128-bit register. If we are building multiple resource descriptors,
2499 // this will allow CSEing of the 2-component register.
2500 const SDValue Ops0[] = {
2501 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
2502 buildSMovImm32(DAG, DL, 0),
2503 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
2504 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
2505 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
2506 };
Matt Arsenault485defe2014-11-05 19:01:17 +00002507
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002508 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
2509 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00002510
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002511 // Combine the constants and the pointer.
2512 const SDValue Ops1[] = {
2513 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
2514 Ptr,
2515 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
2516 SubRegHi,
2517 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
2518 };
Matt Arsenault485defe2014-11-05 19:01:17 +00002519
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002520 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00002521}
2522
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002523/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002524/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
2525/// of the resource descriptor) to create an offset, which is added to
2526/// the resource pointer.
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002527MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
2528 SDLoc DL,
2529 SDValue Ptr,
2530 uint32_t RsrcDword1,
2531 uint64_t RsrcDword2And3) const {
2532 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
2533 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
2534 if (RsrcDword1) {
2535 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002536 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
2537 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002538 }
2539
2540 SDValue DataLo = buildSMovImm32(DAG, DL,
2541 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
2542 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
2543
2544 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002545 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002546 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002547 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002548 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002549 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002550 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002551 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002552 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002553 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002554 };
2555
2556 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2557}
2558
Tom Stellard94593ee2013-06-03 17:40:18 +00002559SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2560 const TargetRegisterClass *RC,
2561 unsigned Reg, EVT VT) const {
2562 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2563
2564 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2565 cast<RegisterSDNode>(VReg)->getReg(), VT);
2566}
Tom Stellardd7e6f132015-04-08 01:09:26 +00002567
2568//===----------------------------------------------------------------------===//
2569// SI Inline Assembly Support
2570//===----------------------------------------------------------------------===//
2571
2572std::pair<unsigned, const TargetRegisterClass *>
2573SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002574 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00002575 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002576
2577 if (Constraint.size() == 1) {
2578 switch (Constraint[0]) {
2579 case 's':
2580 case 'r':
2581 switch (VT.getSizeInBits()) {
2582 default:
2583 return std::make_pair(0U, nullptr);
2584 case 32:
Tom Stellardd7e6f132015-04-08 01:09:26 +00002585 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002586 case 64:
2587 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
2588 case 128:
2589 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
2590 case 256:
2591 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
2592 }
2593
2594 case 'v':
2595 switch (VT.getSizeInBits()) {
2596 default:
2597 return std::make_pair(0U, nullptr);
2598 case 32:
2599 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
2600 case 64:
2601 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
2602 case 96:
2603 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
2604 case 128:
2605 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
2606 case 256:
2607 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
2608 case 512:
2609 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
2610 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00002611 }
2612 }
2613
2614 if (Constraint.size() > 1) {
2615 const TargetRegisterClass *RC = nullptr;
2616 if (Constraint[1] == 'v') {
2617 RC = &AMDGPU::VGPR_32RegClass;
2618 } else if (Constraint[1] == 's') {
2619 RC = &AMDGPU::SGPR_32RegClass;
2620 }
2621
2622 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00002623 uint32_t Idx;
2624 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
2625 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00002626 return std::make_pair(RC->getRegister(Idx), RC);
2627 }
2628 }
2629 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
2630}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002631
2632SITargetLowering::ConstraintType
2633SITargetLowering::getConstraintType(StringRef Constraint) const {
2634 if (Constraint.size() == 1) {
2635 switch (Constraint[0]) {
2636 default: break;
2637 case 's':
2638 case 'v':
2639 return C_RegisterClass;
2640 }
2641 }
2642 return TargetLowering::getConstraintType(Constraint);
2643}