blob: 0c56672f12753230043ce083dc841eea835c036d [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 Stellardd1efda82016-01-20 21:48:24 +0000106 setOperationAction(ISD::SETCC, MVT::i1, Promote);
Tom Stellard83747202013-07-18 21:43:53 +0000107 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
108 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
109
Matt Arsenaulte306a322014-10-21 16:25:08 +0000110 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
Matt Arsenaultd0792852015-12-14 17:25:38 +0000111 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000112
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000114 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
115 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
116
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
120
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
124
Matt Arsenault94812212014-11-14 18:18:16 +0000125 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
127
Tom Stellard94593ee2013-06-03 17:40:18 +0000128 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000129 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
130 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
131 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000132
Tom Stellardafcf12f2013-09-12 02:55:14 +0000133 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000134 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000135
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000136 for (MVT VT : MVT::integer_valuetypes()) {
Matt Arsenaultbd223422015-01-14 01:35:17 +0000137 if (VT == MVT::i64)
138 continue;
139
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000140 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000141 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
142 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000143 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000144
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000145 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000146 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
147 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000148 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000149
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000150 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000151 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
152 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000153 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
154 }
155
156 for (MVT VT : MVT::integer_vector_valuetypes()) {
157 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
158 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
159 }
160
161 for (MVT VT : MVT::fp_valuetypes())
162 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000163
Matt Arsenault61001bb2015-11-25 19:58:34 +0000164 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
165 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
166
Matt Arsenault6f243792013-09-05 19:41:10 +0000167 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000168 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
Matt Arsenaulte1ce3442015-07-31 04:12:04 +0000169 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000170 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000171
Matt Arsenault61001bb2015-11-25 19:58:34 +0000172
173 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
174
175 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
176 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
177
Matt Arsenault470acd82014-04-15 22:28:39 +0000178 setOperationAction(ISD::LOAD, MVT::i1, Custom);
179
Matt Arsenault61001bb2015-11-25 19:58:34 +0000180 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
181 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
182
183 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
184 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
185
186 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
187
Tom Stellardfd155822013-08-26 15:05:36 +0000188 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000189 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000190 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000191
Tom Stellard5f337882014-04-29 23:12:43 +0000192 // These should use UDIVREM, so set them to expand
193 setOperationAction(ISD::UDIV, MVT::i64, Expand);
194 setOperationAction(ISD::UREM, MVT::i64, Expand);
195
Matt Arsenault0d89e842014-07-15 21:44:37 +0000196 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
197 setOperationAction(ISD::SELECT, MVT::i1, Promote);
198
Matt Arsenault61001bb2015-11-25 19:58:34 +0000199 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
200
201
202 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
203
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000204 // We only support LOAD/STORE and vector manipulation ops for vectors
205 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000206 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000207 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
208 switch(Op) {
209 case ISD::LOAD:
210 case ISD::STORE:
211 case ISD::BUILD_VECTOR:
212 case ISD::BITCAST:
213 case ISD::EXTRACT_VECTOR_ELT:
214 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000215 case ISD::INSERT_SUBVECTOR:
216 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000217 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000218 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000219 case ISD::CONCAT_VECTORS:
220 setOperationAction(Op, VT, Custom);
221 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000222 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000223 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000224 break;
225 }
226 }
227 }
228
Matt Arsenault61001bb2015-11-25 19:58:34 +0000229 // Most operations are naturally 32-bit vector operations. We only support
230 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
231 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
232 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
233 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
234
235 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
236 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
237
238 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
239 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
240
241 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
242 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
243 }
244
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000245 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
246 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
247 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000248 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000249 }
250
Marek Olsak7d777282015-03-24 13:40:15 +0000251 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000252 setOperationAction(ISD::FDIV, MVT::f32, Custom);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +0000253 setOperationAction(ISD::FDIV, MVT::f64, Custom);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000254
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000255 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000256 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000257 setTargetDAGCombine(ISD::FMINNUM);
258 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000259 setTargetDAGCombine(ISD::SMIN);
260 setTargetDAGCombine(ISD::SMAX);
261 setTargetDAGCombine(ISD::UMIN);
262 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000263 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000264 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000265 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000266 setTargetDAGCombine(ISD::UINT_TO_FP);
267
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000268 // All memory operations. Some folding on the pointer operand is done to help
269 // matching the constant offsets in the addressing modes.
270 setTargetDAGCombine(ISD::LOAD);
271 setTargetDAGCombine(ISD::STORE);
272 setTargetDAGCombine(ISD::ATOMIC_LOAD);
273 setTargetDAGCombine(ISD::ATOMIC_STORE);
274 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
275 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
276 setTargetDAGCombine(ISD::ATOMIC_SWAP);
277 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
278 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
279 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
280 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
281 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
282 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
283 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
284 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
285 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
286 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
287
Christian Konigeecebd02013-03-26 14:04:02 +0000288 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000289}
290
Tom Stellard0125f2a2013-06-25 02:39:35 +0000291//===----------------------------------------------------------------------===//
292// TargetLowering queries
293//===----------------------------------------------------------------------===//
294
Matt Arsenaulte306a322014-10-21 16:25:08 +0000295bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
296 EVT) const {
297 // SI has some legal vector types, but no legal vector operations. Say no
298 // shuffles are legal in order to prefer scalarizing some vector operations.
299 return false;
300}
301
Tom Stellard70580f82015-07-20 14:28:41 +0000302bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
303 // Flat instructions do not have offsets, and only have the register
304 // address.
305 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
306}
307
Matt Arsenault711b3902015-08-07 20:18:34 +0000308bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
309 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
310 // additionally can do r + r + i with addr64. 32-bit has more addressing
311 // mode options. Depending on the resource constant, it can also do
312 // (i64 r0) + (i32 r1) * (i14 i).
313 //
314 // Private arrays end up using a scratch buffer most of the time, so also
315 // assume those use MUBUF instructions. Scratch loads / stores are currently
316 // implemented as mubuf instructions with offen bit set, so slightly
317 // different than the normal addr64.
318 if (!isUInt<12>(AM.BaseOffs))
319 return false;
320
321 // FIXME: Since we can split immediate into soffset and immediate offset,
322 // would it make sense to allow any immediate?
323
324 switch (AM.Scale) {
325 case 0: // r + i or just i, depending on HasBaseReg.
326 return true;
327 case 1:
328 return true; // We have r + r or r + i.
329 case 2:
330 if (AM.HasBaseReg) {
331 // Reject 2 * r + r.
332 return false;
333 }
334
335 // Allow 2 * r as r + r
336 // Or 2 * r + i is allowed as r + r + i.
337 return true;
338 default: // Don't allow n * r
339 return false;
340 }
341}
342
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000343bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
344 const AddrMode &AM, Type *Ty,
345 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000346 // No global is ever allowed as a base.
347 if (AM.BaseGV)
348 return false;
349
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000350 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000351 case AMDGPUAS::GLOBAL_ADDRESS: {
Tom Stellard70580f82015-07-20 14:28:41 +0000352 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
353 // Assume the we will use FLAT for all global memory accesses
354 // on VI.
355 // FIXME: This assumption is currently wrong. On VI we still use
356 // MUBUF instructions for the r + i addressing mode. As currently
357 // implemented, the MUBUF instructions only work on buffer < 4GB.
358 // It may be possible to support > 4GB buffers with MUBUF instructions,
359 // by setting the stride value in the resource descriptor which would
360 // increase the size limit to (stride * 4GB). However, this is risky,
361 // because it has never been validated.
362 return isLegalFlatAddressingMode(AM);
363 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000364
Matt Arsenault711b3902015-08-07 20:18:34 +0000365 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000366 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000367 case AMDGPUAS::CONSTANT_ADDRESS: {
368 // If the offset isn't a multiple of 4, it probably isn't going to be
369 // correctly aligned.
370 if (AM.BaseOffs % 4 != 0)
371 return isLegalMUBUFAddressingMode(AM);
372
373 // There are no SMRD extloads, so if we have to do a small type access we
374 // will use a MUBUF load.
375 // FIXME?: We also need to do this if unaligned, but we don't know the
376 // alignment here.
377 if (DL.getTypeStoreSize(Ty) < 4)
378 return isLegalMUBUFAddressingMode(AM);
379
380 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
381 // SMRD instructions have an 8-bit, dword offset on SI.
382 if (!isUInt<8>(AM.BaseOffs / 4))
383 return false;
384 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) {
385 // On CI+, this can also be a 32-bit literal constant offset. If it fits
386 // in 8-bits, it can use a smaller encoding.
387 if (!isUInt<32>(AM.BaseOffs / 4))
388 return false;
389 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) {
390 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
391 if (!isUInt<20>(AM.BaseOffs))
392 return false;
393 } else
394 llvm_unreachable("unhandled generation");
395
396 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
397 return true;
398
399 if (AM.Scale == 1 && AM.HasBaseReg)
400 return true;
401
402 return false;
403 }
404
405 case AMDGPUAS::PRIVATE_ADDRESS:
406 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
407 return isLegalMUBUFAddressingMode(AM);
408
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000409 case AMDGPUAS::LOCAL_ADDRESS:
410 case AMDGPUAS::REGION_ADDRESS: {
411 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
412 // field.
413 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
414 // an 8-bit dword offset but we don't know the alignment here.
415 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000416 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000417
418 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
419 return true;
420
421 if (AM.Scale == 1 && AM.HasBaseReg)
422 return true;
423
Matt Arsenault5015a892014-08-15 17:17:07 +0000424 return false;
425 }
Tom Stellard70580f82015-07-20 14:28:41 +0000426 case AMDGPUAS::FLAT_ADDRESS:
427 return isLegalFlatAddressingMode(AM);
428
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000429 default:
430 llvm_unreachable("unhandled address space");
431 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000432}
433
Matt Arsenaulte6986632015-01-14 01:35:22 +0000434bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000435 unsigned AddrSpace,
436 unsigned Align,
437 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000438 if (IsFast)
439 *IsFast = false;
440
Matt Arsenault1018c892014-04-24 17:08:26 +0000441 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
442 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000443 if (!VT.isSimple() || VT == MVT::Other)
444 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000445
Tom Stellardc6b299c2015-02-02 18:02:28 +0000446 // TODO - CI+ supports unaligned memory accesses, but this requires driver
447 // support.
Matt Arsenault1018c892014-04-24 17:08:26 +0000448
Matt Arsenault1018c892014-04-24 17:08:26 +0000449 // XXX - The only mention I see of this in the ISA manual is for LDS direct
450 // reads the "byte address and must be dword aligned". Is it also true for the
451 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000452 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
453 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
454 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
455 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000456 bool AlignedBy4 = (Align % 4 == 0);
457 if (IsFast)
458 *IsFast = AlignedBy4;
459 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000460 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000461
Tom Stellard33e64c62015-02-04 20:49:52 +0000462 // Smaller than dword value must be aligned.
463 // FIXME: This should be allowed on CI+
464 if (VT.bitsLT(MVT::i32))
465 return false;
466
Matt Arsenault1018c892014-04-24 17:08:26 +0000467 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
468 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000469 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000470 if (IsFast)
471 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000472
473 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000474}
475
Matt Arsenault46645fa2014-07-28 17:49:26 +0000476EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
477 unsigned SrcAlign, bool IsMemset,
478 bool ZeroMemset,
479 bool MemcpyStrSrc,
480 MachineFunction &MF) const {
481 // FIXME: Should account for address space here.
482
483 // The default fallback uses the private pointer size as a guess for a type to
484 // use. Make sure we switch these to 64-bit accesses.
485
486 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
487 return MVT::v4i32;
488
489 if (Size >= 8 && DstAlign >= 4)
490 return MVT::v2i32;
491
492 // Use the default.
493 return MVT::Other;
494}
495
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000496static bool isFlatGlobalAddrSpace(unsigned AS) {
497 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
498 AS == AMDGPUAS::FLAT_ADDRESS ||
499 AS == AMDGPUAS::CONSTANT_ADDRESS;
500}
501
502bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
503 unsigned DestAS) const {
504 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
505}
506
Tom Stellarda6f24c62015-12-15 20:55:55 +0000507
508bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
509 const MemSDNode *MemNode = cast<MemSDNode>(N);
510 const Value *Ptr = MemNode->getMemOperand()->getValue();
511
512 // UndefValue means this is a load of a kernel input. These are uniform.
513 // Sometimes LDS instructions have constant pointers
514 if (isa<UndefValue>(Ptr) || isa<Argument>(Ptr) || isa<Constant>(Ptr) ||
515 isa<GlobalValue>(Ptr))
516 return true;
517
518 const Instruction *I = dyn_cast_or_null<Instruction>(Ptr);
519 return I && I->getMetadata("amdgpu.uniform");
520}
521
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000522TargetLoweringBase::LegalizeTypeAction
523SITargetLowering::getPreferredVectorAction(EVT VT) const {
524 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
525 return TypeSplitVector;
526
527 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000528}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000529
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000530bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
531 Type *Ty) const {
Eric Christopher7792e322015-01-30 23:24:40 +0000532 const SIInstrInfo *TII =
533 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000534 return TII->isInlineConstant(Imm);
535}
536
Tom Stellard2e045bb2016-01-20 00:13:22 +0000537bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
538
539 // SimplifySetCC uses this function to determine whether or not it should
540 // create setcc with i1 operands. We don't have instructions for i1 setcc.
541 if (VT == MVT::i1 && Op == ISD::SETCC)
542 return false;
543
544 return TargetLowering::isTypeDesirableForOp(Op, VT);
545}
546
Tom Stellardaf775432013-10-23 00:44:32 +0000547SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000548 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000549 unsigned Offset, bool Signed) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000550 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000551 MachineFunction &MF = DAG.getMachineFunction();
552 const SIRegisterInfo *TRI =
553 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
Matt Arsenaultac234b62015-11-30 21:15:57 +0000554 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000555
Matt Arsenault86033ca2014-07-28 17:31:39 +0000556 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
557
558 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000559 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000560 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000561 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
562 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
563 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
564 DAG.getConstant(Offset, SL, PtrVT));
Mehdi Amini44ede332015-07-09 02:09:04 +0000565 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000566 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
567
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000568 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000569
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000570 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000571 if (MemVT.isFloatingPoint())
572 ExtTy = ISD::EXTLOAD;
573
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000574 return DAG.getLoad(ISD::UNINDEXED, ExtTy,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000575 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
576 false, // isVolatile
577 true, // isNonTemporal
578 true, // isInvariant
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000579 Align); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000580}
581
Christian Konig2c8f6d52013-03-07 09:03:52 +0000582SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000583 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
584 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
585 SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardec2e43c2014-09-22 15:35:29 +0000586 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000587 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000588
589 MachineFunction &MF = DAG.getMachineFunction();
590 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000591 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000592 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000593
Matt Arsenaultd48da142015-11-02 23:23:02 +0000594 if (Subtarget->isAmdHsaOS() && Info->getShaderType() != ShaderType::COMPUTE) {
595 const Function *Fn = MF.getFunction();
596 DiagnosticInfoUnsupported NoGraphicsHSA(*Fn, "non-compute shaders with HSA");
597 DAG.getContext()->diagnose(NoGraphicsHSA);
598 return SDValue();
599 }
600
Tom Stellard0fbf8992015-10-06 21:16:34 +0000601 // FIXME: We currently assume all calling conventions are kernels.
Christian Konig2c8f6d52013-03-07 09:03:52 +0000602
603 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000604 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000605
606 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000607 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000608
609 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000610 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000611 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000612
Marek Olsakfccabaf2016-01-13 11:45:36 +0000613 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000614 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000615 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000616 ++PSInputNum;
617 continue;
618 }
619
Marek Olsakfccabaf2016-01-13 11:45:36 +0000620 Info->markPSInputAllocated(PSInputNum);
621 if (Arg.Used)
622 Info->PSInputEna |= 1 << PSInputNum;
623
624 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000625 }
626
627 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000628 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000629 ISD::InputArg NewArg = Arg;
630 NewArg.Flags.setSplit();
631 NewArg.VT = Arg.VT.getVectorElementType();
632
633 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
634 // three or five element vertex only needs three or five registers,
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000635 // NOT four or eight.
Andrew Trick05938a52015-02-16 18:10:47 +0000636 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000637 unsigned NumElements = ParamType->getVectorNumElements();
638
639 for (unsigned j = 0; j != NumElements; ++j) {
640 Splits.push_back(NewArg);
641 NewArg.PartOffset += NewArg.VT.getStoreSize();
642 }
643
Matt Arsenault762af962014-07-13 03:06:39 +0000644 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000645 Splits.push_back(Arg);
646 }
647 }
648
649 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000650 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
651 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000652
Christian Konig99ee0f42013-03-07 09:04:14 +0000653 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000654 //
655 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
656 // PSInputAddr, the user wants to enable some bits after the compilation
657 // based on run-time states. Since we can't know what the final PSInputEna
658 // will look like, so we shouldn't do anything here and the user should take
659 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000660 //
661 // Otherwise, the following restrictions apply:
662 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
663 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
664 // enabled too.
Matt Arsenault762af962014-07-13 03:06:39 +0000665 if (Info->getShaderType() == ShaderType::PIXEL &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000666 ((Info->getPSInputAddr() & 0x7F) == 0 ||
667 ((Info->getPSInputAddr() & 0xF) == 0 &&
668 Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000669 CCInfo.AllocateReg(AMDGPU::VGPR0);
670 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000671 Info->markPSInputAllocated(0);
672 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000673 }
674
Matt Arsenault762af962014-07-13 03:06:39 +0000675 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000676 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
677 Splits);
678 }
679
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000680 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
681 if (Info->hasPrivateSegmentBuffer()) {
682 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
683 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
684 CCInfo.AllocateReg(PrivateSegmentBufferReg);
685 }
686
687 if (Info->hasDispatchPtr()) {
688 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
689 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
690 CCInfo.AllocateReg(DispatchPtrReg);
691 }
692
693 if (Info->hasKernargSegmentPtr()) {
694 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
695 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
696 CCInfo.AllocateReg(InputPtrReg);
697 }
698
Christian Konig2c8f6d52013-03-07 09:03:52 +0000699 AnalyzeFormalArguments(CCInfo, Splits);
700
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000701 SmallVector<SDValue, 16> Chains;
702
Christian Konig2c8f6d52013-03-07 09:03:52 +0000703 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
704
Christian Konigb7be72d2013-05-17 09:46:48 +0000705 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000706 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000707 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000708 continue;
709 }
710
Christian Konig2c8f6d52013-03-07 09:03:52 +0000711 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000712 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000713
714 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000715 VT = Ins[i].VT;
716 EVT MemVT = Splits[i].VT;
Tom Stellardb5798b02015-06-26 21:15:03 +0000717 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
718 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000719 // The first 36 bytes of the input buffer contains information about
720 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000721 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000722 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000723 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000724
Craig Toppere3dcce92015-08-01 22:20:21 +0000725 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000726 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000727 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
728 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
729 // On SI local pointers are just offsets into LDS, so they are always
730 // less than 16-bits. On CI and newer they could potentially be
731 // real pointers, so we can't guarantee their size.
732 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
733 DAG.getValueType(MVT::i16));
734 }
735
Tom Stellarded882c22013-06-03 17:40:11 +0000736 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000737 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000738 continue;
739 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000740 assert(VA.isRegLoc() && "Parameter must be in a register!");
741
742 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000743
744 if (VT == MVT::i64) {
745 // For now assume it is a pointer
746 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
747 &AMDGPU::SReg_64RegClass);
748 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000749 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
750 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000751 continue;
752 }
753
754 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
755
756 Reg = MF.addLiveIn(Reg, RC);
757 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
758
Christian Konig2c8f6d52013-03-07 09:03:52 +0000759 if (Arg.VT.isVector()) {
760
761 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000762 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000763 unsigned NumElements = ParamType->getVectorNumElements();
764
765 SmallVector<SDValue, 4> Regs;
766 Regs.push_back(Val);
767 for (unsigned j = 1; j != NumElements; ++j) {
768 Reg = ArgLocs[ArgIdx++].getLocReg();
769 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000770
771 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
772 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000773 }
774
775 // Fill up the missing vector elements
776 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000777 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000778
Craig Topper48d114b2014-04-26 18:35:24 +0000779 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000780 continue;
781 }
782
783 InVals.push_back(Val);
784 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000785
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000786 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
787 // these from the dispatch pointer.
788
789 // Start adding system SGPRs.
790 if (Info->hasWorkGroupIDX()) {
791 unsigned Reg = Info->addWorkGroupIDX();
792 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
793 CCInfo.AllocateReg(Reg);
794 } else
795 llvm_unreachable("work group id x is always enabled");
796
797 if (Info->hasWorkGroupIDY()) {
798 unsigned Reg = Info->addWorkGroupIDY();
799 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
800 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000801 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000802
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000803 if (Info->hasWorkGroupIDZ()) {
804 unsigned Reg = Info->addWorkGroupIDZ();
805 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
806 CCInfo.AllocateReg(Reg);
807 }
808
809 if (Info->hasWorkGroupInfo()) {
810 unsigned Reg = Info->addWorkGroupInfo();
811 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
812 CCInfo.AllocateReg(Reg);
813 }
814
815 if (Info->hasPrivateSegmentWaveByteOffset()) {
816 // Scratch wave offset passed in system SGPR.
817 unsigned PrivateSegmentWaveByteOffsetReg
818 = Info->addPrivateSegmentWaveByteOffset();
819
820 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
821 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
822 }
823
824 // Now that we've figured out where the scratch register inputs are, see if
825 // should reserve the arguments and use them directly.
826
827 bool HasStackObjects = MF.getFrameInfo()->hasStackObjects();
828
829 if (ST.isAmdHsaOS()) {
830 // TODO: Assume we will spill without optimizations.
831 if (HasStackObjects) {
832 // If we have stack objects, we unquestionably need the private buffer
833 // resource. For the HSA ABI, this will be the first 4 user SGPR
834 // inputs. We can reserve those and use them directly.
835
836 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
837 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
838 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
839
840 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
841 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
842 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
843 } else {
844 unsigned ReservedBufferReg
845 = TRI->reservedPrivateSegmentBufferReg(MF);
846 unsigned ReservedOffsetReg
847 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
848
849 // We tentatively reserve the last registers (skipping the last two
850 // which may contain VCC). After register allocation, we'll replace
851 // these with the ones immediately after those which were really
852 // allocated. In the prologue copies will be inserted from the argument
853 // to these reserved registers.
854 Info->setScratchRSrcReg(ReservedBufferReg);
855 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
856 }
857 } else {
858 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
859
860 // Without HSA, relocations are used for the scratch pointer and the
861 // buffer resource setup is always inserted in the prologue. Scratch wave
862 // offset is still in an input SGPR.
863 Info->setScratchRSrcReg(ReservedBufferReg);
864
865 if (HasStackObjects) {
866 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
867 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
868 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
869 } else {
870 unsigned ReservedOffsetReg
871 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
872 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
873 }
874 }
875
876 if (Info->hasWorkItemIDX()) {
877 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
878 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
879 CCInfo.AllocateReg(Reg);
880 } else
881 llvm_unreachable("workitem id x should always be enabled");
882
883 if (Info->hasWorkItemIDY()) {
884 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
885 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
886 CCInfo.AllocateReg(Reg);
887 }
888
889 if (Info->hasWorkItemIDZ()) {
890 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
891 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
892 CCInfo.AllocateReg(Reg);
893 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000894
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000895 if (Chains.empty())
896 return Chain;
897
898 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000899}
900
Marek Olsak8a0f3352016-01-13 17:23:04 +0000901SDValue SITargetLowering::LowerReturn(SDValue Chain,
902 CallingConv::ID CallConv,
903 bool isVarArg,
904 const SmallVectorImpl<ISD::OutputArg> &Outs,
905 const SmallVectorImpl<SDValue> &OutVals,
906 SDLoc DL, SelectionDAG &DAG) const {
907 MachineFunction &MF = DAG.getMachineFunction();
908 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
909
910 if (Info->getShaderType() == ShaderType::COMPUTE)
911 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
912 OutVals, DL, DAG);
913
Marek Olsak8e9cc632016-01-13 17:23:09 +0000914 Info->setIfReturnsVoid(Outs.size() == 0);
915
Marek Olsak8a0f3352016-01-13 17:23:04 +0000916 SmallVector<ISD::OutputArg, 48> Splits;
917 SmallVector<SDValue, 48> SplitVals;
918
919 // Split vectors into their elements.
920 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
921 const ISD::OutputArg &Out = Outs[i];
922
923 if (Out.VT.isVector()) {
924 MVT VT = Out.VT.getVectorElementType();
925 ISD::OutputArg NewOut = Out;
926 NewOut.Flags.setSplit();
927 NewOut.VT = VT;
928
929 // We want the original number of vector elements here, e.g.
930 // three or five, not four or eight.
931 unsigned NumElements = Out.ArgVT.getVectorNumElements();
932
933 for (unsigned j = 0; j != NumElements; ++j) {
934 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
935 DAG.getConstant(j, DL, MVT::i32));
936 SplitVals.push_back(Elem);
937 Splits.push_back(NewOut);
938 NewOut.PartOffset += NewOut.VT.getStoreSize();
939 }
940 } else {
941 SplitVals.push_back(OutVals[i]);
942 Splits.push_back(Out);
943 }
944 }
945
946 // CCValAssign - represent the assignment of the return value to a location.
947 SmallVector<CCValAssign, 48> RVLocs;
948
949 // CCState - Info about the registers and stack slots.
950 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
951 *DAG.getContext());
952
953 // Analyze outgoing return values.
954 AnalyzeReturn(CCInfo, Splits);
955
956 SDValue Flag;
957 SmallVector<SDValue, 48> RetOps;
958 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
959
960 // Copy the result values into the output registers.
961 for (unsigned i = 0, realRVLocIdx = 0;
962 i != RVLocs.size();
963 ++i, ++realRVLocIdx) {
964 CCValAssign &VA = RVLocs[i];
965 assert(VA.isRegLoc() && "Can only return in registers!");
966
967 SDValue Arg = SplitVals[realRVLocIdx];
968
969 // Copied from other backends.
970 switch (VA.getLocInfo()) {
971 default: llvm_unreachable("Unknown loc info!");
972 case CCValAssign::Full:
973 break;
974 case CCValAssign::BCvt:
975 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
976 break;
977 }
978
979 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
980 Flag = Chain.getValue(1);
981 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
982 }
983
984 // Update chain and glue.
985 RetOps[0] = Chain;
986 if (Flag.getNode())
987 RetOps.push_back(Flag);
988
989 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, RetOps);
990}
991
Tom Stellard75aadc22012-12-11 21:25:42 +0000992MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
993 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000994
Tom Stellard75aadc22012-12-11 21:25:42 +0000995 switch (MI->getOpcode()) {
996 default:
997 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Matt Arsenault20711b72015-02-20 22:10:45 +0000998 case AMDGPU::BRANCH:
999 return BB;
Tom Stellard75aadc22012-12-11 21:25:42 +00001000 }
1001 return BB;
1002}
1003
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001004bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1005 // This currently forces unfolding various combinations of fsub into fma with
1006 // free fneg'd operands. As long as we have fast FMA (controlled by
1007 // isFMAFasterThanFMulAndFAdd), we should perform these.
1008
1009 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1010 // most of these combines appear to be cycle neutral but save on instruction
1011 // count / code size.
1012 return true;
1013}
1014
Mehdi Amini44ede332015-07-09 02:09:04 +00001015EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1016 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001017 if (!VT.isVector()) {
1018 return MVT::i1;
1019 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001020 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001021}
1022
Mehdi Aminieaabc512015-07-09 15:12:23 +00001023MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001024 return MVT::i32;
1025}
1026
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001027// Answering this is somewhat tricky and depends on the specific device which
1028// have different rates for fma or all f64 operations.
1029//
1030// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1031// regardless of which device (although the number of cycles differs between
1032// devices), so it is always profitable for f64.
1033//
1034// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1035// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1036// which we can always do even without fused FP ops since it returns the same
1037// result as the separate operations and since it is always full
1038// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1039// however does not support denormals, so we do report fma as faster if we have
1040// a fast fma device and require denormals.
1041//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001042bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1043 VT = VT.getScalarType();
1044
1045 if (!VT.isSimple())
1046 return false;
1047
1048 switch (VT.getSimpleVT().SimpleTy) {
1049 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001050 // This is as fast on some subtargets. However, we always have full rate f32
1051 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001052 // which we should prefer over fma. We can't use this if we want to support
1053 // denormals, so only report this in these cases.
1054 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001055 case MVT::f64:
1056 return true;
1057 default:
1058 break;
1059 }
1060
1061 return false;
1062}
1063
Tom Stellard75aadc22012-12-11 21:25:42 +00001064//===----------------------------------------------------------------------===//
1065// Custom DAG Lowering Operations
1066//===----------------------------------------------------------------------===//
1067
1068SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1069 switch (Op.getOpcode()) {
1070 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001071 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001072 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001073 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001074 SDValue Result = LowerLOAD(Op, DAG);
1075 assert((!Result.getNode() ||
1076 Result.getNode()->getNumValues() == 2) &&
1077 "Load should return a value and a chain");
1078 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001079 }
Tom Stellardaf775432013-10-23 00:44:32 +00001080
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001081 case ISD::FSIN:
1082 case ISD::FCOS:
1083 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001084 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001085 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001086 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001087 case ISD::GlobalAddress: {
1088 MachineFunction &MF = DAG.getMachineFunction();
1089 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1090 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001091 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001092 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
1093 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001094 }
1095 return SDValue();
1096}
1097
Tom Stellardf8794352012-12-19 22:10:31 +00001098/// \brief Helper function for LowerBRCOND
1099static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001100
Tom Stellardf8794352012-12-19 22:10:31 +00001101 SDNode *Parent = Value.getNode();
1102 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1103 I != E; ++I) {
1104
1105 if (I.getUse().get() != Value)
1106 continue;
1107
1108 if (I->getOpcode() == Opcode)
1109 return *I;
1110 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001111 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001112}
1113
Tom Stellardb02094e2014-07-21 15:45:01 +00001114SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
1115
Tom Stellardc98ee202015-07-16 19:40:07 +00001116 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +00001117 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
1118 unsigned FrameIndex = FINode->getIndex();
1119
Tom Stellardc98ee202015-07-16 19:40:07 +00001120 // A FrameIndex node represents a 32-bit offset into scratch memory. If
1121 // the high bit of a frame index offset were to be set, this would mean
1122 // that it represented an offset of ~2GB * 64 = ~128GB from the start of the
1123 // scratch buffer, with 64 being the number of threads per wave.
1124 //
1125 // If we know the machine uses less than 128GB of scratch, then we can
1126 // amrk the high bit of the FrameIndex node as known zero,
1127 // which is important, because it means in most situations we can
1128 // prove that values derived from FrameIndex nodes are non-negative.
1129 // This enables us to take advantage of more addressing modes when
1130 // accessing scratch buffers, since for scratch reads/writes, the register
1131 // offset must always be positive.
1132
1133 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
1134 if (Subtarget->enableHugeScratchBuffer())
1135 return TFI;
1136
1137 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
1138 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), 31)));
Tom Stellardb02094e2014-07-21 15:45:01 +00001139}
1140
Tom Stellardf8794352012-12-19 22:10:31 +00001141/// This transforms the control flow intrinsics to get the branch destination as
1142/// last parameter, also switches branch target with BR if the need arise
1143SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1144 SelectionDAG &DAG) const {
1145
Andrew Trickef9de2a2013-05-25 02:42:55 +00001146 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001147
1148 SDNode *Intr = BRCOND.getOperand(1).getNode();
1149 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001150 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001151
1152 if (Intr->getOpcode() == ISD::SETCC) {
1153 // As long as we negate the condition everything is fine
1154 SDNode *SetCC = Intr;
1155 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +00001156 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1157 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +00001158 Intr = SetCC->getOperand(0).getNode();
1159
1160 } else {
1161 // Get the target from BR if we don't negate the condition
1162 BR = findUser(BRCOND, ISD::BR);
1163 Target = BR->getOperand(1);
1164 }
1165
1166 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
1167
1168 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001169 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001170
1171 // operands of the new intrinsic call
1172 SmallVector<SDValue, 4> Ops;
1173 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001174 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001175 Ops.push_back(Target);
1176
1177 // build the new intrinsic call
1178 SDNode *Result = DAG.getNode(
1179 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001180 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001181
1182 if (BR) {
1183 // Give the branch instruction our target
1184 SDValue Ops[] = {
1185 BR->getOperand(0),
1186 BRCOND.getOperand(2)
1187 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001188 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1189 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1190 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001191 }
1192
1193 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1194
1195 // Copy the intrinsic results to registers
1196 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1197 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1198 if (!CopyToReg)
1199 continue;
1200
1201 Chain = DAG.getCopyToReg(
1202 Chain, DL,
1203 CopyToReg->getOperand(1),
1204 SDValue(Result, i - 1),
1205 SDValue());
1206
1207 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1208 }
1209
1210 // Remove the old intrinsic from the chain
1211 DAG.ReplaceAllUsesOfValueWith(
1212 SDValue(Intr, Intr->getNumValues() - 1),
1213 Intr->getOperand(0));
1214
1215 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001216}
1217
Tom Stellard067c8152014-07-21 14:01:14 +00001218SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1219 SDValue Op,
1220 SelectionDAG &DAG) const {
1221 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1222
1223 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
1224 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1225
1226 SDLoc DL(GSD);
1227 const GlobalValue *GV = GSD->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001228 MVT PtrVT = getPointerTy(DAG.getDataLayout(), GSD->getAddressSpace());
Tom Stellard067c8152014-07-21 14:01:14 +00001229
Tom Stellard067c8152014-07-21 14:01:14 +00001230 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
Tom Stellardc93fc112015-12-10 02:13:01 +00001231 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT, GA);
Tom Stellard067c8152014-07-21 14:01:14 +00001232}
1233
Tom Stellardfc92e772015-05-12 14:18:14 +00001234SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL,
1235 SDValue V) const {
1236 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1237 // so we will end up with redundant moves to m0.
1238 //
1239 // We can't use S_MOV_B32, because there is no way to specify m0 as the
1240 // destination register.
1241 //
1242 // We have to use them both. Machine cse will combine all the S_MOV_B32
1243 // instructions and the register coalescer eliminate the extra copies.
1244 SDNode *M0 = DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, V.getValueType(), V);
1245 return DAG.getCopyToReg(Chain, DL, DAG.getRegister(AMDGPU::M0, MVT::i32),
1246 SDValue(M0, 0), SDValue()); // Glue
1247 // A Null SDValue creates
1248 // a glue result.
1249}
1250
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001251SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1252 SDValue Op,
1253 MVT VT,
1254 unsigned Offset) const {
1255 SDLoc SL(Op);
1256 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1257 DAG.getEntryNode(), Offset, false);
1258 // The local size values will have the hi 16-bits as zero.
1259 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1260 DAG.getValueType(VT));
1261}
1262
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001263SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1264 SelectionDAG &DAG) const {
1265 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001266 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardec2e43c2014-09-22 15:35:29 +00001267 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +00001268 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001269
1270 EVT VT = Op.getValueType();
1271 SDLoc DL(Op);
1272 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1273
Sanjay Patela2607012015-09-16 16:31:21 +00001274 // TODO: Should this propagate fast-math-flags?
1275
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001276 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001277 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault800fecf2016-01-11 21:18:33 +00001278 if (!Subtarget->isAmdHsaOS()) {
1279 DiagnosticInfoUnsupported BadIntrin(*MF.getFunction(),
1280 "hsa intrinsic without hsa target");
1281 DAG.getContext()->diagnose(BadIntrin);
1282 return DAG.getUNDEF(VT);
1283 }
1284
Tom Stellard48f29f22015-11-26 00:43:29 +00001285 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
1286 TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_PTR), VT);
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001287 case Intrinsic::amdgcn_rcp:
1288 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
1289 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001290 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001291 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
1292 case Intrinsic::amdgcn_rsq_clamped:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001293 case AMDGPUIntrinsic::AMDGPU_rsq_clamped: { // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001294 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
1295 return DAG.getNode(AMDGPUISD::RSQ_CLAMPED, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00001296
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001297 Type *Type = VT.getTypeForEVT(*DAG.getContext());
1298 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
1299 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
1300
1301 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
1302 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
1303 DAG.getConstantFP(Max, DL, VT));
1304 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
1305 DAG.getConstantFP(Min, DL, VT));
1306 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001307 case Intrinsic::r600_read_ngroups_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001308 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1309 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001310 case Intrinsic::r600_read_ngroups_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001311 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1312 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001313 case Intrinsic::r600_read_ngroups_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001314 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1315 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001316 case Intrinsic::r600_read_global_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001317 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1318 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001319 case Intrinsic::r600_read_global_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001320 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1321 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001322 case Intrinsic::r600_read_global_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001323 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1324 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001325 case Intrinsic::r600_read_local_size_x:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001326 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1327 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001328 case Intrinsic::r600_read_local_size_y:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001329 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1330 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001331 case Intrinsic::r600_read_local_size_z:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001332 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1333 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenaultbef34e22016-01-22 21:30:34 +00001334 case Intrinsic::amdgcn_read_workdim:
1335 case AMDGPUIntrinsic::AMDGPU_read_workdim: // Legacy name.
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001336 // Really only 2 bits.
1337 return lowerImplicitZextParam(DAG, Op, MVT::i8,
1338 getImplicitParameterOffset(MFI, GRID_DIM));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001339 case Intrinsic::r600_read_tgid_x:
1340 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001341 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001342 case Intrinsic::r600_read_tgid_y:
1343 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001344 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001345 case Intrinsic::r600_read_tgid_z:
1346 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001347 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001348 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001349 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001350 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001351 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001352 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001353 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001354 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001355 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001356 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001357 case AMDGPUIntrinsic::SI_load_const: {
1358 SDValue Ops[] = {
1359 Op.getOperand(1),
1360 Op.getOperand(2)
1361 };
1362
1363 MachineMemOperand *MMO = MF.getMachineMemOperand(
1364 MachinePointerInfo(),
1365 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
1366 VT.getStoreSize(), 4);
1367 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
1368 Op->getVTList(), Ops, VT, MMO);
1369 }
1370 case AMDGPUIntrinsic::SI_sample:
1371 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
1372 case AMDGPUIntrinsic::SI_sampleb:
1373 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
1374 case AMDGPUIntrinsic::SI_sampled:
1375 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
1376 case AMDGPUIntrinsic::SI_samplel:
1377 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
1378 case AMDGPUIntrinsic::SI_vs_load_input:
1379 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1380 Op.getOperand(1),
1381 Op.getOperand(2),
1382 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00001383
Tom Stellard2a9d9472015-05-12 15:00:46 +00001384 case AMDGPUIntrinsic::SI_fs_constant: {
1385 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1386 SDValue Glue = M0.getValue(1);
1387 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
1388 DAG.getConstant(2, DL, MVT::i32), // P0
1389 Op.getOperand(1), Op.getOperand(2), Glue);
1390 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00001391 case AMDGPUIntrinsic::SI_packf16:
1392 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
1393 return DAG.getUNDEF(MVT::i32);
1394 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00001395 case AMDGPUIntrinsic::SI_fs_interp: {
1396 SDValue IJ = Op.getOperand(4);
1397 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1398 DAG.getConstant(0, DL, MVT::i32));
1399 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1400 DAG.getConstant(1, DL, MVT::i32));
1401 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1402 SDValue Glue = M0.getValue(1);
1403 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
1404 DAG.getVTList(MVT::f32, MVT::Glue),
1405 I, Op.getOperand(1), Op.getOperand(2), Glue);
1406 Glue = SDValue(P1.getNode(), 1);
1407 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
1408 Op.getOperand(1), Op.getOperand(2), Glue);
1409 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00001410 case Intrinsic::amdgcn_interp_p1: {
1411 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
1412 SDValue Glue = M0.getValue(1);
1413 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
1414 Op.getOperand(2), Op.getOperand(3), Glue);
1415 }
1416 case Intrinsic::amdgcn_interp_p2: {
1417 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
1418 SDValue Glue = SDValue(M0.getNode(), 1);
1419 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
1420 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
1421 Glue);
1422 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001423 case Intrinsic::amdgcn_ldexp:
1424 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
1425 Op.getOperand(1), Op.getOperand(2));
1426 case Intrinsic::amdgcn_class:
1427 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
1428 Op.getOperand(1), Op.getOperand(2));
1429 case Intrinsic::amdgcn_div_fmas:
1430 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
1431 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
1432 Op.getOperand(4));
1433
1434 case Intrinsic::amdgcn_div_fixup:
1435 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
1436 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
1437
1438 case Intrinsic::amdgcn_trig_preop:
1439 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
1440 Op.getOperand(1), Op.getOperand(2));
1441 case Intrinsic::amdgcn_div_scale: {
1442 // 3rd parameter required to be a constant.
1443 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1444 if (!Param)
1445 return DAG.getUNDEF(VT);
1446
1447 // Translate to the operands expected by the machine instruction. The
1448 // first parameter must be the same as the first instruction.
1449 SDValue Numerator = Op.getOperand(1);
1450 SDValue Denominator = Op.getOperand(2);
1451
1452 // Note this order is opposite of the machine instruction's operations,
1453 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
1454 // intrinsic has the numerator as the first operand to match a normal
1455 // division operation.
1456
1457 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
1458
1459 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
1460 Denominator, Numerator);
1461 }
1462 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte0:
1463 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Op.getOperand(1));
1464 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte1:
1465 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE1, DL, VT, Op.getOperand(1));
1466 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte2:
1467 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE2, DL, VT, Op.getOperand(1));
1468 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte3:
1469 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE3, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001470 default:
1471 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1472 }
1473}
1474
1475SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1476 SelectionDAG &DAG) const {
1477 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00001478 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001479 SDValue Chain = Op.getOperand(0);
1480 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1481
1482 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00001483 case AMDGPUIntrinsic::SI_sendmsg: {
1484 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1485 SDValue Glue = Chain.getValue(1);
1486 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1487 Op.getOperand(2), Glue);
1488 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001489 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001490 SDValue Ops[] = {
1491 Chain,
1492 Op.getOperand(2),
1493 Op.getOperand(3),
1494 Op.getOperand(4),
1495 Op.getOperand(5),
1496 Op.getOperand(6),
1497 Op.getOperand(7),
1498 Op.getOperand(8),
1499 Op.getOperand(9),
1500 Op.getOperand(10),
1501 Op.getOperand(11),
1502 Op.getOperand(12),
1503 Op.getOperand(13),
1504 Op.getOperand(14)
1505 };
1506
1507 EVT VT = Op.getOperand(3).getValueType();
1508
1509 MachineMemOperand *MMO = MF.getMachineMemOperand(
1510 MachinePointerInfo(),
1511 MachineMemOperand::MOStore,
1512 VT.getStoreSize(), 4);
1513 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1514 Op->getVTList(), Ops, VT, MMO);
1515 }
1516 default:
1517 return SDValue();
1518 }
1519}
1520
Tom Stellard81d871d2013-11-13 23:36:50 +00001521SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1522 SDLoc DL(Op);
1523 LoadSDNode *Load = cast<LoadSDNode>(Op);
1524
Tom Stellarde812f2f2014-07-21 15:45:06 +00001525 if (Op.getValueType().isVector()) {
1526 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1527 "Custom lowering for non-i32 vectors hasn't been implemented.");
1528 unsigned NumElements = Op.getValueType().getVectorNumElements();
1529 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001530
Tom Stellarde812f2f2014-07-21 15:45:06 +00001531 switch (Load->getAddressSpace()) {
1532 default: break;
Tom Stellarda6f24c62015-12-15 20:55:55 +00001533 case AMDGPUAS::CONSTANT_ADDRESS:
1534 if (isMemOpUniform(Load))
1535 break;
1536 // Non-uniform loads will be selected to MUBUF instructions, so they
1537 // have the same legalization requires ments as global and private
1538 // loads.
1539 //
1540 // Fall-through
Tom Stellarde812f2f2014-07-21 15:45:06 +00001541 case AMDGPUAS::GLOBAL_ADDRESS:
1542 case AMDGPUAS::PRIVATE_ADDRESS:
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001543 if (NumElements >= 8)
1544 return SplitVectorLoad(Op, DAG);
1545
Tom Stellarde812f2f2014-07-21 15:45:06 +00001546 // v4 loads are supported for private and global memory.
1547 if (NumElements <= 4)
1548 break;
1549 // fall-through
1550 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenaultff05da82015-11-24 12:18:54 +00001551 // If properly aligned, if we split we might be able to use ds_read_b64.
1552 return SplitVectorLoad(Op, DAG);
Tom Stellarde812f2f2014-07-21 15:45:06 +00001553 }
Tom Stellarde9373602014-01-22 19:24:14 +00001554 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001555
Tom Stellarde812f2f2014-07-21 15:45:06 +00001556 return AMDGPUTargetLowering::LowerLOAD(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001557}
1558
Tom Stellard9fa17912013-08-14 23:24:45 +00001559SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
1560 const SDValue &Op,
1561 SelectionDAG &DAG) const {
1562 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
1563 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +00001564 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +00001565 Op.getOperand(4));
1566}
1567
Tom Stellard0ec134f2014-02-04 17:18:40 +00001568SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1569 if (Op.getValueType() != MVT::i64)
1570 return SDValue();
1571
1572 SDLoc DL(Op);
1573 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001574
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001575 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1576 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001577
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001578 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1579 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1580
1581 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1582 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001583
1584 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1585
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001586 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1587 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001588
1589 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1590
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001591 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1592 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001593}
1594
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001595// Catch division cases where we can use shortcuts with rcp and rsq
1596// instructions.
1597SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001598 SDLoc SL(Op);
1599 SDValue LHS = Op.getOperand(0);
1600 SDValue RHS = Op.getOperand(1);
1601 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001602 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001603
1604 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001605 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1606 CLHS->isExactlyValue(1.0)) {
1607 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1608 // the CI documentation has a worst case error of 1 ulp.
1609 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1610 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001611
1612 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001613 //
1614 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1615 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001616 if (RHS.getOpcode() == ISD::FSQRT)
1617 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1618
1619 // 1.0 / x -> rcp(x)
1620 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1621 }
1622 }
1623
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001624 if (Unsafe) {
1625 // Turn into multiply by the reciprocal.
1626 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00001627 SDNodeFlags Flags;
1628 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001629 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00001630 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001631 }
1632
1633 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001634}
1635
1636SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001637 SDValue FastLowered = LowerFastFDIV(Op, DAG);
1638 if (FastLowered.getNode())
1639 return FastLowered;
1640
1641 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1642 // selection error for now rather than do something incorrect.
1643 if (Subtarget->hasFP32Denormals())
1644 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001645
1646 SDLoc SL(Op);
1647 SDValue LHS = Op.getOperand(0);
1648 SDValue RHS = Op.getOperand(1);
1649
1650 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1651
1652 const APFloat K0Val(BitsToFloat(0x6f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001653 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001654
1655 const APFloat K1Val(BitsToFloat(0x2f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001656 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001657
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001658 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001659
Mehdi Amini44ede332015-07-09 02:09:04 +00001660 EVT SetCCVT =
1661 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001662
1663 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1664
1665 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1666
Sanjay Patela2607012015-09-16 16:31:21 +00001667 // TODO: Should this propagate fast-math-flags?
1668
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001669 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1670
1671 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1672
1673 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1674
1675 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1676}
1677
1678SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001679 if (DAG.getTarget().Options.UnsafeFPMath)
1680 return LowerFastFDIV(Op, DAG);
1681
1682 SDLoc SL(Op);
1683 SDValue X = Op.getOperand(0);
1684 SDValue Y = Op.getOperand(1);
1685
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001686 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001687
1688 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
1689
1690 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
1691
1692 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
1693
1694 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
1695
1696 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
1697
1698 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
1699
1700 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
1701
1702 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
1703
1704 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
1705 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
1706
1707 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
1708 NegDivScale0, Mul, DivScale1);
1709
1710 SDValue Scale;
1711
1712 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1713 // Workaround a hardware bug on SI where the condition output from div_scale
1714 // is not usable.
1715
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001716 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001717
1718 // Figure out if the scale to use for div_fmas.
1719 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1720 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
1721 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
1722 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
1723
1724 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
1725 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
1726
1727 SDValue Scale0Hi
1728 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
1729 SDValue Scale1Hi
1730 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
1731
1732 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
1733 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
1734 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
1735 } else {
1736 Scale = DivScale1.getValue(1);
1737 }
1738
1739 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
1740 Fma4, Fma3, Mul, Scale);
1741
1742 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001743}
1744
1745SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1746 EVT VT = Op.getValueType();
1747
1748 if (VT == MVT::f32)
1749 return LowerFDIV32(Op, DAG);
1750
1751 if (VT == MVT::f64)
1752 return LowerFDIV64(Op, DAG);
1753
1754 llvm_unreachable("Unexpected type for fdiv");
1755}
1756
Tom Stellard81d871d2013-11-13 23:36:50 +00001757SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1758 SDLoc DL(Op);
1759 StoreSDNode *Store = cast<StoreSDNode>(Op);
1760 EVT VT = Store->getMemoryVT();
1761
Tom Stellard9b3816b2014-06-24 23:33:04 +00001762 // These stores are legal.
Tom Stellardb02094e2014-07-21 15:45:01 +00001763 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1764 if (VT.isVector() && VT.getVectorNumElements() > 4)
Matt Arsenault83e60582014-07-24 17:10:35 +00001765 return ScalarizeVectorStore(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001766 return SDValue();
1767 }
1768
Tom Stellard81d871d2013-11-13 23:36:50 +00001769 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1770 if (Ret.getNode())
1771 return Ret;
1772
1773 if (VT.isVector() && VT.getVectorNumElements() >= 8)
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001774 return SplitVectorStore(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001775
Tom Stellard1c8788e2014-03-07 20:12:33 +00001776 if (VT == MVT::i1)
1777 return DAG.getTruncStore(Store->getChain(), DL,
1778 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1779 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1780
Tom Stellarde812f2f2014-07-21 15:45:06 +00001781 return SDValue();
Tom Stellard81d871d2013-11-13 23:36:50 +00001782}
1783
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001784SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001785 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001786 EVT VT = Op.getValueType();
1787 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00001788 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001789 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
1790 DAG.getNode(ISD::FMUL, DL, VT, Arg,
1791 DAG.getConstantFP(0.5/M_PI, DL,
1792 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001793
1794 switch (Op.getOpcode()) {
1795 case ISD::FCOS:
1796 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1797 case ISD::FSIN:
1798 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1799 default:
1800 llvm_unreachable("Wrong trig opcode");
1801 }
1802}
1803
Tom Stellard75aadc22012-12-11 21:25:42 +00001804//===----------------------------------------------------------------------===//
1805// Custom DAG optimizations
1806//===----------------------------------------------------------------------===//
1807
Matt Arsenault364a6742014-06-11 17:50:44 +00001808SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00001809 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00001810 EVT VT = N->getValueType(0);
1811 EVT ScalarVT = VT.getScalarType();
1812 if (ScalarVT != MVT::f32)
1813 return SDValue();
1814
1815 SelectionDAG &DAG = DCI.DAG;
1816 SDLoc DL(N);
1817
1818 SDValue Src = N->getOperand(0);
1819 EVT SrcVT = Src.getValueType();
1820
1821 // TODO: We could try to match extracting the higher bytes, which would be
1822 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1823 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1824 // about in practice.
1825 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1826 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1827 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1828 DCI.AddToWorklist(Cvt.getNode());
1829 return Cvt;
1830 }
1831 }
1832
1833 // We are primarily trying to catch operations on illegal vector types
1834 // before they are expanded.
1835 // For scalars, we can use the more flexible method of checking masked bits
1836 // after legalization.
1837 if (!DCI.isBeforeLegalize() ||
1838 !SrcVT.isVector() ||
1839 SrcVT.getVectorElementType() != MVT::i8) {
1840 return SDValue();
1841 }
1842
1843 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1844
1845 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1846 // size as 4.
1847 unsigned NElts = SrcVT.getVectorNumElements();
1848 if (!SrcVT.isSimple() && NElts != 3)
1849 return SDValue();
1850
1851 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1852 // prevent a mess from expanding to v4i32 and repacking.
1853 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1854 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1855 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1856 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00001857 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001858
1859 unsigned AS = Load->getAddressSpace();
1860 unsigned Align = Load->getAlignment();
1861 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001862 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001863
1864 // Don't try to replace the load if we have to expand it due to alignment
1865 // problems. Otherwise we will end up scalarizing the load, and trying to
1866 // repack into the vector for no real reason.
1867 if (Align < ABIAlignment &&
1868 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
1869 return SDValue();
1870 }
1871
Matt Arsenault364a6742014-06-11 17:50:44 +00001872 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1873 Load->getChain(),
1874 Load->getBasePtr(),
1875 LoadVT,
1876 Load->getMemOperand());
1877
1878 // Make sure successors of the original load stay after it by updating
1879 // them to use the new Chain.
1880 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1881
1882 SmallVector<SDValue, 4> Elts;
1883 if (RegVT.isVector())
1884 DAG.ExtractVectorElements(NewLoad, Elts);
1885 else
1886 Elts.push_back(NewLoad);
1887
1888 SmallVector<SDValue, 4> Ops;
1889
1890 unsigned EltIdx = 0;
1891 for (SDValue Elt : Elts) {
1892 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1893 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1894 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1895 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1896 DCI.AddToWorklist(Cvt.getNode());
1897 Ops.push_back(Cvt);
1898 }
1899
1900 ++EltIdx;
1901 }
1902
1903 assert(Ops.size() == NElts);
1904
1905 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1906 }
1907
1908 return SDValue();
1909}
1910
Eric Christopher6c5b5112015-03-11 18:43:21 +00001911/// \brief Return true if the given offset Size in bytes can be folded into
1912/// the immediate offsets of a memory instruction for the given address space.
1913static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
1914 const AMDGPUSubtarget &STI) {
1915 switch (AS) {
1916 case AMDGPUAS::GLOBAL_ADDRESS: {
1917 // MUBUF instructions a 12-bit offset in bytes.
1918 return isUInt<12>(OffsetSize);
1919 }
1920 case AMDGPUAS::CONSTANT_ADDRESS: {
1921 // SMRD instructions have an 8-bit offset in dwords on SI and
1922 // a 20-bit offset in bytes on VI.
1923 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
1924 return isUInt<20>(OffsetSize);
1925 else
1926 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
1927 }
1928 case AMDGPUAS::LOCAL_ADDRESS:
1929 case AMDGPUAS::REGION_ADDRESS: {
1930 // The single offset versions have a 16-bit offset in bytes.
1931 return isUInt<16>(OffsetSize);
1932 }
1933 case AMDGPUAS::PRIVATE_ADDRESS:
1934 // Indirect register addressing does not use any offsets.
1935 default:
1936 return 0;
1937 }
1938}
1939
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001940// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
1941
1942// This is a variant of
1943// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
1944//
1945// The normal DAG combiner will do this, but only if the add has one use since
1946// that would increase the number of instructions.
1947//
1948// This prevents us from seeing a constant offset that can be folded into a
1949// memory instruction's addressing mode. If we know the resulting add offset of
1950// a pointer can be folded into an addressing offset, we can replace the pointer
1951// operand with the add of new constant offset. This eliminates one of the uses,
1952// and may allow the remaining use to also be simplified.
1953//
1954SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
1955 unsigned AddrSpace,
1956 DAGCombinerInfo &DCI) const {
1957 SDValue N0 = N->getOperand(0);
1958 SDValue N1 = N->getOperand(1);
1959
1960 if (N0.getOpcode() != ISD::ADD)
1961 return SDValue();
1962
1963 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
1964 if (!CN1)
1965 return SDValue();
1966
1967 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1968 if (!CAdd)
1969 return SDValue();
1970
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001971 // If the resulting offset is too large, we can't fold it into the addressing
1972 // mode offset.
1973 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Eric Christopher6c5b5112015-03-11 18:43:21 +00001974 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001975 return SDValue();
1976
1977 SelectionDAG &DAG = DCI.DAG;
1978 SDLoc SL(N);
1979 EVT VT = N->getValueType(0);
1980
1981 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001982 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001983
1984 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
1985}
1986
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001987SDValue SITargetLowering::performAndCombine(SDNode *N,
1988 DAGCombinerInfo &DCI) const {
1989 if (DCI.isBeforeLegalize())
1990 return SDValue();
1991
Matt Arsenault6e3a4512016-01-18 22:01:13 +00001992 if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI))
1993 return Base;
1994
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001995 SelectionDAG &DAG = DCI.DAG;
1996
1997 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
1998 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
1999 SDValue LHS = N->getOperand(0);
2000 SDValue RHS = N->getOperand(1);
2001
2002 if (LHS.getOpcode() == ISD::SETCC &&
2003 RHS.getOpcode() == ISD::SETCC) {
2004 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2005 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2006
2007 SDValue X = LHS.getOperand(0);
2008 SDValue Y = RHS.getOperand(0);
2009 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2010 return SDValue();
2011
2012 if (LCC == ISD::SETO) {
2013 if (X != LHS.getOperand(1))
2014 return SDValue();
2015
2016 if (RCC == ISD::SETUNE) {
2017 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2018 if (!C1 || !C1->isInfinity() || C1->isNegative())
2019 return SDValue();
2020
2021 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2022 SIInstrFlags::N_SUBNORMAL |
2023 SIInstrFlags::N_ZERO |
2024 SIInstrFlags::P_ZERO |
2025 SIInstrFlags::P_SUBNORMAL |
2026 SIInstrFlags::P_NORMAL;
2027
2028 static_assert(((~(SIInstrFlags::S_NAN |
2029 SIInstrFlags::Q_NAN |
2030 SIInstrFlags::N_INFINITY |
2031 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2032 "mask not equal");
2033
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002034 SDLoc DL(N);
2035 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2036 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002037 }
2038 }
2039 }
2040
2041 return SDValue();
2042}
2043
Matt Arsenaultf2290332015-01-06 23:00:39 +00002044SDValue SITargetLowering::performOrCombine(SDNode *N,
2045 DAGCombinerInfo &DCI) const {
2046 SelectionDAG &DAG = DCI.DAG;
2047 SDValue LHS = N->getOperand(0);
2048 SDValue RHS = N->getOperand(1);
2049
2050 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
2051 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
2052 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
2053 SDValue Src = LHS.getOperand(0);
2054 if (Src != RHS.getOperand(0))
2055 return SDValue();
2056
2057 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
2058 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
2059 if (!CLHS || !CRHS)
2060 return SDValue();
2061
2062 // Only 10 bits are used.
2063 static const uint32_t MaxMask = 0x3ff;
2064
2065 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002066 SDLoc DL(N);
2067 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2068 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00002069 }
2070
2071 return SDValue();
2072}
2073
2074SDValue SITargetLowering::performClassCombine(SDNode *N,
2075 DAGCombinerInfo &DCI) const {
2076 SelectionDAG &DAG = DCI.DAG;
2077 SDValue Mask = N->getOperand(1);
2078
2079 // fp_class x, 0 -> false
2080 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
2081 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002082 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002083 }
2084
2085 return SDValue();
2086}
2087
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002088static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
2089 switch (Opc) {
2090 case ISD::FMAXNUM:
2091 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002092 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002093 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002094 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002095 return AMDGPUISD::UMAX3;
2096 case ISD::FMINNUM:
2097 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002098 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002099 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002100 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002101 return AMDGPUISD::UMIN3;
2102 default:
2103 llvm_unreachable("Not a min/max opcode");
2104 }
2105}
2106
2107SDValue SITargetLowering::performMin3Max3Combine(SDNode *N,
2108 DAGCombinerInfo &DCI) const {
2109 SelectionDAG &DAG = DCI.DAG;
2110
2111 unsigned Opc = N->getOpcode();
2112 SDValue Op0 = N->getOperand(0);
2113 SDValue Op1 = N->getOperand(1);
2114
2115 // Only do this if the inner op has one use since this will just increases
2116 // register pressure for no benefit.
2117
2118 // max(max(a, b), c)
2119 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
2120 SDLoc DL(N);
2121 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2122 DL,
2123 N->getValueType(0),
2124 Op0.getOperand(0),
2125 Op0.getOperand(1),
2126 Op1);
2127 }
2128
2129 // max(a, max(b, c))
2130 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
2131 SDLoc DL(N);
2132 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2133 DL,
2134 N->getValueType(0),
2135 Op0,
2136 Op1.getOperand(0),
2137 Op1.getOperand(1));
2138 }
2139
2140 return SDValue();
2141}
2142
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002143SDValue SITargetLowering::performSetCCCombine(SDNode *N,
2144 DAGCombinerInfo &DCI) const {
2145 SelectionDAG &DAG = DCI.DAG;
2146 SDLoc SL(N);
2147
2148 SDValue LHS = N->getOperand(0);
2149 SDValue RHS = N->getOperand(1);
2150 EVT VT = LHS.getValueType();
2151
2152 if (VT != MVT::f32 && VT != MVT::f64)
2153 return SDValue();
2154
2155 // Match isinf pattern
2156 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
2157 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
2158 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
2159 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2160 if (!CRHS)
2161 return SDValue();
2162
2163 const APFloat &APF = CRHS->getValueAPF();
2164 if (APF.isInfinity() && !APF.isNegative()) {
2165 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002166 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
2167 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002168 }
2169 }
2170
2171 return SDValue();
2172}
2173
Tom Stellard75aadc22012-12-11 21:25:42 +00002174SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
2175 DAGCombinerInfo &DCI) const {
2176 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002177 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00002178
2179 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00002180 default:
2181 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002182 case ISD::SETCC:
2183 return performSetCCCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002184 case ISD::FMAXNUM: // TODO: What about fmax_legacy?
2185 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002186 case ISD::SMAX:
2187 case ISD::SMIN:
2188 case ISD::UMAX:
2189 case ISD::UMIN: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002190 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00002191 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002192 getTargetMachine().getOptLevel() > CodeGenOpt::None)
2193 return performMin3Max3Combine(N, DCI);
2194 break;
2195 }
Matt Arsenault364a6742014-06-11 17:50:44 +00002196
2197 case AMDGPUISD::CVT_F32_UBYTE0:
2198 case AMDGPUISD::CVT_F32_UBYTE1:
2199 case AMDGPUISD::CVT_F32_UBYTE2:
2200 case AMDGPUISD::CVT_F32_UBYTE3: {
2201 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
2202
2203 SDValue Src = N->getOperand(0);
2204 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
2205
2206 APInt KnownZero, KnownOne;
2207 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2208 !DCI.isBeforeLegalizeOps());
2209 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2210 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
2211 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
2212 DCI.CommitTargetLoweringOpt(TLO);
2213 }
2214
2215 break;
2216 }
2217
2218 case ISD::UINT_TO_FP: {
2219 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002220 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002221 case ISD::FADD: {
2222 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2223 break;
2224
2225 EVT VT = N->getValueType(0);
2226 if (VT != MVT::f32)
2227 break;
2228
Matt Arsenault8d630032015-02-20 22:10:41 +00002229 // Only do this if we are not trying to support denormals. v_mad_f32 does
2230 // not support denormals ever.
2231 if (Subtarget->hasFP32Denormals())
2232 break;
2233
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002234 SDValue LHS = N->getOperand(0);
2235 SDValue RHS = N->getOperand(1);
2236
2237 // These should really be instruction patterns, but writing patterns with
2238 // source modiifiers is a pain.
2239
2240 // fadd (fadd (a, a), b) -> mad 2.0, a, b
2241 if (LHS.getOpcode() == ISD::FADD) {
2242 SDValue A = LHS.getOperand(0);
2243 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002244 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002245 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002246 }
2247 }
2248
2249 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
2250 if (RHS.getOpcode() == ISD::FADD) {
2251 SDValue A = RHS.getOperand(0);
2252 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002253 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002254 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002255 }
2256 }
2257
Matt Arsenault8d630032015-02-20 22:10:41 +00002258 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002259 }
Matt Arsenault8675db12014-08-29 16:01:14 +00002260 case ISD::FSUB: {
2261 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2262 break;
2263
2264 EVT VT = N->getValueType(0);
2265
2266 // Try to get the fneg to fold into the source modifier. This undoes generic
2267 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00002268 //
2269 // Only do this if we are not trying to support denormals. v_mad_f32 does
2270 // not support denormals ever.
2271 if (VT == MVT::f32 &&
2272 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00002273 SDValue LHS = N->getOperand(0);
2274 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002275 if (LHS.getOpcode() == ISD::FADD) {
2276 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
2277
2278 SDValue A = LHS.getOperand(0);
2279 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002280 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002281 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
2282
Matt Arsenault8d630032015-02-20 22:10:41 +00002283 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002284 }
2285 }
2286
2287 if (RHS.getOpcode() == ISD::FADD) {
2288 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
2289
2290 SDValue A = RHS.getOperand(0);
2291 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002292 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002293 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002294 }
2295 }
Matt Arsenault8d630032015-02-20 22:10:41 +00002296
2297 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00002298 }
2299
2300 break;
2301 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002302 case ISD::LOAD:
2303 case ISD::STORE:
2304 case ISD::ATOMIC_LOAD:
2305 case ISD::ATOMIC_STORE:
2306 case ISD::ATOMIC_CMP_SWAP:
2307 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2308 case ISD::ATOMIC_SWAP:
2309 case ISD::ATOMIC_LOAD_ADD:
2310 case ISD::ATOMIC_LOAD_SUB:
2311 case ISD::ATOMIC_LOAD_AND:
2312 case ISD::ATOMIC_LOAD_OR:
2313 case ISD::ATOMIC_LOAD_XOR:
2314 case ISD::ATOMIC_LOAD_NAND:
2315 case ISD::ATOMIC_LOAD_MIN:
2316 case ISD::ATOMIC_LOAD_MAX:
2317 case ISD::ATOMIC_LOAD_UMIN:
2318 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
2319 if (DCI.isBeforeLegalize())
2320 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002321
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002322 MemSDNode *MemNode = cast<MemSDNode>(N);
2323 SDValue Ptr = MemNode->getBasePtr();
2324
2325 // TODO: We could also do this for multiplies.
2326 unsigned AS = MemNode->getAddressSpace();
2327 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
2328 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
2329 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002330 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002331
2332 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
2333 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
2334 }
2335 }
2336 break;
2337 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002338 case ISD::AND:
2339 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002340 case ISD::OR:
2341 return performOrCombine(N, DCI);
2342 case AMDGPUISD::FP_CLASS:
2343 return performClassCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002344 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002345 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00002346}
Christian Konigd910b7d2013-02-26 17:52:16 +00002347
Christian Konigf82901a2013-02-26 17:52:23 +00002348/// \brief Analyze the possible immediate value Op
2349///
2350/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
2351/// and the immediate value if it's a literal immediate
2352int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
2353
Eric Christopher7792e322015-01-30 23:24:40 +00002354 const SIInstrInfo *TII =
2355 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00002356
Tom Stellardedbf1eb2013-04-05 23:31:20 +00002357 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00002358 if (TII->isInlineConstant(Node->getAPIntValue()))
2359 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00002360
Matt Arsenault11a4d672015-02-13 19:05:03 +00002361 uint64_t Val = Node->getZExtValue();
2362 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00002363 }
2364
2365 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
2366 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
2367 return 0;
2368
2369 if (Node->getValueType(0) == MVT::f32)
2370 return FloatToBits(Node->getValueAPF().convertToFloat());
2371
2372 return -1;
2373 }
2374
2375 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00002376}
2377
Christian Konig8e06e2a2013-04-10 08:39:08 +00002378/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00002379static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00002380 switch (Idx) {
2381 default: return 0;
2382 case AMDGPU::sub0: return 0;
2383 case AMDGPU::sub1: return 1;
2384 case AMDGPU::sub2: return 2;
2385 case AMDGPU::sub3: return 3;
2386 }
2387}
2388
2389/// \brief Adjust the writemask of MIMG instructions
2390void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
2391 SelectionDAG &DAG) const {
2392 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00002393 unsigned Lane = 0;
2394 unsigned OldDmask = Node->getConstantOperandVal(0);
2395 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002396
2397 // Try to figure out the used register components
2398 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
2399 I != E; ++I) {
2400
2401 // Abort if we can't understand the usage
2402 if (!I->isMachineOpcode() ||
2403 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
2404 return;
2405
Tom Stellard54774e52013-10-23 02:53:47 +00002406 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
2407 // Note that subregs are packed, i.e. Lane==0 is the first bit set
2408 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
2409 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00002410 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00002411
Tom Stellard54774e52013-10-23 02:53:47 +00002412 // Set which texture component corresponds to the lane.
2413 unsigned Comp;
2414 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
2415 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00002416 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00002417 Dmask &= ~(1 << Comp);
2418 }
2419
Christian Konig8e06e2a2013-04-10 08:39:08 +00002420 // Abort if we have more than one user per component
2421 if (Users[Lane])
2422 return;
2423
2424 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00002425 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002426 }
2427
Tom Stellard54774e52013-10-23 02:53:47 +00002428 // Abort if there's no change
2429 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00002430 return;
2431
2432 // Adjust the writemask in the node
2433 std::vector<SDValue> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002434 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002435 Ops.insert(Ops.end(), Node->op_begin() + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00002436 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002437
Christian Konig8b1ed282013-04-10 08:39:16 +00002438 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00002439 // (if NewDmask has only one bit set...)
2440 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002441 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
2442 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00002443 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002444 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00002445 SDValue(Node, 0), RC);
2446 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
2447 return;
2448 }
2449
Christian Konig8e06e2a2013-04-10 08:39:08 +00002450 // Update the users of the node with the new indices
2451 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
2452
2453 SDNode *User = Users[i];
2454 if (!User)
2455 continue;
2456
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002457 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002458 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
2459
2460 switch (Idx) {
2461 default: break;
2462 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
2463 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
2464 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
2465 }
2466 }
2467}
2468
Tom Stellardc98ee202015-07-16 19:40:07 +00002469static bool isFrameIndexOp(SDValue Op) {
2470 if (Op.getOpcode() == ISD::AssertZext)
2471 Op = Op.getOperand(0);
2472
2473 return isa<FrameIndexSDNode>(Op);
2474}
2475
Tom Stellard3457a842014-10-09 19:06:00 +00002476/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
2477/// with frame index operands.
2478/// LLVM assumes that inputs are to these instructions are registers.
2479void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
2480 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002481
2482 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00002483 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00002484 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00002485 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002486 continue;
2487 }
2488
Tom Stellard3457a842014-10-09 19:06:00 +00002489 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002490 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00002491 Node->getOperand(i).getValueType(),
2492 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002493 }
2494
Tom Stellard3457a842014-10-09 19:06:00 +00002495 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002496}
2497
Matt Arsenault08d84942014-06-03 23:06:13 +00002498/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00002499SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
2500 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002501 const SIInstrInfo *TII =
2502 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konig8e06e2a2013-04-10 08:39:08 +00002503
Tom Stellard16a9a202013-08-14 23:24:17 +00002504 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00002505 adjustWritemask(Node, DAG);
2506
Matt Arsenault7d858d82014-11-02 23:46:54 +00002507 if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG ||
2508 Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002509 legalizeTargetIndependentNode(Node, DAG);
2510 return Node;
2511 }
Tom Stellard654d6692015-01-08 15:08:17 +00002512 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002513}
Christian Konig8b1ed282013-04-10 08:39:16 +00002514
2515/// \brief Assign the register class depending on the number of
2516/// bits set in the writemask
2517void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
2518 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002519 const SIInstrInfo *TII =
2520 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002521
Tom Stellarda99ada52014-11-21 22:31:44 +00002522 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00002523
2524 if (TII->isVOP3(MI->getOpcode())) {
2525 // Make sure constant bus requirements are respected.
2526 TII->legalizeOperandsVOP3(MRI, MI);
2527 return;
2528 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00002529
Matt Arsenault3add6432015-10-20 04:35:43 +00002530 if (TII->isMIMG(*MI)) {
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002531 unsigned VReg = MI->getOperand(0).getReg();
2532 unsigned Writemask = MI->getOperand(1).getImm();
2533 unsigned BitsSet = 0;
2534 for (unsigned i = 0; i < 4; ++i)
2535 BitsSet += Writemask & (1 << i) ? 1 : 0;
2536
2537 const TargetRegisterClass *RC;
2538 switch (BitsSet) {
2539 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002540 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002541 case 2: RC = &AMDGPU::VReg_64RegClass; break;
2542 case 3: RC = &AMDGPU::VReg_96RegClass; break;
2543 }
2544
2545 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
2546 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002547 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00002548 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00002549 }
2550
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002551 // Replace unused atomics with the no return version.
2552 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
2553 if (NoRetAtomicOp != -1) {
2554 if (!Node->hasAnyUseOfValue(0)) {
2555 MI->setDesc(TII->get(NoRetAtomicOp));
2556 MI->RemoveOperand(0);
2557 }
2558
2559 return;
2560 }
Christian Konig8b1ed282013-04-10 08:39:16 +00002561}
Tom Stellard0518ff82013-06-03 17:39:58 +00002562
Matt Arsenault485defe2014-11-05 19:01:17 +00002563static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002564 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00002565 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
2566}
2567
2568MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
2569 SDLoc DL,
2570 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002571 const SIInstrInfo *TII =
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002572 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00002573
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002574 // Build the half of the subregister with the constants before building the
2575 // full 128-bit register. If we are building multiple resource descriptors,
2576 // this will allow CSEing of the 2-component register.
2577 const SDValue Ops0[] = {
2578 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
2579 buildSMovImm32(DAG, DL, 0),
2580 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
2581 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
2582 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
2583 };
Matt Arsenault485defe2014-11-05 19:01:17 +00002584
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002585 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
2586 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00002587
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002588 // Combine the constants and the pointer.
2589 const SDValue Ops1[] = {
2590 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
2591 Ptr,
2592 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
2593 SubRegHi,
2594 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
2595 };
Matt Arsenault485defe2014-11-05 19:01:17 +00002596
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002597 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00002598}
2599
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002600/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002601/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
2602/// of the resource descriptor) to create an offset, which is added to
2603/// the resource pointer.
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002604MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
2605 SDLoc DL,
2606 SDValue Ptr,
2607 uint32_t RsrcDword1,
2608 uint64_t RsrcDword2And3) const {
2609 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
2610 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
2611 if (RsrcDword1) {
2612 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002613 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
2614 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002615 }
2616
2617 SDValue DataLo = buildSMovImm32(DAG, DL,
2618 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
2619 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
2620
2621 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002622 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002623 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002624 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002625 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002626 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002627 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002628 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002629 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002630 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002631 };
2632
2633 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2634}
2635
Tom Stellard94593ee2013-06-03 17:40:18 +00002636SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2637 const TargetRegisterClass *RC,
2638 unsigned Reg, EVT VT) const {
2639 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2640
2641 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2642 cast<RegisterSDNode>(VReg)->getReg(), VT);
2643}
Tom Stellardd7e6f132015-04-08 01:09:26 +00002644
2645//===----------------------------------------------------------------------===//
2646// SI Inline Assembly Support
2647//===----------------------------------------------------------------------===//
2648
2649std::pair<unsigned, const TargetRegisterClass *>
2650SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002651 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00002652 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002653
2654 if (Constraint.size() == 1) {
2655 switch (Constraint[0]) {
2656 case 's':
2657 case 'r':
2658 switch (VT.getSizeInBits()) {
2659 default:
2660 return std::make_pair(0U, nullptr);
2661 case 32:
Tom Stellardd7e6f132015-04-08 01:09:26 +00002662 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002663 case 64:
2664 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
2665 case 128:
2666 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
2667 case 256:
2668 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
2669 }
2670
2671 case 'v':
2672 switch (VT.getSizeInBits()) {
2673 default:
2674 return std::make_pair(0U, nullptr);
2675 case 32:
2676 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
2677 case 64:
2678 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
2679 case 96:
2680 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
2681 case 128:
2682 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
2683 case 256:
2684 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
2685 case 512:
2686 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
2687 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00002688 }
2689 }
2690
2691 if (Constraint.size() > 1) {
2692 const TargetRegisterClass *RC = nullptr;
2693 if (Constraint[1] == 'v') {
2694 RC = &AMDGPU::VGPR_32RegClass;
2695 } else if (Constraint[1] == 's') {
2696 RC = &AMDGPU::SGPR_32RegClass;
2697 }
2698
2699 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00002700 uint32_t Idx;
2701 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
2702 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00002703 return std::make_pair(RC->getRegister(Idx), RC);
2704 }
2705 }
2706 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
2707}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002708
2709SITargetLowering::ConstraintType
2710SITargetLowering::getConstraintType(StringRef Constraint) const {
2711 if (Constraint.size() == 1) {
2712 switch (Constraint[0]) {
2713 default: break;
2714 case 's':
2715 case 'v':
2716 return C_RegisterClass;
2717 }
2718 }
2719 return TargetLowering::getConstraintType(Constraint);
2720}