blob: 7c83e96921e409185c74fd5ff42c39a19b9ea628 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief Custom DAG lowering for SI
12//
13//===----------------------------------------------------------------------===//
14
NAKAMURA Takumi45e0a832014-07-20 11:15:07 +000015#ifdef _MSC_VER
16// Provide M_PI.
17#define _USE_MATH_DEFINES
18#include <cmath>
19#endif
20
Christian Konig99ee0f42013-03-07 09:04:14 +000021#include "AMDGPU.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000022#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000023#include "AMDGPUSubtarget.h"
Tom Stellard8485fa02016-12-07 02:42:15 +000024#include "SIDefines.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000025#include "SIISelLowering.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"
Matt Arsenault9a10cea2016-01-26 04:29:24 +000030#include "llvm/ADT/StringSwitch.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000031#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000032#include "llvm/CodeGen/MachineInstrBuilder.h"
33#include "llvm/CodeGen/MachineRegisterInfo.h"
34#include "llvm/CodeGen/SelectionDAG.h"
Wei Ding07e03712016-07-28 16:42:13 +000035#include "llvm/CodeGen/Analysis.h"
Oliver Stannard7e7d9832016-02-02 13:52:43 +000036#include "llvm/IR/DiagnosticInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000037#include "llvm/IR/Function.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000038
39using namespace llvm;
40
Matt Arsenaultd486d3f2016-10-12 18:49:05 +000041static cl::opt<bool> EnableVGPRIndexMode(
42 "amdgpu-vgpr-index-mode",
43 cl::desc("Use GPR indexing mode instead of movrel for vector indexing"),
44 cl::init(false));
45
46
Tom Stellardf110f8f2016-04-14 16:27:03 +000047static unsigned findFirstFreeSGPR(CCState &CCInfo) {
48 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
49 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) {
50 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) {
51 return AMDGPU::SGPR0 + Reg;
52 }
53 }
54 llvm_unreachable("Cannot allocate sgpr");
55}
56
Matt Arsenault43e92fe2016-06-24 06:30:11 +000057SITargetLowering::SITargetLowering(const TargetMachine &TM,
58 const SISubtarget &STI)
Eric Christopher7792e322015-01-30 23:24:40 +000059 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000060 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000061 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000062
Marek Olsak79c05872016-11-25 17:37:09 +000063 addRegisterClass(MVT::i32, &AMDGPU::SReg_32_XM0RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000064 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000065
Tom Stellard436780b2014-05-15 14:41:57 +000066 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
67 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
68 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000069
Matt Arsenault61001bb2015-11-25 19:58:34 +000070 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
71 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
72
Tom Stellard436780b2014-05-15 14:41:57 +000073 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
74 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000075
Tom Stellardf0a21072014-11-18 20:39:39 +000076 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000077 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
78
Tom Stellardf0a21072014-11-18 20:39:39 +000079 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000080 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000081
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +000082 if (Subtarget->has16BitInsts()) {
Marek Olsak79c05872016-11-25 17:37:09 +000083 addRegisterClass(MVT::i16, &AMDGPU::SReg_32_XM0RegClass);
84 addRegisterClass(MVT::f16, &AMDGPU::SReg_32_XM0RegClass);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +000085 }
Tom Stellard115a6152016-11-10 16:02:37 +000086
Eric Christopher23a3a7c2015-02-26 00:00:24 +000087 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000088
Tom Stellard35bb18c2013-08-26 15:06:04 +000089 // We need to custom lower vector stores from local memory
Matt Arsenault71e66762016-05-21 02:27:49 +000090 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000091 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000092 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
93 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000094 setOperationAction(ISD::LOAD, MVT::i1, Custom);
Matt Arsenault2b957b52016-05-02 20:07:26 +000095
Matt Arsenaultbcdfee72016-05-02 20:13:51 +000096 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000097 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
98 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
99 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
100 setOperationAction(ISD::STORE, MVT::i1, Custom);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +0000101
Jan Vesely06200bd2017-01-06 21:00:46 +0000102 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Expand);
103 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
104 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
105 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
106 setTruncStoreAction(MVT::v32i32, MVT::v32i16, Expand);
107 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Expand);
108 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Expand);
109 setTruncStoreAction(MVT::v8i32, MVT::v8i8, Expand);
110 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand);
111 setTruncStoreAction(MVT::v32i32, MVT::v32i8, Expand);
112
113
Matt Arsenault71e66762016-05-21 02:27:49 +0000114 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
115 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000116 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
117
118 setOperationAction(ISD::SELECT, MVT::i1, Promote);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000119 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +0000120 setOperationAction(ISD::SELECT, MVT::f64, Promote);
121 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000122
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000123 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
124 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
125 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
126 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Matt Arsenault71e66762016-05-21 02:27:49 +0000127 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000128
Tom Stellardd1efda82016-01-20 21:48:24 +0000129 setOperationAction(ISD::SETCC, MVT::i1, Promote);
Tom Stellard83747202013-07-18 21:43:53 +0000130 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
131 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
Matt Arsenault18f56be2016-12-22 16:27:11 +0000132 AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
Tom Stellard83747202013-07-18 21:43:53 +0000133
Matt Arsenault71e66762016-05-21 02:27:49 +0000134 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
135 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000136
Matt Arsenault4e466652014-04-16 01:41:30 +0000137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000139 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
140 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000141 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
142 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000143 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
144
Tom Stellard9fa17912013-08-14 23:24:45 +0000145 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000146 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000147 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
148
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000149 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000150 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000151 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
152 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
153 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
154 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000155
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000156 // We only support LOAD/STORE and vector manipulation ops for vectors
157 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000158 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000159 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000160 switch (Op) {
Tom Stellard967bf582014-02-13 23:34:15 +0000161 case ISD::LOAD:
162 case ISD::STORE:
163 case ISD::BUILD_VECTOR:
164 case ISD::BITCAST:
165 case ISD::EXTRACT_VECTOR_ELT:
166 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000167 case ISD::INSERT_SUBVECTOR:
168 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000169 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000170 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000171 case ISD::CONCAT_VECTORS:
172 setOperationAction(Op, VT, Custom);
173 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000174 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000175 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000176 break;
177 }
178 }
179 }
180
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000181 // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that
182 // is expanded to avoid having two separate loops in case the index is a VGPR.
183
Matt Arsenault61001bb2015-11-25 19:58:34 +0000184 // Most operations are naturally 32-bit vector operations. We only support
185 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
186 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
187 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
188 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
189
190 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
191 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
192
193 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
194 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
195
196 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
197 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
198 }
199
Matt Arsenault71e66762016-05-21 02:27:49 +0000200 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
201 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
202 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
203 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000204
Tom Stellard354a43c2016-04-01 18:27:37 +0000205 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
206 // and output demarshalling
207 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
208 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
209
210 // We can't return success/failure, only the old value,
211 // let LLVM add the comparison
212 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand);
213 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand);
214
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000215 if (getSubtarget()->hasFlatAddressSpace()) {
Matt Arsenault99c14522016-04-25 19:27:24 +0000216 setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom);
217 setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom);
218 }
219
Matt Arsenault71e66762016-05-21 02:27:49 +0000220 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
221 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
222
223 // On SI this is s_memtime and s_memrealtime on VI.
224 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
Matt Arsenault0bb294b2016-06-17 22:27:03 +0000225 setOperationAction(ISD::TRAP, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000226
227 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
228 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
229
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000230 if (Subtarget->getGeneration() >= SISubtarget::SEA_ISLANDS) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000231 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
232 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
233 setOperationAction(ISD::FRINT, MVT::f64, Legal);
234 }
235
236 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
237
238 setOperationAction(ISD::FSIN, MVT::f32, Custom);
239 setOperationAction(ISD::FCOS, MVT::f32, Custom);
240 setOperationAction(ISD::FDIV, MVT::f32, Custom);
241 setOperationAction(ISD::FDIV, MVT::f64, Custom);
242
Tom Stellard115a6152016-11-10 16:02:37 +0000243 if (Subtarget->has16BitInsts()) {
244 setOperationAction(ISD::Constant, MVT::i16, Legal);
245
246 setOperationAction(ISD::SMIN, MVT::i16, Legal);
247 setOperationAction(ISD::SMAX, MVT::i16, Legal);
248
249 setOperationAction(ISD::UMIN, MVT::i16, Legal);
250 setOperationAction(ISD::UMAX, MVT::i16, Legal);
251
Tom Stellard115a6152016-11-10 16:02:37 +0000252 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Promote);
253 AddPromotedToType(ISD::SIGN_EXTEND, MVT::i16, MVT::i32);
254
255 setOperationAction(ISD::ROTR, MVT::i16, Promote);
256 setOperationAction(ISD::ROTL, MVT::i16, Promote);
257
258 setOperationAction(ISD::SDIV, MVT::i16, Promote);
259 setOperationAction(ISD::UDIV, MVT::i16, Promote);
260 setOperationAction(ISD::SREM, MVT::i16, Promote);
261 setOperationAction(ISD::UREM, MVT::i16, Promote);
262
263 setOperationAction(ISD::BSWAP, MVT::i16, Promote);
264 setOperationAction(ISD::BITREVERSE, MVT::i16, Promote);
265
266 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
267 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Promote);
268 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
269 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Promote);
270
271 setOperationAction(ISD::SELECT_CC, MVT::i16, Expand);
272
273 setOperationAction(ISD::BR_CC, MVT::i16, Expand);
274
275 setOperationAction(ISD::LOAD, MVT::i16, Custom);
276
277 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
278
Tom Stellard115a6152016-11-10 16:02:37 +0000279 setOperationAction(ISD::FP16_TO_FP, MVT::i16, Promote);
280 AddPromotedToType(ISD::FP16_TO_FP, MVT::i16, MVT::i32);
281 setOperationAction(ISD::FP_TO_FP16, MVT::i16, Promote);
282 AddPromotedToType(ISD::FP_TO_FP16, MVT::i16, MVT::i32);
Tom Stellardb4c8e8e2016-11-12 00:19:11 +0000283
Konstantin Zhuravlyov3f0cdc72016-11-17 04:00:46 +0000284 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
285 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
286 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
287 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
Tom Stellardb4c8e8e2016-11-12 00:19:11 +0000288
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000289 // F16 - Constant Actions.
Matt Arsenaulte96d0372016-12-08 20:14:46 +0000290 setOperationAction(ISD::ConstantFP, MVT::f16, Legal);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000291
292 // F16 - Load/Store Actions.
293 setOperationAction(ISD::LOAD, MVT::f16, Promote);
294 AddPromotedToType(ISD::LOAD, MVT::f16, MVT::i16);
295 setOperationAction(ISD::STORE, MVT::f16, Promote);
296 AddPromotedToType(ISD::STORE, MVT::f16, MVT::i16);
297
298 // F16 - VOP1 Actions.
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +0000299 setOperationAction(ISD::FP_ROUND, MVT::f16, Custom);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000300 setOperationAction(ISD::FCOS, MVT::f16, Promote);
301 setOperationAction(ISD::FSIN, MVT::f16, Promote);
Konstantin Zhuravlyov3f0cdc72016-11-17 04:00:46 +0000302 setOperationAction(ISD::FP_TO_SINT, MVT::f16, Promote);
303 setOperationAction(ISD::FP_TO_UINT, MVT::f16, Promote);
304 setOperationAction(ISD::SINT_TO_FP, MVT::f16, Promote);
305 setOperationAction(ISD::UINT_TO_FP, MVT::f16, Promote);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000306
307 // F16 - VOP2 Actions.
Konstantin Zhuravlyov662e01d2016-11-17 03:49:01 +0000308 setOperationAction(ISD::BR_CC, MVT::f16, Expand);
Konstantin Zhuravlyov2a87a422016-11-16 03:16:26 +0000309 setOperationAction(ISD::SELECT_CC, MVT::f16, Expand);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000310 setOperationAction(ISD::FMAXNUM, MVT::f16, Legal);
311 setOperationAction(ISD::FMINNUM, MVT::f16, Legal);
Matt Arsenault4052a572016-12-22 03:05:41 +0000312 setOperationAction(ISD::FDIV, MVT::f16, Custom);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000313
314 // F16 - VOP3 Actions.
315 setOperationAction(ISD::FMA, MVT::f16, Legal);
316 if (!Subtarget->hasFP16Denormals())
317 setOperationAction(ISD::FMAD, MVT::f16, Legal);
Tom Stellard115a6152016-11-10 16:02:37 +0000318 }
319
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000320 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000321 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000322 setTargetDAGCombine(ISD::FMINNUM);
323 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000324 setTargetDAGCombine(ISD::SMIN);
325 setTargetDAGCombine(ISD::SMAX);
326 setTargetDAGCombine(ISD::UMIN);
327 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000328 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000329 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000330 setTargetDAGCombine(ISD::OR);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000331 setTargetDAGCombine(ISD::XOR);
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +0000332 setTargetDAGCombine(ISD::SINT_TO_FP);
Matt Arsenault364a6742014-06-11 17:50:44 +0000333 setTargetDAGCombine(ISD::UINT_TO_FP);
Matt Arsenault9cd90712016-04-14 01:42:16 +0000334 setTargetDAGCombine(ISD::FCANONICALIZE);
Matt Arsenault364a6742014-06-11 17:50:44 +0000335
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000336 // All memory operations. Some folding on the pointer operand is done to help
337 // matching the constant offsets in the addressing modes.
338 setTargetDAGCombine(ISD::LOAD);
339 setTargetDAGCombine(ISD::STORE);
340 setTargetDAGCombine(ISD::ATOMIC_LOAD);
341 setTargetDAGCombine(ISD::ATOMIC_STORE);
342 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
343 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
344 setTargetDAGCombine(ISD::ATOMIC_SWAP);
345 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
346 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
347 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
348 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
349 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
350 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
351 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
352 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
353 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
354 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
355
Christian Konigeecebd02013-03-26 14:04:02 +0000356 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000357}
358
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000359const SISubtarget *SITargetLowering::getSubtarget() const {
360 return static_cast<const SISubtarget *>(Subtarget);
361}
362
Tom Stellard0125f2a2013-06-25 02:39:35 +0000363//===----------------------------------------------------------------------===//
364// TargetLowering queries
365//===----------------------------------------------------------------------===//
366
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000367bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
368 const CallInst &CI,
369 unsigned IntrID) const {
370 switch (IntrID) {
371 case Intrinsic::amdgcn_atomic_inc:
372 case Intrinsic::amdgcn_atomic_dec:
373 Info.opc = ISD::INTRINSIC_W_CHAIN;
374 Info.memVT = MVT::getVT(CI.getType());
375 Info.ptrVal = CI.getOperand(0);
376 Info.align = 0;
377 Info.vol = false;
378 Info.readMem = true;
379 Info.writeMem = true;
380 return true;
381 default:
382 return false;
383 }
384}
385
Matt Arsenaulte306a322014-10-21 16:25:08 +0000386bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
387 EVT) const {
388 // SI has some legal vector types, but no legal vector operations. Say no
389 // shuffles are legal in order to prefer scalarizing some vector operations.
390 return false;
391}
392
Tom Stellard70580f82015-07-20 14:28:41 +0000393bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
394 // Flat instructions do not have offsets, and only have the register
395 // address.
396 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
397}
398
Matt Arsenault711b3902015-08-07 20:18:34 +0000399bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
400 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
401 // additionally can do r + r + i with addr64. 32-bit has more addressing
402 // mode options. Depending on the resource constant, it can also do
403 // (i64 r0) + (i32 r1) * (i14 i).
404 //
405 // Private arrays end up using a scratch buffer most of the time, so also
406 // assume those use MUBUF instructions. Scratch loads / stores are currently
407 // implemented as mubuf instructions with offen bit set, so slightly
408 // different than the normal addr64.
409 if (!isUInt<12>(AM.BaseOffs))
410 return false;
411
412 // FIXME: Since we can split immediate into soffset and immediate offset,
413 // would it make sense to allow any immediate?
414
415 switch (AM.Scale) {
416 case 0: // r + i or just i, depending on HasBaseReg.
417 return true;
418 case 1:
419 return true; // We have r + r or r + i.
420 case 2:
421 if (AM.HasBaseReg) {
422 // Reject 2 * r + r.
423 return false;
424 }
425
426 // Allow 2 * r as r + r
427 // Or 2 * r + i is allowed as r + r + i.
428 return true;
429 default: // Don't allow n * r
430 return false;
431 }
432}
433
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000434bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
435 const AddrMode &AM, Type *Ty,
436 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000437 // No global is ever allowed as a base.
438 if (AM.BaseGV)
439 return false;
440
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000441 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000442 case AMDGPUAS::GLOBAL_ADDRESS: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000443 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) {
Tom Stellard70580f82015-07-20 14:28:41 +0000444 // Assume the we will use FLAT for all global memory accesses
445 // on VI.
446 // FIXME: This assumption is currently wrong. On VI we still use
447 // MUBUF instructions for the r + i addressing mode. As currently
448 // implemented, the MUBUF instructions only work on buffer < 4GB.
449 // It may be possible to support > 4GB buffers with MUBUF instructions,
450 // by setting the stride value in the resource descriptor which would
451 // increase the size limit to (stride * 4GB). However, this is risky,
452 // because it has never been validated.
453 return isLegalFlatAddressingMode(AM);
454 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000455
Matt Arsenault711b3902015-08-07 20:18:34 +0000456 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000457 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000458 case AMDGPUAS::CONSTANT_ADDRESS: {
459 // If the offset isn't a multiple of 4, it probably isn't going to be
460 // correctly aligned.
Matt Arsenault3cc1e002016-08-13 01:43:51 +0000461 // FIXME: Can we get the real alignment here?
Matt Arsenault711b3902015-08-07 20:18:34 +0000462 if (AM.BaseOffs % 4 != 0)
463 return isLegalMUBUFAddressingMode(AM);
464
465 // There are no SMRD extloads, so if we have to do a small type access we
466 // will use a MUBUF load.
467 // FIXME?: We also need to do this if unaligned, but we don't know the
468 // alignment here.
469 if (DL.getTypeStoreSize(Ty) < 4)
470 return isLegalMUBUFAddressingMode(AM);
471
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000472 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000473 // SMRD instructions have an 8-bit, dword offset on SI.
474 if (!isUInt<8>(AM.BaseOffs / 4))
475 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000476 } else if (Subtarget->getGeneration() == SISubtarget::SEA_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000477 // On CI+, this can also be a 32-bit literal constant offset. If it fits
478 // in 8-bits, it can use a smaller encoding.
479 if (!isUInt<32>(AM.BaseOffs / 4))
480 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000481 } else if (Subtarget->getGeneration() == SISubtarget::VOLCANIC_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000482 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
483 if (!isUInt<20>(AM.BaseOffs))
484 return false;
485 } else
486 llvm_unreachable("unhandled generation");
487
488 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
489 return true;
490
491 if (AM.Scale == 1 && AM.HasBaseReg)
492 return true;
493
494 return false;
495 }
496
497 case AMDGPUAS::PRIVATE_ADDRESS:
Matt Arsenault711b3902015-08-07 20:18:34 +0000498 return isLegalMUBUFAddressingMode(AM);
499
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000500 case AMDGPUAS::LOCAL_ADDRESS:
501 case AMDGPUAS::REGION_ADDRESS: {
502 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
503 // field.
504 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
505 // an 8-bit dword offset but we don't know the alignment here.
506 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000507 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000508
509 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
510 return true;
511
512 if (AM.Scale == 1 && AM.HasBaseReg)
513 return true;
514
Matt Arsenault5015a892014-08-15 17:17:07 +0000515 return false;
516 }
Tom Stellard70580f82015-07-20 14:28:41 +0000517 case AMDGPUAS::FLAT_ADDRESS:
Matt Arsenault7d1b6c82016-04-29 06:25:10 +0000518 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
519 // For an unknown address space, this usually means that this is for some
520 // reason being used for pure arithmetic, and not based on some addressing
521 // computation. We don't have instructions that compute pointers with any
522 // addressing modes, so treat them as having no offset like flat
523 // instructions.
Tom Stellard70580f82015-07-20 14:28:41 +0000524 return isLegalFlatAddressingMode(AM);
525
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000526 default:
527 llvm_unreachable("unhandled address space");
528 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000529}
530
Matt Arsenaulte6986632015-01-14 01:35:22 +0000531bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000532 unsigned AddrSpace,
533 unsigned Align,
534 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000535 if (IsFast)
536 *IsFast = false;
537
Matt Arsenault1018c892014-04-24 17:08:26 +0000538 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
539 // which isn't a simple VT.
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000540 // Until MVT is extended to handle this, simply check for the size and
541 // rely on the condition below: allow accesses if the size is a multiple of 4.
542 if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 &&
543 VT.getStoreSize() > 16)) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000544 return false;
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000545 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000546
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000547 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
548 AddrSpace == AMDGPUAS::REGION_ADDRESS) {
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000549 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
550 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
551 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000552 bool AlignedBy4 = (Align % 4 == 0);
553 if (IsFast)
554 *IsFast = AlignedBy4;
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000555
Sanjay Patelce74db92015-09-03 15:03:19 +0000556 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000557 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000558
Tom Stellard64a9d082016-10-14 18:10:39 +0000559 // FIXME: We have to be conservative here and assume that flat operations
560 // will access scratch. If we had access to the IR function, then we
561 // could determine if any private memory was used in the function.
562 if (!Subtarget->hasUnalignedScratchAccess() &&
563 (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS ||
564 AddrSpace == AMDGPUAS::FLAT_ADDRESS)) {
565 return false;
566 }
567
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000568 if (Subtarget->hasUnalignedBufferAccess()) {
569 // If we have an uniform constant load, it still requires using a slow
570 // buffer instruction if unaligned.
571 if (IsFast) {
572 *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) ?
573 (Align % 4 == 0) : true;
574 }
575
576 return true;
577 }
578
Tom Stellard33e64c62015-02-04 20:49:52 +0000579 // Smaller than dword value must be aligned.
Tom Stellard33e64c62015-02-04 20:49:52 +0000580 if (VT.bitsLT(MVT::i32))
581 return false;
582
Matt Arsenault1018c892014-04-24 17:08:26 +0000583 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
584 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000585 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000586 if (IsFast)
587 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000588
589 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000590}
591
Matt Arsenault46645fa2014-07-28 17:49:26 +0000592EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
593 unsigned SrcAlign, bool IsMemset,
594 bool ZeroMemset,
595 bool MemcpyStrSrc,
596 MachineFunction &MF) const {
597 // FIXME: Should account for address space here.
598
599 // The default fallback uses the private pointer size as a guess for a type to
600 // use. Make sure we switch these to 64-bit accesses.
601
602 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
603 return MVT::v4i32;
604
605 if (Size >= 8 && DstAlign >= 4)
606 return MVT::v2i32;
607
608 // Use the default.
609 return MVT::Other;
610}
611
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000612static bool isFlatGlobalAddrSpace(unsigned AS) {
613 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
Matt Arsenaultd4da0ed2016-12-02 18:12:53 +0000614 AS == AMDGPUAS::FLAT_ADDRESS ||
615 AS == AMDGPUAS::CONSTANT_ADDRESS;
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000616}
617
618bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
619 unsigned DestAS) const {
Matt Arsenault37fefd62016-06-10 02:18:02 +0000620 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000621}
622
Alexander Timofeev18009562016-12-08 17:28:47 +0000623bool SITargetLowering::isMemOpHasNoClobberedMemOperand(const SDNode *N) const {
624 const MemSDNode *MemNode = cast<MemSDNode>(N);
625 const Value *Ptr = MemNode->getMemOperand()->getValue();
626 const Instruction *I = dyn_cast<Instruction>(Ptr);
627 return I && I->getMetadata("amdgpu.noclobber");
628}
629
Matt Arsenaultd4da0ed2016-12-02 18:12:53 +0000630bool SITargetLowering::isCheapAddrSpaceCast(unsigned SrcAS,
631 unsigned DestAS) const {
632 // Flat -> private/local is a simple truncate.
633 // Flat -> global is no-op
634 if (SrcAS == AMDGPUAS::FLAT_ADDRESS)
635 return true;
636
637 return isNoopAddrSpaceCast(SrcAS, DestAS);
638}
639
Tom Stellarda6f24c62015-12-15 20:55:55 +0000640bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
641 const MemSDNode *MemNode = cast<MemSDNode>(N);
642 const Value *Ptr = MemNode->getMemOperand()->getValue();
643
644 // UndefValue means this is a load of a kernel input. These are uniform.
Tom Stellard418beb72016-07-13 14:23:33 +0000645 // Sometimes LDS instructions have constant pointers.
646 // If Ptr is null, then that means this mem operand contains a
647 // PseudoSourceValue like GOT.
648 if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) ||
649 isa<Constant>(Ptr) || isa<GlobalValue>(Ptr))
Tom Stellarda6f24c62015-12-15 20:55:55 +0000650 return true;
651
Tom Stellard418beb72016-07-13 14:23:33 +0000652 const Instruction *I = dyn_cast<Instruction>(Ptr);
Tom Stellarda6f24c62015-12-15 20:55:55 +0000653 return I && I->getMetadata("amdgpu.uniform");
654}
655
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000656TargetLoweringBase::LegalizeTypeAction
657SITargetLowering::getPreferredVectorAction(EVT VT) const {
658 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
659 return TypeSplitVector;
660
661 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000662}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000663
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000664bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
665 Type *Ty) const {
Matt Arsenault749035b2016-07-30 01:40:36 +0000666 // FIXME: Could be smarter if called for vector constants.
667 return true;
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000668}
669
Tom Stellard2e045bb2016-01-20 00:13:22 +0000670bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
Matt Arsenault7b00cf42016-12-09 17:57:43 +0000671 if (Subtarget->has16BitInsts() && VT == MVT::i16) {
672 switch (Op) {
673 case ISD::LOAD:
674 case ISD::STORE:
Tom Stellard2e045bb2016-01-20 00:13:22 +0000675
Matt Arsenault7b00cf42016-12-09 17:57:43 +0000676 // These operations are done with 32-bit instructions anyway.
677 case ISD::AND:
678 case ISD::OR:
679 case ISD::XOR:
680 case ISD::SELECT:
681 // TODO: Extensions?
682 return true;
683 default:
684 return false;
685 }
686 }
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000687
Tom Stellard2e045bb2016-01-20 00:13:22 +0000688 // SimplifySetCC uses this function to determine whether or not it should
689 // create setcc with i1 operands. We don't have instructions for i1 setcc.
690 if (VT == MVT::i1 && Op == ISD::SETCC)
691 return false;
692
693 return TargetLowering::isTypeDesirableForOp(Op, VT);
694}
695
Jan Veselyfea814d2016-06-21 20:46:20 +0000696SDValue SITargetLowering::LowerParameterPtr(SelectionDAG &DAG,
697 const SDLoc &SL, SDValue Chain,
698 unsigned Offset) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000699 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000700 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000701 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaultac234b62015-11-30 21:15:57 +0000702 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000703
Matt Arsenault86033ca2014-07-28 17:31:39 +0000704 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000705 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000706 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
707 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
Jan Veselyfea814d2016-06-21 20:46:20 +0000708 return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
709 DAG.getConstant(Offset, SL, PtrVT));
710}
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000711
Jan Veselyfea814d2016-06-21 20:46:20 +0000712SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
713 const SDLoc &SL, SDValue Chain,
714 unsigned Offset, bool Signed) const {
715 const DataLayout &DL = DAG.getDataLayout();
Tom Stellard083f1622016-10-17 16:56:19 +0000716 Type *Ty = MemVT.getTypeForEVT(*DAG.getContext());
Jan Veselyfea814d2016-06-21 20:46:20 +0000717 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000718 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
719
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000720 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000721
Jan Veselyfea814d2016-06-21 20:46:20 +0000722 SDValue Ptr = LowerParameterPtr(DAG, SL, Chain, Offset);
Tom Stellardbc6c5232016-10-17 16:21:45 +0000723 SDValue Load = DAG.getLoad(MemVT, SL, Chain, Ptr, PtrInfo, Align,
724 MachineMemOperand::MONonTemporal |
725 MachineMemOperand::MODereferenceable |
726 MachineMemOperand::MOInvariant);
727
728 SDValue Val;
729 if (MemVT.isFloatingPoint())
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000730 Val = getFPExtOrFPTrunc(DAG, Load, SL, VT);
Tom Stellardbc6c5232016-10-17 16:21:45 +0000731 else if (Signed)
732 Val = DAG.getSExtOrTrunc(Load, SL, VT);
733 else
734 Val = DAG.getZExtOrTrunc(Load, SL, VT);
735
736 SDValue Ops[] = {
737 Val,
738 Load.getValue(1)
739 };
740
741 return DAG.getMergeValues(Ops, SL);
Tom Stellard94593ee2013-06-03 17:40:18 +0000742}
743
Christian Konig2c8f6d52013-03-07 09:03:52 +0000744SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000745 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000746 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
747 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000748 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000749
750 MachineFunction &MF = DAG.getMachineFunction();
751 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000752 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000753 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000754
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000755 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) {
Matt Arsenaultd48da142015-11-02 23:23:02 +0000756 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000757 DiagnosticInfoUnsupported NoGraphicsHSA(
758 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000759 DAG.getContext()->diagnose(NoGraphicsHSA);
Diana Picus81bc3172016-05-26 15:24:55 +0000760 return DAG.getEntryNode();
Matt Arsenaultd48da142015-11-02 23:23:02 +0000761 }
762
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000763 // Create stack objects that are used for emitting debugger prologue if
764 // "amdgpu-debugger-emit-prologue" attribute was specified.
765 if (ST.debuggerEmitPrologue())
766 createDebuggerPrologueStackObjects(MF);
767
Christian Konig2c8f6d52013-03-07 09:03:52 +0000768 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000769 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000770
771 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000772 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000773
774 // First check if it's a PS input addr
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000775 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000776 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000777
Marek Olsakfccabaf2016-01-13 11:45:36 +0000778 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000779 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000780 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000781 ++PSInputNum;
782 continue;
783 }
784
Marek Olsakfccabaf2016-01-13 11:45:36 +0000785 Info->markPSInputAllocated(PSInputNum);
786 if (Arg.Used)
787 Info->PSInputEna |= 1 << PSInputNum;
788
789 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000790 }
791
Matt Arsenault539ca882016-05-05 20:27:02 +0000792 if (AMDGPU::isShader(CallConv)) {
793 // Second split vertices into their elements
794 if (Arg.VT.isVector()) {
795 ISD::InputArg NewArg = Arg;
796 NewArg.Flags.setSplit();
797 NewArg.VT = Arg.VT.getVectorElementType();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000798
Matt Arsenault539ca882016-05-05 20:27:02 +0000799 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
800 // three or five element vertex only needs three or five registers,
801 // NOT four or eight.
802 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
803 unsigned NumElements = ParamType->getVectorNumElements();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000804
Matt Arsenault539ca882016-05-05 20:27:02 +0000805 for (unsigned j = 0; j != NumElements; ++j) {
806 Splits.push_back(NewArg);
807 NewArg.PartOffset += NewArg.VT.getStoreSize();
808 }
809 } else {
810 Splits.push_back(Arg);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000811 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000812 }
813 }
814
815 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000816 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
817 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000818
Christian Konig99ee0f42013-03-07 09:04:14 +0000819 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000820 //
821 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
822 // PSInputAddr, the user wants to enable some bits after the compilation
823 // based on run-time states. Since we can't know what the final PSInputEna
824 // will look like, so we shouldn't do anything here and the user should take
825 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000826 //
827 // Otherwise, the following restrictions apply:
828 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
829 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
830 // enabled too.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000831 if (CallConv == CallingConv::AMDGPU_PS &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000832 ((Info->getPSInputAddr() & 0x7F) == 0 ||
NAKAMURA Takumife1202c2016-06-20 00:37:41 +0000833 ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000834 CCInfo.AllocateReg(AMDGPU::VGPR0);
835 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000836 Info->markPSInputAllocated(0);
837 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000838 }
839
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000840 if (!AMDGPU::isShader(CallConv)) {
Tom Stellardf110f8f2016-04-14 16:27:03 +0000841 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
842 } else {
843 assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() &&
844 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() &&
845 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
846 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
847 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
848 !Info->hasWorkItemIDZ());
Tom Stellardaf775432013-10-23 00:44:32 +0000849 }
850
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000851 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
852 if (Info->hasPrivateSegmentBuffer()) {
853 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
854 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
855 CCInfo.AllocateReg(PrivateSegmentBufferReg);
856 }
857
858 if (Info->hasDispatchPtr()) {
859 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000860 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000861 CCInfo.AllocateReg(DispatchPtrReg);
862 }
863
Matt Arsenault48ab5262016-04-25 19:27:18 +0000864 if (Info->hasQueuePtr()) {
865 unsigned QueuePtrReg = Info->addQueuePtr(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000866 MF.addLiveIn(QueuePtrReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault48ab5262016-04-25 19:27:18 +0000867 CCInfo.AllocateReg(QueuePtrReg);
868 }
869
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000870 if (Info->hasKernargSegmentPtr()) {
871 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000872 MF.addLiveIn(InputPtrReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000873 CCInfo.AllocateReg(InputPtrReg);
874 }
875
Matt Arsenault8d718dc2016-07-22 17:01:30 +0000876 if (Info->hasDispatchID()) {
877 unsigned DispatchIDReg = Info->addDispatchID(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000878 MF.addLiveIn(DispatchIDReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault8d718dc2016-07-22 17:01:30 +0000879 CCInfo.AllocateReg(DispatchIDReg);
880 }
881
Matt Arsenault296b8492016-02-12 06:31:30 +0000882 if (Info->hasFlatScratchInit()) {
883 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000884 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault296b8492016-02-12 06:31:30 +0000885 CCInfo.AllocateReg(FlatScratchInitReg);
886 }
887
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000888 if (!AMDGPU::isShader(CallConv))
889 analyzeFormalArgumentsCompute(CCInfo, Ins);
890 else
891 AnalyzeFormalArguments(CCInfo, Splits);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000892
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000893 SmallVector<SDValue, 16> Chains;
894
Christian Konig2c8f6d52013-03-07 09:03:52 +0000895 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
896
Christian Konigb7be72d2013-05-17 09:46:48 +0000897 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000898 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000899 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000900 continue;
901 }
902
Christian Konig2c8f6d52013-03-07 09:03:52 +0000903 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000904 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000905
906 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000907 VT = Ins[i].VT;
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000908 EVT MemVT = VA.getLocVT();
Tom Stellardb5798b02015-06-26 21:15:03 +0000909 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
910 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000911 // The first 36 bytes of the input buffer contains information about
912 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000913 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000914 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000915 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000916
Craig Toppere3dcce92015-08-01 22:20:21 +0000917 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000918 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000919 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Tom Stellardca7ecf32014-08-22 18:49:31 +0000920 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
921 // On SI local pointers are just offsets into LDS, so they are always
922 // less than 16-bits. On CI and newer they could potentially be
923 // real pointers, so we can't guarantee their size.
924 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
925 DAG.getValueType(MVT::i16));
926 }
927
Tom Stellarded882c22013-06-03 17:40:11 +0000928 InVals.push_back(Arg);
Matt Arsenault52ef4012016-07-26 16:45:58 +0000929 Info->setABIArgOffset(Offset + MemVT.getStoreSize());
Tom Stellarded882c22013-06-03 17:40:11 +0000930 continue;
931 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000932 assert(VA.isRegLoc() && "Parameter must be in a register!");
933
934 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000935
936 if (VT == MVT::i64) {
937 // For now assume it is a pointer
938 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000939 &AMDGPU::SGPR_64RegClass);
940 Reg = MF.addLiveIn(Reg, &AMDGPU::SGPR_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000941 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
942 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000943 continue;
944 }
945
946 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
947
948 Reg = MF.addLiveIn(Reg, RC);
949 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
950
Christian Konig2c8f6d52013-03-07 09:03:52 +0000951 if (Arg.VT.isVector()) {
952
953 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000954 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000955 unsigned NumElements = ParamType->getVectorNumElements();
956
957 SmallVector<SDValue, 4> Regs;
958 Regs.push_back(Val);
959 for (unsigned j = 1; j != NumElements; ++j) {
960 Reg = ArgLocs[ArgIdx++].getLocReg();
961 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000962
963 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
964 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000965 }
966
967 // Fill up the missing vector elements
968 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000969 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000970
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000971 InVals.push_back(DAG.getBuildVector(Arg.VT, DL, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000972 continue;
973 }
974
975 InVals.push_back(Val);
976 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000977
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000978 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
979 // these from the dispatch pointer.
980
981 // Start adding system SGPRs.
982 if (Info->hasWorkGroupIDX()) {
983 unsigned Reg = Info->addWorkGroupIDX();
Marek Olsak79c05872016-11-25 17:37:09 +0000984 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000985 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000986 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000987
988 if (Info->hasWorkGroupIDY()) {
989 unsigned Reg = Info->addWorkGroupIDY();
Marek Olsak79c05872016-11-25 17:37:09 +0000990 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000991 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000992 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000993
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000994 if (Info->hasWorkGroupIDZ()) {
995 unsigned Reg = Info->addWorkGroupIDZ();
Marek Olsak79c05872016-11-25 17:37:09 +0000996 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000997 CCInfo.AllocateReg(Reg);
998 }
999
1000 if (Info->hasWorkGroupInfo()) {
1001 unsigned Reg = Info->addWorkGroupInfo();
Marek Olsak79c05872016-11-25 17:37:09 +00001002 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001003 CCInfo.AllocateReg(Reg);
1004 }
1005
1006 if (Info->hasPrivateSegmentWaveByteOffset()) {
1007 // Scratch wave offset passed in system SGPR.
Tom Stellardf110f8f2016-04-14 16:27:03 +00001008 unsigned PrivateSegmentWaveByteOffsetReg;
1009
1010 if (AMDGPU::isShader(CallConv)) {
1011 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
1012 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
1013 } else
1014 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset();
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001015
1016 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
1017 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
1018 }
1019
1020 // Now that we've figured out where the scratch register inputs are, see if
1021 // should reserve the arguments and use them directly.
Matthias Braun941a7052016-07-28 18:40:00 +00001022 bool HasStackObjects = MF.getFrameInfo().hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +00001023 // Record that we know we have non-spill stack objects so we don't need to
1024 // check all stack objects later.
1025 if (HasStackObjects)
1026 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001027
Matt Arsenault253640e2016-10-13 13:10:00 +00001028 // Everything live out of a block is spilled with fast regalloc, so it's
1029 // almost certain that spilling will be required.
1030 if (getTargetMachine().getOptLevel() == CodeGenOpt::None)
1031 HasStackObjects = true;
1032
Tom Stellard0b76fc4c2016-09-16 21:34:26 +00001033 if (ST.isAmdCodeObjectV2()) {
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001034 if (HasStackObjects) {
1035 // If we have stack objects, we unquestionably need the private buffer
Tom Stellard0b76fc4c2016-09-16 21:34:26 +00001036 // resource. For the Code Object V2 ABI, this will be the first 4 user
1037 // SGPR inputs. We can reserve those and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001038
1039 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
1040 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
1041 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
1042
1043 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
1044 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
1045 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
1046 } else {
1047 unsigned ReservedBufferReg
1048 = TRI->reservedPrivateSegmentBufferReg(MF);
1049 unsigned ReservedOffsetReg
1050 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
1051
1052 // We tentatively reserve the last registers (skipping the last two
1053 // which may contain VCC). After register allocation, we'll replace
1054 // these with the ones immediately after those which were really
1055 // allocated. In the prologue copies will be inserted from the argument
1056 // to these reserved registers.
1057 Info->setScratchRSrcReg(ReservedBufferReg);
1058 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
1059 }
1060 } else {
1061 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
1062
1063 // Without HSA, relocations are used for the scratch pointer and the
1064 // buffer resource setup is always inserted in the prologue. Scratch wave
1065 // offset is still in an input SGPR.
1066 Info->setScratchRSrcReg(ReservedBufferReg);
1067
1068 if (HasStackObjects) {
1069 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
1070 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
1071 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
1072 } else {
1073 unsigned ReservedOffsetReg
1074 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
1075 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
1076 }
1077 }
1078
1079 if (Info->hasWorkItemIDX()) {
1080 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
1081 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1082 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +00001083 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001084
1085 if (Info->hasWorkItemIDY()) {
1086 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
1087 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1088 CCInfo.AllocateReg(Reg);
1089 }
1090
1091 if (Info->hasWorkItemIDZ()) {
1092 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
1093 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1094 CCInfo.AllocateReg(Reg);
1095 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +00001096
Matt Arsenaultcf13d182015-07-10 22:51:36 +00001097 if (Chains.empty())
1098 return Chain;
1099
1100 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +00001101}
1102
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001103SDValue
1104SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1105 bool isVarArg,
1106 const SmallVectorImpl<ISD::OutputArg> &Outs,
1107 const SmallVectorImpl<SDValue> &OutVals,
1108 const SDLoc &DL, SelectionDAG &DAG) const {
Marek Olsak8a0f3352016-01-13 17:23:04 +00001109 MachineFunction &MF = DAG.getMachineFunction();
1110 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1111
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001112 if (!AMDGPU::isShader(CallConv))
Marek Olsak8a0f3352016-01-13 17:23:04 +00001113 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
1114 OutVals, DL, DAG);
1115
Marek Olsak8e9cc632016-01-13 17:23:09 +00001116 Info->setIfReturnsVoid(Outs.size() == 0);
1117
Marek Olsak8a0f3352016-01-13 17:23:04 +00001118 SmallVector<ISD::OutputArg, 48> Splits;
1119 SmallVector<SDValue, 48> SplitVals;
1120
1121 // Split vectors into their elements.
1122 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1123 const ISD::OutputArg &Out = Outs[i];
1124
1125 if (Out.VT.isVector()) {
1126 MVT VT = Out.VT.getVectorElementType();
1127 ISD::OutputArg NewOut = Out;
1128 NewOut.Flags.setSplit();
1129 NewOut.VT = VT;
1130
1131 // We want the original number of vector elements here, e.g.
1132 // three or five, not four or eight.
1133 unsigned NumElements = Out.ArgVT.getVectorNumElements();
1134
1135 for (unsigned j = 0; j != NumElements; ++j) {
1136 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
1137 DAG.getConstant(j, DL, MVT::i32));
1138 SplitVals.push_back(Elem);
1139 Splits.push_back(NewOut);
1140 NewOut.PartOffset += NewOut.VT.getStoreSize();
1141 }
1142 } else {
1143 SplitVals.push_back(OutVals[i]);
1144 Splits.push_back(Out);
1145 }
1146 }
1147
1148 // CCValAssign - represent the assignment of the return value to a location.
1149 SmallVector<CCValAssign, 48> RVLocs;
1150
1151 // CCState - Info about the registers and stack slots.
1152 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1153 *DAG.getContext());
1154
1155 // Analyze outgoing return values.
1156 AnalyzeReturn(CCInfo, Splits);
1157
1158 SDValue Flag;
1159 SmallVector<SDValue, 48> RetOps;
1160 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1161
1162 // Copy the result values into the output registers.
1163 for (unsigned i = 0, realRVLocIdx = 0;
1164 i != RVLocs.size();
1165 ++i, ++realRVLocIdx) {
1166 CCValAssign &VA = RVLocs[i];
1167 assert(VA.isRegLoc() && "Can only return in registers!");
1168
1169 SDValue Arg = SplitVals[realRVLocIdx];
1170
1171 // Copied from other backends.
1172 switch (VA.getLocInfo()) {
1173 default: llvm_unreachable("Unknown loc info!");
1174 case CCValAssign::Full:
1175 break;
1176 case CCValAssign::BCvt:
1177 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1178 break;
1179 }
1180
1181 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
1182 Flag = Chain.getValue(1);
1183 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1184 }
1185
1186 // Update chain and glue.
1187 RetOps[0] = Chain;
1188 if (Flag.getNode())
1189 RetOps.push_back(Flag);
1190
Matt Arsenault9babdf42016-06-22 20:15:28 +00001191 unsigned Opc = Info->returnsVoid() ? AMDGPUISD::ENDPGM : AMDGPUISD::RETURN;
1192 return DAG.getNode(Opc, DL, MVT::Other, RetOps);
Marek Olsak8a0f3352016-01-13 17:23:04 +00001193}
1194
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001195unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1196 SelectionDAG &DAG) const {
1197 unsigned Reg = StringSwitch<unsigned>(RegName)
1198 .Case("m0", AMDGPU::M0)
1199 .Case("exec", AMDGPU::EXEC)
1200 .Case("exec_lo", AMDGPU::EXEC_LO)
1201 .Case("exec_hi", AMDGPU::EXEC_HI)
1202 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1203 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1204 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1205 .Default(AMDGPU::NoRegister);
1206
1207 if (Reg == AMDGPU::NoRegister) {
1208 report_fatal_error(Twine("invalid register name \""
1209 + StringRef(RegName) + "\"."));
1210
1211 }
1212
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001213 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001214 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1215 report_fatal_error(Twine("invalid register \""
1216 + StringRef(RegName) + "\" for subtarget."));
1217 }
1218
1219 switch (Reg) {
1220 case AMDGPU::M0:
1221 case AMDGPU::EXEC_LO:
1222 case AMDGPU::EXEC_HI:
1223 case AMDGPU::FLAT_SCR_LO:
1224 case AMDGPU::FLAT_SCR_HI:
1225 if (VT.getSizeInBits() == 32)
1226 return Reg;
1227 break;
1228 case AMDGPU::EXEC:
1229 case AMDGPU::FLAT_SCR:
1230 if (VT.getSizeInBits() == 64)
1231 return Reg;
1232 break;
1233 default:
1234 llvm_unreachable("missing register type checking");
1235 }
1236
1237 report_fatal_error(Twine("invalid type for register \""
1238 + StringRef(RegName) + "\"."));
1239}
1240
Matt Arsenault786724a2016-07-12 21:41:32 +00001241// If kill is not the last instruction, split the block so kill is always a
1242// proper terminator.
1243MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI,
1244 MachineBasicBlock *BB) const {
1245 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1246
1247 MachineBasicBlock::iterator SplitPoint(&MI);
1248 ++SplitPoint;
1249
1250 if (SplitPoint == BB->end()) {
1251 // Don't bother with a new block.
1252 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1253 return BB;
1254 }
1255
1256 MachineFunction *MF = BB->getParent();
1257 MachineBasicBlock *SplitBB
1258 = MF->CreateMachineBasicBlock(BB->getBasicBlock());
1259
Matt Arsenault786724a2016-07-12 21:41:32 +00001260 MF->insert(++MachineFunction::iterator(BB), SplitBB);
1261 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end());
1262
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001263 SplitBB->transferSuccessorsAndUpdatePHIs(BB);
Matt Arsenault786724a2016-07-12 21:41:32 +00001264 BB->addSuccessor(SplitBB);
1265
1266 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1267 return SplitBB;
1268}
1269
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001270// Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the
1271// wavefront. If the value is uniform and just happens to be in a VGPR, this
1272// will only do one iteration. In the worst case, this will loop 64 times.
1273//
1274// TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001275static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop(
1276 const SIInstrInfo *TII,
1277 MachineRegisterInfo &MRI,
1278 MachineBasicBlock &OrigBB,
1279 MachineBasicBlock &LoopBB,
1280 const DebugLoc &DL,
1281 const MachineOperand &IdxReg,
1282 unsigned InitReg,
1283 unsigned ResultReg,
1284 unsigned PhiReg,
1285 unsigned InitSaveExecReg,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001286 int Offset,
1287 bool UseGPRIdxMode) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001288 MachineBasicBlock::iterator I = LoopBB.begin();
1289
1290 unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1291 unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1292 unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1293 unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1294
1295 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg)
1296 .addReg(InitReg)
1297 .addMBB(&OrigBB)
1298 .addReg(ResultReg)
1299 .addMBB(&LoopBB);
1300
1301 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec)
1302 .addReg(InitSaveExecReg)
1303 .addMBB(&OrigBB)
1304 .addReg(NewExec)
1305 .addMBB(&LoopBB);
1306
1307 // Read the next variant <- also loop target.
1308 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg)
1309 .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef()));
1310
1311 // Compare the just read M0 value to all possible Idx values.
1312 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg)
1313 .addReg(CurrentIdxReg)
Matt Arsenaultf0ba86a2016-07-21 09:40:57 +00001314 .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg());
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001315
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001316 if (UseGPRIdxMode) {
1317 unsigned IdxReg;
1318 if (Offset == 0) {
1319 IdxReg = CurrentIdxReg;
1320 } else {
1321 IdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1322 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), IdxReg)
1323 .addReg(CurrentIdxReg, RegState::Kill)
1324 .addImm(Offset);
1325 }
1326
1327 MachineInstr *SetIdx =
1328 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_IDX))
1329 .addReg(IdxReg, RegState::Kill);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001330 SetIdx->getOperand(2).setIsUndef();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001331 } else {
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001332 // Move index from VCC into M0
1333 if (Offset == 0) {
1334 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1335 .addReg(CurrentIdxReg, RegState::Kill);
1336 } else {
1337 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1338 .addReg(CurrentIdxReg, RegState::Kill)
1339 .addImm(Offset);
1340 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001341 }
1342
1343 // Update EXEC, save the original EXEC value to VCC.
1344 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec)
1345 .addReg(CondReg, RegState::Kill);
1346
1347 MRI.setSimpleHint(NewExec, CondReg);
1348
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001349 // Update EXEC, switch all done bits to 0 and all todo bits to 1.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001350 MachineInstr *InsertPt =
1351 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001352 .addReg(AMDGPU::EXEC)
1353 .addReg(NewExec);
1354
1355 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use
1356 // s_cbranch_scc0?
1357
1358 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover.
1359 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
1360 .addMBB(&LoopBB);
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001361
1362 return InsertPt->getIterator();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001363}
1364
1365// This has slightly sub-optimal regalloc when the source vector is killed by
1366// the read. The register allocator does not understand that the kill is
1367// per-workitem, so is kept alive for the whole loop so we end up not re-using a
1368// subregister from it, using 1 more VGPR than necessary. This was saved when
1369// this was expanded after register allocation.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001370static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII,
1371 MachineBasicBlock &MBB,
1372 MachineInstr &MI,
1373 unsigned InitResultReg,
1374 unsigned PhiReg,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001375 int Offset,
1376 bool UseGPRIdxMode) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001377 MachineFunction *MF = MBB.getParent();
1378 MachineRegisterInfo &MRI = MF->getRegInfo();
1379 const DebugLoc &DL = MI.getDebugLoc();
1380 MachineBasicBlock::iterator I(&MI);
1381
1382 unsigned DstReg = MI.getOperand(0).getReg();
1383 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1384 unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1385
1386 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec);
1387
1388 // Save the EXEC mask
1389 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec)
1390 .addReg(AMDGPU::EXEC);
1391
1392 // To insert the loop we need to split the block. Move everything after this
1393 // point to a new block, and insert a new empty block between the two.
1394 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock();
1395 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
1396 MachineFunction::iterator MBBI(MBB);
1397 ++MBBI;
1398
1399 MF->insert(MBBI, LoopBB);
1400 MF->insert(MBBI, RemainderBB);
1401
1402 LoopBB->addSuccessor(LoopBB);
1403 LoopBB->addSuccessor(RemainderBB);
1404
1405 // Move the rest of the block into a new block.
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001406 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001407 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
1408
1409 MBB.addSuccessor(LoopBB);
1410
1411 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1412
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001413 auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx,
1414 InitResultReg, DstReg, PhiReg, TmpExec,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001415 Offset, UseGPRIdxMode);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001416
1417 MachineBasicBlock::iterator First = RemainderBB->begin();
1418 BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
1419 .addReg(SaveExec);
1420
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001421 return InsPt;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001422}
1423
1424// Returns subreg index, offset
1425static std::pair<unsigned, int>
1426computeIndirectRegAndOffset(const SIRegisterInfo &TRI,
1427 const TargetRegisterClass *SuperRC,
1428 unsigned VecReg,
1429 int Offset) {
1430 int NumElts = SuperRC->getSize() / 4;
1431
1432 // Skip out of bounds offsets, or else we would end up using an undefined
1433 // register.
1434 if (Offset >= NumElts || Offset < 0)
1435 return std::make_pair(AMDGPU::sub0, Offset);
1436
1437 return std::make_pair(AMDGPU::sub0 + Offset, 0);
1438}
1439
1440// Return true if the index is an SGPR and was set.
1441static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII,
1442 MachineRegisterInfo &MRI,
1443 MachineInstr &MI,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001444 int Offset,
1445 bool UseGPRIdxMode,
1446 bool IsIndirectSrc) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001447 MachineBasicBlock *MBB = MI.getParent();
1448 const DebugLoc &DL = MI.getDebugLoc();
1449 MachineBasicBlock::iterator I(&MI);
1450
1451 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1452 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
1453
1454 assert(Idx->getReg() != AMDGPU::NoRegister);
1455
1456 if (!TII->getRegisterInfo().isSGPRClass(IdxRC))
1457 return false;
1458
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001459 if (UseGPRIdxMode) {
1460 unsigned IdxMode = IsIndirectSrc ?
1461 VGPRIndexMode::SRC0_ENABLE : VGPRIndexMode::DST_ENABLE;
1462 if (Offset == 0) {
1463 MachineInstr *SetOn =
1464 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1465 .addOperand(*Idx)
1466 .addImm(IdxMode);
1467
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001468 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001469 } else {
1470 unsigned Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
1471 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp)
1472 .addOperand(*Idx)
1473 .addImm(Offset);
1474 MachineInstr *SetOn =
1475 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1476 .addReg(Tmp, RegState::Kill)
1477 .addImm(IdxMode);
1478
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001479 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001480 }
1481
1482 return true;
1483 }
1484
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001485 if (Offset == 0) {
1486 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1487 .addOperand(*Idx);
1488 } else {
1489 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1490 .addOperand(*Idx)
1491 .addImm(Offset);
1492 }
1493
1494 return true;
1495}
1496
1497// Control flow needs to be inserted if indexing with a VGPR.
1498static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI,
1499 MachineBasicBlock &MBB,
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001500 const SISubtarget &ST) {
1501 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001502 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1503 MachineFunction *MF = MBB.getParent();
1504 MachineRegisterInfo &MRI = MF->getRegInfo();
1505
1506 unsigned Dst = MI.getOperand(0).getReg();
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001507 unsigned SrcReg = TII->getNamedOperand(MI, AMDGPU::OpName::src)->getReg();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001508 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1509
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001510 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcReg);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001511
1512 unsigned SubReg;
1513 std::tie(SubReg, Offset)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001514 = computeIndirectRegAndOffset(TRI, VecRC, SrcReg, Offset);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001515
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001516 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode;
1517
1518 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, true)) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001519 MachineBasicBlock::iterator I(&MI);
1520 const DebugLoc &DL = MI.getDebugLoc();
1521
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001522 if (UseGPRIdxMode) {
1523 // TODO: Look at the uses to avoid the copy. This may require rescheduling
1524 // to avoid interfering with other uses, so probably requires a new
1525 // optimization pass.
1526 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001527 .addReg(SrcReg, RegState::Undef, SubReg)
1528 .addReg(SrcReg, RegState::Implicit)
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001529 .addReg(AMDGPU::M0, RegState::Implicit);
1530 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1531 } else {
1532 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001533 .addReg(SrcReg, RegState::Undef, SubReg)
1534 .addReg(SrcReg, RegState::Implicit);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001535 }
1536
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001537 MI.eraseFromParent();
1538
1539 return &MBB;
1540 }
1541
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001542
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001543 const DebugLoc &DL = MI.getDebugLoc();
1544 MachineBasicBlock::iterator I(&MI);
1545
1546 unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1547 unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1548
1549 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg);
1550
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001551 if (UseGPRIdxMode) {
1552 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1553 .addImm(0) // Reset inside loop.
1554 .addImm(VGPRIndexMode::SRC0_ENABLE);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001555 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001556
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001557 // Disable again after the loop.
1558 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1559 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001560
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001561 auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset, UseGPRIdxMode);
1562 MachineBasicBlock *LoopBB = InsPt->getParent();
1563
1564 if (UseGPRIdxMode) {
1565 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001566 .addReg(SrcReg, RegState::Undef, SubReg)
1567 .addReg(SrcReg, RegState::Implicit)
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001568 .addReg(AMDGPU::M0, RegState::Implicit);
1569 } else {
1570 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001571 .addReg(SrcReg, RegState::Undef, SubReg)
1572 .addReg(SrcReg, RegState::Implicit);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001573 }
1574
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001575 MI.eraseFromParent();
1576
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001577 return LoopBB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001578}
1579
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001580static unsigned getMOVRELDPseudo(const TargetRegisterClass *VecRC) {
1581 switch (VecRC->getSize()) {
1582 case 4:
1583 return AMDGPU::V_MOVRELD_B32_V1;
1584 case 8:
1585 return AMDGPU::V_MOVRELD_B32_V2;
1586 case 16:
1587 return AMDGPU::V_MOVRELD_B32_V4;
1588 case 32:
1589 return AMDGPU::V_MOVRELD_B32_V8;
1590 case 64:
1591 return AMDGPU::V_MOVRELD_B32_V16;
1592 default:
1593 llvm_unreachable("unsupported size for MOVRELD pseudos");
1594 }
1595}
1596
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001597static MachineBasicBlock *emitIndirectDst(MachineInstr &MI,
1598 MachineBasicBlock &MBB,
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001599 const SISubtarget &ST) {
1600 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001601 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1602 MachineFunction *MF = MBB.getParent();
1603 MachineRegisterInfo &MRI = MF->getRegInfo();
1604
1605 unsigned Dst = MI.getOperand(0).getReg();
1606 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
1607 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1608 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val);
1609 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1610 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
1611
1612 // This can be an immediate, but will be folded later.
1613 assert(Val->getReg());
1614
1615 unsigned SubReg;
1616 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC,
1617 SrcVec->getReg(),
1618 Offset);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001619 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode;
1620
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001621 if (Idx->getReg() == AMDGPU::NoRegister) {
1622 MachineBasicBlock::iterator I(&MI);
1623 const DebugLoc &DL = MI.getDebugLoc();
1624
1625 assert(Offset == 0);
1626
1627 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst)
1628 .addOperand(*SrcVec)
1629 .addOperand(*Val)
1630 .addImm(SubReg);
1631
1632 MI.eraseFromParent();
1633 return &MBB;
1634 }
1635
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001636 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, false)) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001637 MachineBasicBlock::iterator I(&MI);
1638 const DebugLoc &DL = MI.getDebugLoc();
1639
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001640 if (UseGPRIdxMode) {
1641 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_indirect))
1642 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst
1643 .addOperand(*Val)
1644 .addReg(Dst, RegState::ImplicitDefine)
1645 .addReg(SrcVec->getReg(), RegState::Implicit)
1646 .addReg(AMDGPU::M0, RegState::Implicit);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001647
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001648 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1649 } else {
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001650 const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(VecRC));
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001651
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001652 BuildMI(MBB, I, DL, MovRelDesc)
1653 .addReg(Dst, RegState::Define)
1654 .addReg(SrcVec->getReg())
1655 .addOperand(*Val)
1656 .addImm(SubReg - AMDGPU::sub0);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001657 }
1658
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001659 MI.eraseFromParent();
1660 return &MBB;
1661 }
1662
1663 if (Val->isReg())
1664 MRI.clearKillFlags(Val->getReg());
1665
1666 const DebugLoc &DL = MI.getDebugLoc();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001667
1668 if (UseGPRIdxMode) {
1669 MachineBasicBlock::iterator I(&MI);
1670
1671 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1672 .addImm(0) // Reset inside loop.
1673 .addImm(VGPRIndexMode::DST_ENABLE);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001674 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001675
1676 // Disable again after the loop.
1677 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1678 }
1679
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001680 unsigned PhiReg = MRI.createVirtualRegister(VecRC);
1681
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001682 auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg,
1683 Offset, UseGPRIdxMode);
1684 MachineBasicBlock *LoopBB = InsPt->getParent();
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001685
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001686 if (UseGPRIdxMode) {
1687 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_indirect))
1688 .addReg(PhiReg, RegState::Undef, SubReg) // vdst
1689 .addOperand(*Val) // src0
1690 .addReg(Dst, RegState::ImplicitDefine)
1691 .addReg(PhiReg, RegState::Implicit)
1692 .addReg(AMDGPU::M0, RegState::Implicit);
1693 } else {
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001694 const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(VecRC));
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001695
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001696 BuildMI(*LoopBB, InsPt, DL, MovRelDesc)
1697 .addReg(Dst, RegState::Define)
1698 .addReg(PhiReg)
1699 .addOperand(*Val)
1700 .addImm(SubReg - AMDGPU::sub0);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001701 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001702
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001703 MI.eraseFromParent();
1704
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001705 return LoopBB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001706}
1707
Matt Arsenault786724a2016-07-12 21:41:32 +00001708MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
1709 MachineInstr &MI, MachineBasicBlock *BB) const {
Tom Stellard244891d2016-12-20 15:52:17 +00001710
1711 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1712 MachineFunction *MF = BB->getParent();
1713 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1714
1715 if (TII->isMIMG(MI)) {
1716 if (!MI.memoperands_empty())
1717 return BB;
1718 // Add a memoperand for mimg instructions so that they aren't assumed to
1719 // be ordered memory instuctions.
1720
1721 MachinePointerInfo PtrInfo(MFI->getImagePSV());
1722 MachineMemOperand::Flags Flags = MachineMemOperand::MODereferenceable;
1723 if (MI.mayStore())
1724 Flags |= MachineMemOperand::MOStore;
1725
1726 if (MI.mayLoad())
1727 Flags |= MachineMemOperand::MOLoad;
1728
1729 auto MMO = MF->getMachineMemOperand(PtrInfo, Flags, 0, 0);
1730 MI.addMemOperand(*MF, MMO);
1731 return BB;
1732 }
1733
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001734 switch (MI.getOpcode()) {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001735 case AMDGPU::SI_INIT_M0: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001736 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001737 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001738 .addOperand(MI.getOperand(0));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001739 MI.eraseFromParent();
Matt Arsenault20711b72015-02-20 22:10:45 +00001740 return BB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001741 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001742 case AMDGPU::GET_GROUPSTATICSIZE: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001743 DebugLoc DL = MI.getDebugLoc();
Matt Arsenault3c07c812016-07-22 17:01:33 +00001744 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32))
1745 .addOperand(MI.getOperand(0))
Matt Arsenault52ef4012016-07-26 16:45:58 +00001746 .addImm(MFI->getLDSSize());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001747 MI.eraseFromParent();
Changpeng Fang01f60622016-03-15 17:28:44 +00001748 return BB;
1749 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001750 case AMDGPU::SI_INDIRECT_SRC_V1:
1751 case AMDGPU::SI_INDIRECT_SRC_V2:
1752 case AMDGPU::SI_INDIRECT_SRC_V4:
1753 case AMDGPU::SI_INDIRECT_SRC_V8:
1754 case AMDGPU::SI_INDIRECT_SRC_V16:
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001755 return emitIndirectSrc(MI, *BB, *getSubtarget());
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001756 case AMDGPU::SI_INDIRECT_DST_V1:
1757 case AMDGPU::SI_INDIRECT_DST_V2:
1758 case AMDGPU::SI_INDIRECT_DST_V4:
1759 case AMDGPU::SI_INDIRECT_DST_V8:
1760 case AMDGPU::SI_INDIRECT_DST_V16:
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001761 return emitIndirectDst(MI, *BB, *getSubtarget());
Matt Arsenault786724a2016-07-12 21:41:32 +00001762 case AMDGPU::SI_KILL:
1763 return splitKillBlock(MI, BB);
Matt Arsenault22e41792016-08-27 01:00:37 +00001764 case AMDGPU::V_CNDMASK_B64_PSEUDO: {
1765 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Matt Arsenault22e41792016-08-27 01:00:37 +00001766
1767 unsigned Dst = MI.getOperand(0).getReg();
1768 unsigned Src0 = MI.getOperand(1).getReg();
1769 unsigned Src1 = MI.getOperand(2).getReg();
1770 const DebugLoc &DL = MI.getDebugLoc();
1771 unsigned SrcCond = MI.getOperand(3).getReg();
1772
1773 unsigned DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1774 unsigned DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1775
1776 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo)
1777 .addReg(Src0, 0, AMDGPU::sub0)
1778 .addReg(Src1, 0, AMDGPU::sub0)
1779 .addReg(SrcCond);
1780 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi)
1781 .addReg(Src0, 0, AMDGPU::sub1)
1782 .addReg(Src1, 0, AMDGPU::sub1)
1783 .addReg(SrcCond);
1784
1785 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst)
1786 .addReg(DstLo)
1787 .addImm(AMDGPU::sub0)
1788 .addReg(DstHi)
1789 .addImm(AMDGPU::sub1);
1790 MI.eraseFromParent();
1791 return BB;
1792 }
Matt Arsenault327188a2016-12-15 21:57:11 +00001793 case AMDGPU::SI_BR_UNDEF: {
1794 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1795 const DebugLoc &DL = MI.getDebugLoc();
1796 MachineInstr *Br = BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CBRANCH_SCC1))
1797 .addOperand(MI.getOperand(0));
1798 Br->getOperand(1).setIsUndef(true); // read undef SCC
1799 MI.eraseFromParent();
1800 return BB;
1801 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001802 default:
1803 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001804 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001805}
1806
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001807bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1808 // This currently forces unfolding various combinations of fsub into fma with
1809 // free fneg'd operands. As long as we have fast FMA (controlled by
1810 // isFMAFasterThanFMulAndFAdd), we should perform these.
1811
1812 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1813 // most of these combines appear to be cycle neutral but save on instruction
1814 // count / code size.
1815 return true;
1816}
1817
Mehdi Amini44ede332015-07-09 02:09:04 +00001818EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1819 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001820 if (!VT.isVector()) {
1821 return MVT::i1;
1822 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001823 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001824}
1825
Matt Arsenault94163282016-12-22 16:36:25 +00001826MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT VT) const {
1827 // TODO: Should i16 be used always if legal? For now it would force VALU
1828 // shifts.
1829 return (VT == MVT::i16) ? MVT::i16 : MVT::i32;
Christian Konig082a14a2013-03-18 11:34:05 +00001830}
1831
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001832// Answering this is somewhat tricky and depends on the specific device which
1833// have different rates for fma or all f64 operations.
1834//
1835// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1836// regardless of which device (although the number of cycles differs between
1837// devices), so it is always profitable for f64.
1838//
1839// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1840// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1841// which we can always do even without fused FP ops since it returns the same
1842// result as the separate operations and since it is always full
1843// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1844// however does not support denormals, so we do report fma as faster if we have
1845// a fast fma device and require denormals.
1846//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001847bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1848 VT = VT.getScalarType();
1849
1850 if (!VT.isSimple())
1851 return false;
1852
1853 switch (VT.getSimpleVT().SimpleTy) {
1854 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001855 // This is as fast on some subtargets. However, we always have full rate f32
1856 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001857 // which we should prefer over fma. We can't use this if we want to support
1858 // denormals, so only report this in these cases.
1859 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001860 case MVT::f64:
1861 return true;
Matt Arsenault9e22bc22016-12-22 03:21:48 +00001862 case MVT::f16:
1863 return Subtarget->has16BitInsts() && Subtarget->hasFP16Denormals();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001864 default:
1865 break;
1866 }
1867
1868 return false;
1869}
1870
Tom Stellard75aadc22012-12-11 21:25:42 +00001871//===----------------------------------------------------------------------===//
1872// Custom DAG Lowering Operations
1873//===----------------------------------------------------------------------===//
1874
1875SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1876 switch (Op.getOpcode()) {
1877 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001878 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001879 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001880 SDValue Result = LowerLOAD(Op, DAG);
1881 assert((!Result.getNode() ||
1882 Result.getNode()->getNumValues() == 2) &&
1883 "Load should return a value and a chain");
1884 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001885 }
Tom Stellardaf775432013-10-23 00:44:32 +00001886
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001887 case ISD::FSIN:
1888 case ISD::FCOS:
1889 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001890 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001891 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001892 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001893 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001894 case ISD::GlobalAddress: {
1895 MachineFunction &MF = DAG.getMachineFunction();
1896 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1897 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001898 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001899 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001900 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001901 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Matt Arsenault99c14522016-04-25 19:27:24 +00001902 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG);
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001903 case ISD::TRAP: return lowerTRAP(Op, DAG);
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00001904 case ISD::FP_ROUND:
1905 return lowerFP_ROUND(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001906 }
1907 return SDValue();
1908}
1909
Tom Stellardf8794352012-12-19 22:10:31 +00001910/// \brief Helper function for LowerBRCOND
1911static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001912
Tom Stellardf8794352012-12-19 22:10:31 +00001913 SDNode *Parent = Value.getNode();
1914 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1915 I != E; ++I) {
1916
1917 if (I.getUse().get() != Value)
1918 continue;
1919
1920 if (I->getOpcode() == Opcode)
1921 return *I;
1922 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001923 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001924}
1925
Tom Stellardbc4497b2016-02-12 23:45:29 +00001926bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault6408c912016-09-16 22:11:18 +00001927 if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
1928 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1929 case AMDGPUIntrinsic::amdgcn_if:
1930 case AMDGPUIntrinsic::amdgcn_else:
1931 case AMDGPUIntrinsic::amdgcn_end_cf:
1932 case AMDGPUIntrinsic::amdgcn_loop:
1933 return true;
1934 default:
1935 return false;
1936 }
Tom Stellardbc4497b2016-02-12 23:45:29 +00001937 }
Matt Arsenault6408c912016-09-16 22:11:18 +00001938
1939 if (Intr->getOpcode() == ISD::INTRINSIC_WO_CHAIN) {
1940 switch (cast<ConstantSDNode>(Intr->getOperand(0))->getZExtValue()) {
1941 case AMDGPUIntrinsic::amdgcn_break:
1942 case AMDGPUIntrinsic::amdgcn_if_break:
1943 case AMDGPUIntrinsic::amdgcn_else_break:
1944 return true;
1945 default:
1946 return false;
1947 }
1948 }
1949
1950 return false;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001951}
1952
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001953void SITargetLowering::createDebuggerPrologueStackObjects(
1954 MachineFunction &MF) const {
1955 // Create stack objects that are used for emitting debugger prologue.
1956 //
1957 // Debugger prologue writes work group IDs and work item IDs to scratch memory
1958 // at fixed location in the following format:
1959 // offset 0: work group ID x
1960 // offset 4: work group ID y
1961 // offset 8: work group ID z
1962 // offset 16: work item ID x
1963 // offset 20: work item ID y
1964 // offset 24: work item ID z
1965 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1966 int ObjectIdx = 0;
1967
1968 // For each dimension:
1969 for (unsigned i = 0; i < 3; ++i) {
1970 // Create fixed stack object for work group ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001971 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001972 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx);
1973 // Create fixed stack object for work item ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001974 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001975 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx);
1976 }
1977}
1978
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00001979bool SITargetLowering::shouldEmitFixup(const GlobalValue *GV) const {
1980 const Triple &TT = getTargetMachine().getTargetTriple();
1981 return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS &&
1982 AMDGPU::shouldEmitConstantsToTextSection(TT);
1983}
1984
1985bool SITargetLowering::shouldEmitGOTReloc(const GlobalValue *GV) const {
1986 return (GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS ||
1987 GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) &&
1988 !shouldEmitFixup(GV) &&
1989 !getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
1990}
1991
1992bool SITargetLowering::shouldEmitPCReloc(const GlobalValue *GV) const {
1993 return !shouldEmitFixup(GV) && !shouldEmitGOTReloc(GV);
1994}
1995
Tom Stellardf8794352012-12-19 22:10:31 +00001996/// This transforms the control flow intrinsics to get the branch destination as
1997/// last parameter, also switches branch target with BR if the need arise
1998SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1999 SelectionDAG &DAG) const {
2000
Andrew Trickef9de2a2013-05-25 02:42:55 +00002001 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00002002
2003 SDNode *Intr = BRCOND.getOperand(1).getNode();
2004 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00002005 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00002006 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00002007
2008 if (Intr->getOpcode() == ISD::SETCC) {
2009 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00002010 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00002011 Intr = SetCC->getOperand(0).getNode();
2012
2013 } else {
2014 // Get the target from BR if we don't negate the condition
2015 BR = findUser(BRCOND, ISD::BR);
2016 Target = BR->getOperand(1);
2017 }
2018
Matt Arsenault6408c912016-09-16 22:11:18 +00002019 // FIXME: This changes the types of the intrinsics instead of introducing new
2020 // nodes with the correct types.
2021 // e.g. llvm.amdgcn.loop
2022
2023 // eg: i1,ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3
2024 // => t9: ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3, BasicBlock:ch<bb1 0x7fee5286d088>
2025
Nicolai Haehnleffbd56a2016-05-05 17:36:36 +00002026 if (!isCFIntrinsic(Intr)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00002027 // This is a uniform branch so we don't need to legalize.
2028 return BRCOND;
2029 }
2030
Matt Arsenault6408c912016-09-16 22:11:18 +00002031 bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID ||
2032 Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN;
2033
Tom Stellardbc4497b2016-02-12 23:45:29 +00002034 assert(!SetCC ||
2035 (SetCC->getConstantOperandVal(1) == 1 &&
Tom Stellardbc4497b2016-02-12 23:45:29 +00002036 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
2037 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00002038
Tom Stellardf8794352012-12-19 22:10:31 +00002039 // operands of the new intrinsic call
2040 SmallVector<SDValue, 4> Ops;
Matt Arsenault6408c912016-09-16 22:11:18 +00002041 if (HaveChain)
2042 Ops.push_back(BRCOND.getOperand(0));
2043
2044 Ops.append(Intr->op_begin() + (HaveChain ? 1 : 0), Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00002045 Ops.push_back(Target);
2046
Matt Arsenault6408c912016-09-16 22:11:18 +00002047 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
2048
Tom Stellardf8794352012-12-19 22:10:31 +00002049 // build the new intrinsic call
2050 SDNode *Result = DAG.getNode(
2051 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00002052 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00002053
Matt Arsenault6408c912016-09-16 22:11:18 +00002054 if (!HaveChain) {
2055 SDValue Ops[] = {
2056 SDValue(Result, 0),
2057 BRCOND.getOperand(0)
2058 };
2059
2060 Result = DAG.getMergeValues(Ops, DL).getNode();
2061 }
2062
Tom Stellardf8794352012-12-19 22:10:31 +00002063 if (BR) {
2064 // Give the branch instruction our target
2065 SDValue Ops[] = {
2066 BR->getOperand(0),
2067 BRCOND.getOperand(2)
2068 };
Chandler Carruth356665a2014-08-01 22:09:43 +00002069 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
2070 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
2071 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00002072 }
2073
2074 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
2075
2076 // Copy the intrinsic results to registers
2077 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
2078 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
2079 if (!CopyToReg)
2080 continue;
2081
2082 Chain = DAG.getCopyToReg(
2083 Chain, DL,
2084 CopyToReg->getOperand(1),
2085 SDValue(Result, i - 1),
2086 SDValue());
2087
2088 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
2089 }
2090
2091 // Remove the old intrinsic from the chain
2092 DAG.ReplaceAllUsesOfValueWith(
2093 SDValue(Intr, Intr->getNumValues() - 1),
2094 Intr->getOperand(0));
2095
2096 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00002097}
2098
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002099SDValue SITargetLowering::getFPExtOrFPTrunc(SelectionDAG &DAG,
2100 SDValue Op,
2101 const SDLoc &DL,
2102 EVT VT) const {
2103 return Op.getValueType().bitsLE(VT) ?
2104 DAG.getNode(ISD::FP_EXTEND, DL, VT, Op) :
2105 DAG.getNode(ISD::FTRUNC, DL, VT, Op);
2106}
2107
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00002108SDValue SITargetLowering::lowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaultafe614c2016-11-18 18:33:36 +00002109 assert(Op.getValueType() == MVT::f16 &&
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00002110 "Do not know how to custom lower FP_ROUND for non-f16 type");
2111
Matt Arsenaultafe614c2016-11-18 18:33:36 +00002112 SDValue Src = Op.getOperand(0);
2113 EVT SrcVT = Src.getValueType();
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00002114 if (SrcVT != MVT::f64)
2115 return Op;
2116
2117 SDLoc DL(Op);
Matt Arsenaultafe614c2016-11-18 18:33:36 +00002118
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00002119 SDValue FpToFp16 = DAG.getNode(ISD::FP_TO_FP16, DL, MVT::i32, Src);
2120 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToFp16);
2121 return DAG.getNode(ISD::BITCAST, DL, MVT::f16, Trunc);;
2122}
2123
Matt Arsenault99c14522016-04-25 19:27:24 +00002124SDValue SITargetLowering::getSegmentAperture(unsigned AS,
2125 SelectionDAG &DAG) const {
2126 SDLoc SL;
2127 MachineFunction &MF = DAG.getMachineFunction();
2128 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00002129 unsigned UserSGPR = Info->getQueuePtrUserSGPR();
2130 assert(UserSGPR != AMDGPU::NoRegister);
2131
Matt Arsenault99c14522016-04-25 19:27:24 +00002132 SDValue QueuePtr = CreateLiveInRegister(
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00002133 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
Matt Arsenault99c14522016-04-25 19:27:24 +00002134
2135 // Offset into amd_queue_t for group_segment_aperture_base_hi /
2136 // private_segment_aperture_base_hi.
2137 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
2138
2139 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr,
2140 DAG.getConstant(StructOffset, SL, MVT::i64));
2141
2142 // TODO: Use custom target PseudoSourceValue.
2143 // TODO: We should use the value from the IR intrinsic call, but it might not
2144 // be available and how do we get it?
2145 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()),
2146 AMDGPUAS::CONSTANT_ADDRESS));
2147
2148 MachinePointerInfo PtrInfo(V, StructOffset);
Justin Lebar9c375812016-07-15 18:27:10 +00002149 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo,
2150 MinAlign(64, StructOffset),
Justin Lebaradbf09e2016-09-11 01:38:58 +00002151 MachineMemOperand::MODereferenceable |
2152 MachineMemOperand::MOInvariant);
Matt Arsenault99c14522016-04-25 19:27:24 +00002153}
2154
2155SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
2156 SelectionDAG &DAG) const {
2157 SDLoc SL(Op);
2158 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op);
2159
2160 SDValue Src = ASC->getOperand(0);
2161
2162 // FIXME: Really support non-0 null pointers.
2163 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32);
2164 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
2165
2166 // flat -> local/private
2167 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
2168 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
2169 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
2170 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
2171 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
2172
2173 return DAG.getNode(ISD::SELECT, SL, MVT::i32,
2174 NonNull, Ptr, SegmentNullPtr);
2175 }
2176 }
2177
2178 // local/private -> flat
2179 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
2180 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
2181 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
2182 SDValue NonNull
2183 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
2184
2185 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG);
2186 SDValue CvtPtr
2187 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
2188
2189 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull,
2190 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr),
2191 FlatNullPtr);
2192 }
2193 }
2194
2195 // global <-> flat are no-ops and never emitted.
2196
2197 const MachineFunction &MF = DAG.getMachineFunction();
2198 DiagnosticInfoUnsupported InvalidAddrSpaceCast(
2199 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc());
2200 DAG.getContext()->diagnose(InvalidAddrSpaceCast);
2201
2202 return DAG.getUNDEF(ASC->getValueType(0));
2203}
2204
Tom Stellard418beb72016-07-13 14:23:33 +00002205bool
2206SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2207 // We can fold offsets for anything that doesn't require a GOT relocation.
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00002208 return (GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS ||
2209 GA->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) &&
2210 !shouldEmitGOTReloc(GA->getGlobal());
Tom Stellard418beb72016-07-13 14:23:33 +00002211}
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002212
Tom Stellard418beb72016-07-13 14:23:33 +00002213static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV,
2214 SDLoc DL, unsigned Offset, EVT PtrVT,
2215 unsigned GAFlags = SIInstrInfo::MO_NONE) {
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002216 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
2217 // lowered to the following code sequence:
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002218 //
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002219 // For constant address space:
2220 // s_getpc_b64 s[0:1]
2221 // s_add_u32 s0, s0, $symbol
2222 // s_addc_u32 s1, s1, 0
2223 //
2224 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
2225 // a fixup or relocation is emitted to replace $symbol with a literal
2226 // constant, which is a pc-relative offset from the encoding of the $symbol
2227 // operand to the global variable.
2228 //
2229 // For global address space:
2230 // s_getpc_b64 s[0:1]
2231 // s_add_u32 s0, s0, $symbol@{gotpc}rel32@lo
2232 // s_addc_u32 s1, s1, $symbol@{gotpc}rel32@hi
2233 //
2234 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
2235 // fixups or relocations are emitted to replace $symbol@*@lo and
2236 // $symbol@*@hi with lower 32 bits and higher 32 bits of a literal constant,
2237 // which is a 64-bit pc-relative offset from the encoding of the $symbol
2238 // operand to the global variable.
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002239 //
2240 // What we want here is an offset from the value returned by s_getpc
2241 // (which is the address of the s_add_u32 instruction) to the global
2242 // variable, but since the encoding of $symbol starts 4 bytes after the start
2243 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too
2244 // small. This requires us to add 4 to the global variable offset in order to
2245 // compute the correct address.
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002246 SDValue PtrLo = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
2247 GAFlags);
2248 SDValue PtrHi = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
2249 GAFlags == SIInstrInfo::MO_NONE ?
2250 GAFlags : GAFlags + 1);
2251 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, PtrLo, PtrHi);
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002252}
2253
Tom Stellard418beb72016-07-13 14:23:33 +00002254SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
2255 SDValue Op,
2256 SelectionDAG &DAG) const {
2257 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
2258
2259 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS &&
2260 GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS)
2261 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
2262
2263 SDLoc DL(GSD);
2264 const GlobalValue *GV = GSD->getGlobal();
2265 EVT PtrVT = Op.getValueType();
2266
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00002267 if (shouldEmitFixup(GV))
Tom Stellard418beb72016-07-13 14:23:33 +00002268 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00002269 else if (shouldEmitPCReloc(GV))
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002270 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT,
2271 SIInstrInfo::MO_REL32);
Tom Stellard418beb72016-07-13 14:23:33 +00002272
2273 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002274 SIInstrInfo::MO_GOTPCREL32);
Tom Stellard418beb72016-07-13 14:23:33 +00002275
2276 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext());
2277 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
2278 const DataLayout &DataLayout = DAG.getDataLayout();
2279 unsigned Align = DataLayout.getABITypeAlignment(PtrTy);
2280 // FIXME: Use a PseudoSourceValue once those can be assigned an address space.
2281 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
2282
Justin Lebar9c375812016-07-15 18:27:10 +00002283 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align,
Justin Lebaradbf09e2016-09-11 01:38:58 +00002284 MachineMemOperand::MODereferenceable |
2285 MachineMemOperand::MOInvariant);
Tom Stellard418beb72016-07-13 14:23:33 +00002286}
2287
Matt Arsenault0bb294b2016-06-17 22:27:03 +00002288SDValue SITargetLowering::lowerTRAP(SDValue Op,
2289 SelectionDAG &DAG) const {
2290 const MachineFunction &MF = DAG.getMachineFunction();
2291 DiagnosticInfoUnsupported NoTrap(*MF.getFunction(),
2292 "trap handler not supported",
2293 Op.getDebugLoc(),
2294 DS_Warning);
2295 DAG.getContext()->diagnose(NoTrap);
2296
2297 // Emit s_endpgm.
2298
2299 // FIXME: This should really be selected to s_trap, but that requires
2300 // setting up the trap handler for it o do anything.
Matt Arsenault9babdf42016-06-22 20:15:28 +00002301 return DAG.getNode(AMDGPUISD::ENDPGM, SDLoc(Op), MVT::Other,
2302 Op.getOperand(0));
Matt Arsenault0bb294b2016-06-17 22:27:03 +00002303}
2304
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002305SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain,
2306 const SDLoc &DL, SDValue V) const {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00002307 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
2308 // the destination register.
2309 //
Tom Stellardfc92e772015-05-12 14:18:14 +00002310 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
2311 // so we will end up with redundant moves to m0.
2312 //
Matt Arsenault4ac341c2016-04-14 21:58:15 +00002313 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
2314
2315 // A Null SDValue creates a glue result.
2316 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
2317 V, Chain);
2318 return SDValue(M0, 0);
Tom Stellardfc92e772015-05-12 14:18:14 +00002319}
2320
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002321SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
2322 SDValue Op,
2323 MVT VT,
2324 unsigned Offset) const {
2325 SDLoc SL(Op);
2326 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
2327 DAG.getEntryNode(), Offset, false);
2328 // The local size values will have the hi 16-bits as zero.
2329 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
2330 DAG.getValueType(VT));
2331}
2332
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002333static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
Matt Arsenaulte0132462016-01-30 05:19:45 +00002334 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002335 "non-hsa intrinsic with hsa target",
2336 DL.getDebugLoc());
2337 DAG.getContext()->diagnose(BadIntrin);
2338 return DAG.getUNDEF(VT);
2339}
2340
2341static SDValue emitRemovedIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
2342 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
2343 "intrinsic not supported on subtarget",
2344 DL.getDebugLoc());
Matt Arsenaulte0132462016-01-30 05:19:45 +00002345 DAG.getContext()->diagnose(BadIntrin);
2346 return DAG.getUNDEF(VT);
2347}
2348
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002349SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2350 SelectionDAG &DAG) const {
2351 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00002352 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002353 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002354
2355 EVT VT = Op.getValueType();
2356 SDLoc DL(Op);
2357 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2358
Sanjay Patela2607012015-09-16 16:31:21 +00002359 // TODO: Should this propagate fast-math-flags?
2360
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002361 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00002362 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault48ab5262016-04-25 19:27:18 +00002363 case Intrinsic::amdgcn_queue_ptr: {
Tom Stellard0b76fc4c2016-09-16 21:34:26 +00002364 if (!Subtarget->isAmdCodeObjectV2()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00002365 DiagnosticInfoUnsupported BadIntrin(
2366 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
2367 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00002368 DAG.getContext()->diagnose(BadIntrin);
2369 return DAG.getUNDEF(VT);
2370 }
2371
Matt Arsenault48ab5262016-04-25 19:27:18 +00002372 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
2373 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00002374 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
Matt Arsenault48ab5262016-04-25 19:27:18 +00002375 TRI->getPreloadedValue(MF, Reg), VT);
2376 }
Jan Veselyfea814d2016-06-21 20:46:20 +00002377 case Intrinsic::amdgcn_implicitarg_ptr: {
2378 unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
2379 return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset);
2380 }
Matt Arsenaultdc4ebad2016-04-29 21:16:52 +00002381 case Intrinsic::amdgcn_kernarg_segment_ptr: {
2382 unsigned Reg
2383 = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
2384 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2385 }
Matt Arsenault8d718dc2016-07-22 17:01:30 +00002386 case Intrinsic::amdgcn_dispatch_id: {
2387 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_ID);
2388 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2389 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002390 case Intrinsic::amdgcn_rcp:
2391 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
2392 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00002393 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002394 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002395 case Intrinsic::amdgcn_rsq_legacy: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002396 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002397 return emitRemovedIntrinsicError(DAG, DL, VT);
2398
2399 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
2400 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002401 case Intrinsic::amdgcn_rcp_legacy: {
2402 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
2403 return emitRemovedIntrinsicError(DAG, DL, VT);
2404 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1));
2405 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +00002406 case Intrinsic::amdgcn_rsq_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002407 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00002408 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00002409
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002410 Type *Type = VT.getTypeForEVT(*DAG.getContext());
2411 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
2412 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
2413
2414 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
2415 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
2416 DAG.getConstantFP(Max, DL, VT));
2417 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
2418 DAG.getConstantFP(Min, DL, VT));
2419 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002420 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002421 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002422 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002423
Tom Stellardec2e43c2014-09-22 15:35:29 +00002424 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2425 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002426 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002427 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002428 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002429
Tom Stellardec2e43c2014-09-22 15:35:29 +00002430 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2431 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002432 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002433 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002434 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002435
Tom Stellardec2e43c2014-09-22 15:35:29 +00002436 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2437 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002438 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002439 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002440 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002441
Tom Stellardec2e43c2014-09-22 15:35:29 +00002442 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2443 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002444 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002445 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002446 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002447
Tom Stellardec2e43c2014-09-22 15:35:29 +00002448 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2449 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002450 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002451 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002452 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002453
Tom Stellardec2e43c2014-09-22 15:35:29 +00002454 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2455 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002456 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002457 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002458 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002459
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002460 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2461 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002462 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002463 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002464 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002465
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002466 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2467 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002468 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002469 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002470 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002471
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002472 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2473 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenault43976df2016-01-30 04:25:19 +00002474 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002475 case Intrinsic::r600_read_tgid_x:
Marek Olsak79c05872016-11-25 17:37:09 +00002476 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002477 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002478 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002479 case Intrinsic::r600_read_tgid_y:
Marek Olsak79c05872016-11-25 17:37:09 +00002480 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002481 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002482 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002483 case Intrinsic::r600_read_tgid_z:
Marek Olsak79c05872016-11-25 17:37:09 +00002484 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002485 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002486 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002487 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002488 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002489 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002490 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002491 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002492 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002493 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002494 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002495 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002496 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002497 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002498 case AMDGPUIntrinsic::SI_load_const: {
2499 SDValue Ops[] = {
2500 Op.getOperand(1),
2501 Op.getOperand(2)
2502 };
2503
2504 MachineMemOperand *MMO = MF.getMachineMemOperand(
Justin Lebaradbf09e2016-09-11 01:38:58 +00002505 MachinePointerInfo(),
2506 MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable |
2507 MachineMemOperand::MOInvariant,
2508 VT.getStoreSize(), 4);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002509 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
2510 Op->getVTList(), Ops, VT, MMO);
2511 }
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002512 case AMDGPUIntrinsic::amdgcn_fdiv_fast: {
2513 return lowerFDIV_FAST(Op, DAG);
2514 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002515 case AMDGPUIntrinsic::SI_vs_load_input:
2516 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
2517 Op.getOperand(1),
2518 Op.getOperand(2),
2519 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00002520
Tom Stellard2a9d9472015-05-12 15:00:46 +00002521 case AMDGPUIntrinsic::SI_fs_constant: {
2522 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2523 SDValue Glue = M0.getValue(1);
2524 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
2525 DAG.getConstant(2, DL, MVT::i32), // P0
2526 Op.getOperand(1), Op.getOperand(2), Glue);
2527 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00002528 case AMDGPUIntrinsic::SI_packf16:
2529 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
2530 return DAG.getUNDEF(MVT::i32);
2531 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00002532 case AMDGPUIntrinsic::SI_fs_interp: {
2533 SDValue IJ = Op.getOperand(4);
2534 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2535 DAG.getConstant(0, DL, MVT::i32));
2536 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2537 DAG.getConstant(1, DL, MVT::i32));
Tom Stellard1473f072016-11-26 02:26:04 +00002538 I = DAG.getNode(ISD::BITCAST, DL, MVT::f32, I);
2539 J = DAG.getNode(ISD::BITCAST, DL, MVT::f32, J);
Tom Stellard2a9d9472015-05-12 15:00:46 +00002540 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2541 SDValue Glue = M0.getValue(1);
2542 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
2543 DAG.getVTList(MVT::f32, MVT::Glue),
2544 I, Op.getOperand(1), Op.getOperand(2), Glue);
2545 Glue = SDValue(P1.getNode(), 1);
2546 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
2547 Op.getOperand(1), Op.getOperand(2), Glue);
2548 }
Tom Stellard2187bb82016-12-06 23:52:13 +00002549 case Intrinsic::amdgcn_interp_mov: {
2550 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
2551 SDValue Glue = M0.getValue(1);
2552 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32, Op.getOperand(1),
2553 Op.getOperand(2), Op.getOperand(3), Glue);
2554 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00002555 case Intrinsic::amdgcn_interp_p1: {
2556 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
2557 SDValue Glue = M0.getValue(1);
2558 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
2559 Op.getOperand(2), Op.getOperand(3), Glue);
2560 }
2561 case Intrinsic::amdgcn_interp_p2: {
2562 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
2563 SDValue Glue = SDValue(M0.getNode(), 1);
2564 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
2565 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
2566 Glue);
2567 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002568 case Intrinsic::amdgcn_sin:
2569 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
2570
2571 case Intrinsic::amdgcn_cos:
2572 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
2573
2574 case Intrinsic::amdgcn_log_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002575 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002576 return SDValue();
2577
2578 DiagnosticInfoUnsupported BadIntrin(
2579 *MF.getFunction(), "intrinsic not supported on subtarget",
2580 DL.getDebugLoc());
2581 DAG.getContext()->diagnose(BadIntrin);
2582 return DAG.getUNDEF(VT);
2583 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002584 case Intrinsic::amdgcn_ldexp:
2585 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
2586 Op.getOperand(1), Op.getOperand(2));
Matt Arsenault74015162016-05-28 00:19:52 +00002587
2588 case Intrinsic::amdgcn_fract:
2589 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
2590
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002591 case Intrinsic::amdgcn_class:
2592 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
2593 Op.getOperand(1), Op.getOperand(2));
2594 case Intrinsic::amdgcn_div_fmas:
2595 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
2596 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
2597 Op.getOperand(4));
2598
2599 case Intrinsic::amdgcn_div_fixup:
2600 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
2601 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
2602
2603 case Intrinsic::amdgcn_trig_preop:
2604 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
2605 Op.getOperand(1), Op.getOperand(2));
2606 case Intrinsic::amdgcn_div_scale: {
2607 // 3rd parameter required to be a constant.
2608 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2609 if (!Param)
2610 return DAG.getUNDEF(VT);
2611
2612 // Translate to the operands expected by the machine instruction. The
2613 // first parameter must be the same as the first instruction.
2614 SDValue Numerator = Op.getOperand(1);
2615 SDValue Denominator = Op.getOperand(2);
2616
2617 // Note this order is opposite of the machine instruction's operations,
2618 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
2619 // intrinsic has the numerator as the first operand to match a normal
2620 // division operation.
2621
2622 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
2623
2624 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
2625 Denominator, Numerator);
2626 }
Wei Ding07e03712016-07-28 16:42:13 +00002627 case Intrinsic::amdgcn_icmp: {
2628 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2629 int CondCode = CD->getSExtValue();
2630
2631 if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002632 CondCode >= ICmpInst::Predicate::BAD_ICMP_PREDICATE)
Wei Ding07e03712016-07-28 16:42:13 +00002633 return DAG.getUNDEF(VT);
2634
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002635 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002636 ISD::CondCode CCOpcode = getICmpCondCode(IcInput);
2637 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2638 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2639 }
2640 case Intrinsic::amdgcn_fcmp: {
2641 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2642 int CondCode = CD->getSExtValue();
2643
2644 if (CondCode <= FCmpInst::Predicate::FCMP_FALSE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002645 CondCode >= FCmpInst::Predicate::FCMP_TRUE)
Wei Ding07e03712016-07-28 16:42:13 +00002646 return DAG.getUNDEF(VT);
2647
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002648 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002649 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput);
2650 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2651 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2652 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002653 case Intrinsic::amdgcn_fmul_legacy:
2654 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT,
2655 Op.getOperand(1), Op.getOperand(2));
Matt Arsenaultc96e1de2016-07-18 18:35:05 +00002656 case Intrinsic::amdgcn_sffbh:
2657 case AMDGPUIntrinsic::AMDGPU_flbit_i32: // Legacy name.
2658 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002659 default:
2660 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
2661 }
2662}
2663
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002664SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
2665 SelectionDAG &DAG) const {
2666 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Tom Stellard6f9ef142016-12-20 17:19:44 +00002667 SDLoc DL(Op);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002668 switch (IntrID) {
2669 case Intrinsic::amdgcn_atomic_inc:
2670 case Intrinsic::amdgcn_atomic_dec: {
2671 MemSDNode *M = cast<MemSDNode>(Op);
2672 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
2673 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
2674 SDValue Ops[] = {
2675 M->getOperand(0), // Chain
2676 M->getOperand(2), // Ptr
2677 M->getOperand(3) // Value
2678 };
2679
2680 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
2681 M->getMemoryVT(), M->getMemOperand());
2682 }
Tom Stellard6f9ef142016-12-20 17:19:44 +00002683 case Intrinsic::amdgcn_buffer_load:
2684 case Intrinsic::amdgcn_buffer_load_format: {
2685 SDValue Ops[] = {
2686 Op.getOperand(0), // Chain
2687 Op.getOperand(2), // rsrc
2688 Op.getOperand(3), // vindex
2689 Op.getOperand(4), // offset
2690 Op.getOperand(5), // glc
2691 Op.getOperand(6) // slc
2692 };
2693 MachineFunction &MF = DAG.getMachineFunction();
2694 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
2695
2696 unsigned Opc = (IntrID == Intrinsic::amdgcn_buffer_load) ?
2697 AMDGPUISD::BUFFER_LOAD : AMDGPUISD::BUFFER_LOAD_FORMAT;
2698 EVT VT = Op.getValueType();
2699 EVT IntVT = VT.changeTypeToInteger();
2700
2701 MachineMemOperand *MMO = MF.getMachineMemOperand(
2702 MachinePointerInfo(MFI->getBufferPSV()),
2703 MachineMemOperand::MOLoad,
2704 VT.getStoreSize(), VT.getStoreSize());
2705
2706 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, IntVT, MMO);
2707 }
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002708 default:
2709 return SDValue();
2710 }
2711}
2712
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002713SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
2714 SelectionDAG &DAG) const {
2715 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00002716 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002717 SDValue Chain = Op.getOperand(0);
2718 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2719
2720 switch (IntrinsicID) {
Jan Veselyd48445d2017-01-04 18:06:55 +00002721 case AMDGPUIntrinsic::SI_sendmsg:
2722 case Intrinsic::amdgcn_s_sendmsg: {
Tom Stellardfc92e772015-05-12 14:18:14 +00002723 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
2724 SDValue Glue = Chain.getValue(1);
2725 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
2726 Op.getOperand(2), Glue);
2727 }
Jan Veselyd48445d2017-01-04 18:06:55 +00002728 case Intrinsic::amdgcn_s_sendmsghalt: {
2729 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
2730 SDValue Glue = Chain.getValue(1);
2731 return DAG.getNode(AMDGPUISD::SENDMSGHALT, DL, MVT::Other, Chain,
2732 Op.getOperand(2), Glue);
2733 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002734 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002735 SDValue Ops[] = {
2736 Chain,
2737 Op.getOperand(2),
2738 Op.getOperand(3),
2739 Op.getOperand(4),
2740 Op.getOperand(5),
2741 Op.getOperand(6),
2742 Op.getOperand(7),
2743 Op.getOperand(8),
2744 Op.getOperand(9),
2745 Op.getOperand(10),
2746 Op.getOperand(11),
2747 Op.getOperand(12),
2748 Op.getOperand(13),
2749 Op.getOperand(14)
2750 };
2751
2752 EVT VT = Op.getOperand(3).getValueType();
2753
2754 MachineMemOperand *MMO = MF.getMachineMemOperand(
2755 MachinePointerInfo(),
2756 MachineMemOperand::MOStore,
2757 VT.getStoreSize(), 4);
2758 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
2759 Op->getVTList(), Ops, VT, MMO);
2760 }
Matt Arsenault00568682016-07-13 06:04:22 +00002761 case AMDGPUIntrinsic::AMDGPU_kill: {
Matt Arsenault03006fd2016-07-19 16:27:56 +00002762 SDValue Src = Op.getOperand(2);
2763 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) {
Matt Arsenault00568682016-07-13 06:04:22 +00002764 if (!K->isNegative())
2765 return Chain;
Matt Arsenault03006fd2016-07-19 16:27:56 +00002766
2767 SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32);
2768 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne);
Matt Arsenault00568682016-07-13 06:04:22 +00002769 }
2770
Matt Arsenault03006fd2016-07-19 16:27:56 +00002771 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src);
2772 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast);
Matt Arsenault00568682016-07-13 06:04:22 +00002773 }
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00002774 case AMDGPUIntrinsic::SI_export: {
2775 const ConstantSDNode *En = cast<ConstantSDNode>(Op.getOperand(2));
2776 const ConstantSDNode *VM = cast<ConstantSDNode>(Op.getOperand(3));
2777 const ConstantSDNode *Done = cast<ConstantSDNode>(Op.getOperand(4));
2778 const ConstantSDNode *Tgt = cast<ConstantSDNode>(Op.getOperand(5));
2779 const ConstantSDNode *Compr = cast<ConstantSDNode>(Op.getOperand(6));
2780
2781 const SDValue Ops[] = {
2782 Chain,
2783 DAG.getTargetConstant(En->getZExtValue(), DL, MVT::i8),
2784 DAG.getTargetConstant(VM->getZExtValue(), DL, MVT::i1),
2785 DAG.getTargetConstant(Tgt->getZExtValue(), DL, MVT::i8),
2786 DAG.getTargetConstant(Compr->getZExtValue(), DL, MVT::i1),
2787 Op.getOperand(7), // src0
2788 Op.getOperand(8), // src1
2789 Op.getOperand(9), // src2
2790 Op.getOperand(10) // src3
2791 };
2792
2793 unsigned Opc = Done->isNullValue() ?
2794 AMDGPUISD::EXPORT : AMDGPUISD::EXPORT_DONE;
2795 return DAG.getNode(Opc, DL, Op->getVTList(), Ops);
2796 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002797 default:
2798 return SDValue();
2799 }
2800}
2801
Tom Stellard81d871d2013-11-13 23:36:50 +00002802SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2803 SDLoc DL(Op);
2804 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00002805 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00002806 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00002807
Matt Arsenaulta1436412016-02-10 18:21:45 +00002808 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
Matt Arsenault6dfda962016-02-10 18:21:39 +00002809 // FIXME: Copied from PPC
2810 // First, load into 32 bits, then truncate to 1 bit.
2811
2812 SDValue Chain = Load->getChain();
2813 SDValue BasePtr = Load->getBasePtr();
2814 MachineMemOperand *MMO = Load->getMemOperand();
2815
Tom Stellard115a6152016-11-10 16:02:37 +00002816 EVT RealMemVT = (MemVT == MVT::i1) ? MVT::i8 : MVT::i16;
2817
Matt Arsenault6dfda962016-02-10 18:21:39 +00002818 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
Tom Stellard115a6152016-11-10 16:02:37 +00002819 BasePtr, RealMemVT, MMO);
Matt Arsenault6dfda962016-02-10 18:21:39 +00002820
2821 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002822 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00002823 NewLD.getValue(1)
2824 };
2825
2826 return DAG.getMergeValues(Ops, DL);
2827 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002828
Matt Arsenaulta1436412016-02-10 18:21:45 +00002829 if (!MemVT.isVector())
2830 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002831
Matt Arsenaulta1436412016-02-10 18:21:45 +00002832 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
2833 "Custom lowering for non-i32 vectors hasn't been implemented.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002834
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002835 unsigned AS = Load->getAddressSpace();
2836 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
2837 AS, Load->getAlignment())) {
2838 SDValue Ops[2];
2839 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
2840 return DAG.getMergeValues(Ops, DL);
2841 }
2842
Tom Stellardf8e6eaf2016-10-26 14:38:47 +00002843 MachineFunction &MF = DAG.getMachineFunction();
2844 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
2845 // If there is a possibilty that flat instruction access scratch memory
2846 // then we need to use the same legalization rules we use for private.
2847 if (AS == AMDGPUAS::FLAT_ADDRESS)
2848 AS = MFI->hasFlatScratchInit() ?
2849 AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS;
2850
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002851 unsigned NumElements = MemVT.getVectorNumElements();
2852 switch (AS) {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002853 case AMDGPUAS::CONSTANT_ADDRESS:
2854 if (isMemOpUniform(Load))
2855 return SDValue();
2856 // Non-uniform loads will be selected to MUBUF instructions, so they
Alexander Timofeev18009562016-12-08 17:28:47 +00002857 // have the same legalization requirements as global and private
Matt Arsenaulta1436412016-02-10 18:21:45 +00002858 // loads.
2859 //
Justin Bognerb03fd122016-08-17 05:10:15 +00002860 LLVM_FALLTHROUGH;
Alexander Timofeev18009562016-12-08 17:28:47 +00002861 case AMDGPUAS::GLOBAL_ADDRESS: {
Alexander Timofeeva57511c2016-12-15 15:17:19 +00002862 if (Subtarget->getScalarizeGlobalBehavior() && isMemOpUniform(Load) &&
2863 isMemOpHasNoClobberedMemOperand(Load))
Alexander Timofeev18009562016-12-08 17:28:47 +00002864 return SDValue();
2865 // Non-uniform loads will be selected to MUBUF instructions, so they
2866 // have the same legalization requirements as global and private
2867 // loads.
2868 //
2869 }
2870 LLVM_FALLTHROUGH;
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002871 case AMDGPUAS::FLAT_ADDRESS:
2872 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00002873 return SplitVectorLoad(Op, DAG);
2874 // v4 loads are supported for private and global memory.
2875 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002876 case AMDGPUAS::PRIVATE_ADDRESS: {
2877 // Depending on the setting of the private_element_size field in the
2878 // resource descriptor, we can only make private accesses up to a certain
2879 // size.
2880 switch (Subtarget->getMaxPrivateElementSize()) {
2881 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002882 return scalarizeVectorLoad(Load, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002883 case 8:
2884 if (NumElements > 2)
2885 return SplitVectorLoad(Op, DAG);
2886 return SDValue();
2887 case 16:
2888 // Same as global/flat
2889 if (NumElements > 4)
2890 return SplitVectorLoad(Op, DAG);
2891 return SDValue();
2892 default:
2893 llvm_unreachable("unsupported private_element_size");
2894 }
2895 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002896 case AMDGPUAS::LOCAL_ADDRESS: {
2897 if (NumElements > 2)
2898 return SplitVectorLoad(Op, DAG);
2899
2900 if (NumElements == 2)
2901 return SDValue();
2902
Matt Arsenaulta1436412016-02-10 18:21:45 +00002903 // If properly aligned, if we split we might be able to use ds_read_b64.
2904 return SplitVectorLoad(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002905 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00002906 default:
2907 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00002908 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002909}
2910
Tom Stellard0ec134f2014-02-04 17:18:40 +00002911SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2912 if (Op.getValueType() != MVT::i64)
2913 return SDValue();
2914
2915 SDLoc DL(Op);
2916 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002917
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002918 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2919 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002920
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002921 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
2922 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
2923
2924 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
2925 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002926
2927 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
2928
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002929 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
2930 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002931
2932 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
2933
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002934 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002935 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002936}
2937
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002938// Catch division cases where we can use shortcuts with rcp and rsq
2939// instructions.
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002940SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op,
2941 SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002942 SDLoc SL(Op);
2943 SDValue LHS = Op.getOperand(0);
2944 SDValue RHS = Op.getOperand(1);
2945 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002946 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002947
2948 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenaultcdff21b2016-12-22 03:05:44 +00002949 if (Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals()) ||
2950 VT == MVT::f16) {
Matt Arsenault979902b2016-08-02 22:25:04 +00002951 if (CLHS->isExactlyValue(1.0)) {
2952 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
2953 // the CI documentation has a worst case error of 1 ulp.
2954 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
2955 // use it as long as we aren't trying to use denormals.
Matt Arsenaultcdff21b2016-12-22 03:05:44 +00002956 //
2957 // v_rcp_f16 and v_rsq_f16 DO support denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002958
Matt Arsenault979902b2016-08-02 22:25:04 +00002959 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenaultcdff21b2016-12-22 03:05:44 +00002960
Matt Arsenault979902b2016-08-02 22:25:04 +00002961 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
2962 // error seems really high at 2^29 ULP.
2963 if (RHS.getOpcode() == ISD::FSQRT)
2964 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
2965
2966 // 1.0 / x -> rcp(x)
2967 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
2968 }
2969
2970 // Same as for 1.0, but expand the sign out of the constant.
2971 if (CLHS->isExactlyValue(-1.0)) {
2972 // -1.0 / x -> rcp (fneg x)
2973 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2974 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS);
2975 }
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002976 }
2977 }
2978
Wei Dinged0f97f2016-06-09 19:17:15 +00002979 const SDNodeFlags *Flags = Op->getFlags();
2980
2981 if (Unsafe || Flags->hasAllowReciprocal()) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002982 // Turn into multiply by the reciprocal.
2983 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00002984 SDNodeFlags Flags;
2985 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002986 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00002987 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002988 }
2989
2990 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002991}
2992
Tom Stellard8485fa02016-12-07 02:42:15 +00002993static SDValue getFPBinOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL,
2994 EVT VT, SDValue A, SDValue B, SDValue GlueChain) {
2995 if (GlueChain->getNumValues() <= 1) {
2996 return DAG.getNode(Opcode, SL, VT, A, B);
2997 }
2998
2999 assert(GlueChain->getNumValues() == 3);
3000
3001 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue);
3002 switch (Opcode) {
3003 default: llvm_unreachable("no chain equivalent for opcode");
3004 case ISD::FMUL:
3005 Opcode = AMDGPUISD::FMUL_W_CHAIN;
3006 break;
3007 }
3008
3009 return DAG.getNode(Opcode, SL, VTList, GlueChain.getValue(1), A, B,
3010 GlueChain.getValue(2));
3011}
3012
3013static SDValue getFPTernOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL,
3014 EVT VT, SDValue A, SDValue B, SDValue C,
3015 SDValue GlueChain) {
3016 if (GlueChain->getNumValues() <= 1) {
3017 return DAG.getNode(Opcode, SL, VT, A, B, C);
3018 }
3019
3020 assert(GlueChain->getNumValues() == 3);
3021
3022 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue);
3023 switch (Opcode) {
3024 default: llvm_unreachable("no chain equivalent for opcode");
3025 case ISD::FMA:
3026 Opcode = AMDGPUISD::FMA_W_CHAIN;
3027 break;
3028 }
3029
3030 return DAG.getNode(Opcode, SL, VTList, GlueChain.getValue(1), A, B, C,
3031 GlueChain.getValue(2));
3032}
3033
Matt Arsenault4052a572016-12-22 03:05:41 +00003034SDValue SITargetLowering::LowerFDIV16(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaultcdff21b2016-12-22 03:05:44 +00003035 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
3036 return FastLowered;
3037
Matt Arsenault4052a572016-12-22 03:05:41 +00003038 SDLoc SL(Op);
3039 SDValue Src0 = Op.getOperand(0);
3040 SDValue Src1 = Op.getOperand(1);
3041
3042 SDValue CvtSrc0 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src0);
3043 SDValue CvtSrc1 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src1);
3044
3045 SDValue RcpSrc1 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, CvtSrc1);
3046 SDValue Quot = DAG.getNode(ISD::FMUL, SL, MVT::f32, CvtSrc0, RcpSrc1);
3047
3048 SDValue FPRoundFlag = DAG.getTargetConstant(0, SL, MVT::i32);
3049 SDValue BestQuot = DAG.getNode(ISD::FP_ROUND, SL, MVT::f16, Quot, FPRoundFlag);
3050
3051 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f16, BestQuot, Src1, Src0);
3052}
3053
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00003054// Faster 2.5 ULP division that does not support denormals.
3055SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const {
3056 SDLoc SL(Op);
3057 SDValue LHS = Op.getOperand(1);
3058 SDValue RHS = Op.getOperand(2);
3059
3060 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
3061
3062 const APFloat K0Val(BitsToFloat(0x6f800000));
3063 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
3064
3065 const APFloat K1Val(BitsToFloat(0x2f800000));
3066 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
3067
3068 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
3069
3070 EVT SetCCVT =
3071 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
3072
3073 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
3074
3075 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
3076
3077 // TODO: Should this propagate fast-math-flags?
3078 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
3079
3080 // rcp does not support denormals.
3081 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
3082
3083 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
3084
3085 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
3086}
3087
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00003088SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00003089 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
Eric Christopher538d09d02016-06-07 20:27:12 +00003090 return FastLowered;
Matt Arsenault22ca3f82014-07-15 23:50:10 +00003091
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00003092 SDLoc SL(Op);
3093 SDValue LHS = Op.getOperand(0);
3094 SDValue RHS = Op.getOperand(1);
3095
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003096 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003097
Wei Dinged0f97f2016-06-09 19:17:15 +00003098 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003099
Tom Stellard8485fa02016-12-07 02:42:15 +00003100 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT,
3101 RHS, RHS, LHS);
3102 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT,
3103 LHS, RHS, LHS);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003104
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00003105 // Denominator is scaled to not be denormal, so using rcp is ok.
Tom Stellard8485fa02016-12-07 02:42:15 +00003106 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32,
3107 DenominatorScaled);
3108 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32,
3109 DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003110
Tom Stellard8485fa02016-12-07 02:42:15 +00003111 const unsigned Denorm32Reg = AMDGPU::Hwreg::ID_MODE |
3112 (4 << AMDGPU::Hwreg::OFFSET_SHIFT_) |
3113 (1 << AMDGPU::Hwreg::WIDTH_M1_SHIFT_);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003114
Tom Stellard8485fa02016-12-07 02:42:15 +00003115 const SDValue BitField = DAG.getTargetConstant(Denorm32Reg, SL, MVT::i16);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003116
Tom Stellard8485fa02016-12-07 02:42:15 +00003117 if (!Subtarget->hasFP32Denormals()) {
3118 SDVTList BindParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
3119 const SDValue EnableDenormValue = DAG.getConstant(FP_DENORM_FLUSH_NONE,
3120 SL, MVT::i32);
3121 SDValue EnableDenorm = DAG.getNode(AMDGPUISD::SETREG, SL, BindParamVTs,
3122 DAG.getEntryNode(),
3123 EnableDenormValue, BitField);
3124 SDValue Ops[3] = {
3125 NegDivScale0,
3126 EnableDenorm.getValue(0),
3127 EnableDenorm.getValue(1)
3128 };
Matt Arsenault37fefd62016-06-10 02:18:02 +00003129
Tom Stellard8485fa02016-12-07 02:42:15 +00003130 NegDivScale0 = DAG.getMergeValues(Ops, SL);
3131 }
3132
3133 SDValue Fma0 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0,
3134 ApproxRcp, One, NegDivScale0);
3135
3136 SDValue Fma1 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp,
3137 ApproxRcp, Fma0);
3138
3139 SDValue Mul = getFPBinOp(DAG, ISD::FMUL, SL, MVT::f32, NumeratorScaled,
3140 Fma1, Fma1);
3141
3142 SDValue Fma2 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Mul,
3143 NumeratorScaled, Mul);
3144
3145 SDValue Fma3 = getFPTernOp(DAG, ISD::FMA,SL, MVT::f32, Fma2, Fma1, Mul, Fma2);
3146
3147 SDValue Fma4 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3,
3148 NumeratorScaled, Fma3);
3149
3150 if (!Subtarget->hasFP32Denormals()) {
3151 const SDValue DisableDenormValue =
3152 DAG.getConstant(FP_DENORM_FLUSH_IN_FLUSH_OUT, SL, MVT::i32);
3153 SDValue DisableDenorm = DAG.getNode(AMDGPUISD::SETREG, SL, MVT::Other,
3154 Fma4.getValue(1),
3155 DisableDenormValue,
3156 BitField,
3157 Fma4.getValue(2));
3158
3159 SDValue OutputChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
3160 DisableDenorm, DAG.getRoot());
3161 DAG.setRoot(OutputChain);
3162 }
Matt Arsenault37fefd62016-06-10 02:18:02 +00003163
Wei Dinged0f97f2016-06-09 19:17:15 +00003164 SDValue Scale = NumeratorScaled.getValue(1);
Tom Stellard8485fa02016-12-07 02:42:15 +00003165 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32,
3166 Fma4, Fma1, Fma3, Scale);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003167
Wei Dinged0f97f2016-06-09 19:17:15 +00003168 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00003169}
3170
3171SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00003172 if (DAG.getTarget().Options.UnsafeFPMath)
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00003173 return lowerFastUnsafeFDIV(Op, DAG);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00003174
3175 SDLoc SL(Op);
3176 SDValue X = Op.getOperand(0);
3177 SDValue Y = Op.getOperand(1);
3178
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003179 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00003180
3181 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
3182
3183 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
3184
3185 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
3186
3187 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
3188
3189 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
3190
3191 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
3192
3193 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
3194
3195 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
3196
3197 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
3198 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
3199
3200 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
3201 NegDivScale0, Mul, DivScale1);
3202
3203 SDValue Scale;
3204
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003205 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00003206 // Workaround a hardware bug on SI where the condition output from div_scale
3207 // is not usable.
3208
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003209 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00003210
3211 // Figure out if the scale to use for div_fmas.
3212 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
3213 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
3214 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
3215 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
3216
3217 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
3218 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
3219
3220 SDValue Scale0Hi
3221 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
3222 SDValue Scale1Hi
3223 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
3224
3225 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
3226 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
3227 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
3228 } else {
3229 Scale = DivScale1.getValue(1);
3230 }
3231
3232 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
3233 Fma4, Fma3, Mul, Scale);
3234
3235 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00003236}
3237
3238SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
3239 EVT VT = Op.getValueType();
3240
3241 if (VT == MVT::f32)
3242 return LowerFDIV32(Op, DAG);
3243
3244 if (VT == MVT::f64)
3245 return LowerFDIV64(Op, DAG);
3246
Matt Arsenault4052a572016-12-22 03:05:41 +00003247 if (VT == MVT::f16)
3248 return LowerFDIV16(Op, DAG);
3249
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00003250 llvm_unreachable("Unexpected type for fdiv");
3251}
3252
Tom Stellard81d871d2013-11-13 23:36:50 +00003253SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
3254 SDLoc DL(Op);
3255 StoreSDNode *Store = cast<StoreSDNode>(Op);
3256 EVT VT = Store->getMemoryVT();
3257
Matt Arsenault95245662016-02-11 05:32:46 +00003258 if (VT == MVT::i1) {
3259 return DAG.getTruncStore(Store->getChain(), DL,
3260 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
3261 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00003262 }
3263
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003264 assert(VT.isVector() &&
3265 Store->getValue().getValueType().getScalarType() == MVT::i32);
3266
3267 unsigned AS = Store->getAddressSpace();
3268 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
3269 AS, Store->getAlignment())) {
3270 return expandUnalignedStore(Store, DAG);
3271 }
Tom Stellard81d871d2013-11-13 23:36:50 +00003272
Tom Stellardf8e6eaf2016-10-26 14:38:47 +00003273 MachineFunction &MF = DAG.getMachineFunction();
3274 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
3275 // If there is a possibilty that flat instruction access scratch memory
3276 // then we need to use the same legalization rules we use for private.
3277 if (AS == AMDGPUAS::FLAT_ADDRESS)
3278 AS = MFI->hasFlatScratchInit() ?
3279 AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS;
3280
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003281 unsigned NumElements = VT.getVectorNumElements();
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003282 switch (AS) {
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003283 case AMDGPUAS::GLOBAL_ADDRESS:
3284 case AMDGPUAS::FLAT_ADDRESS:
3285 if (NumElements > 4)
3286 return SplitVectorStore(Op, DAG);
3287 return SDValue();
3288 case AMDGPUAS::PRIVATE_ADDRESS: {
3289 switch (Subtarget->getMaxPrivateElementSize()) {
3290 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00003291 return scalarizeVectorStore(Store, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003292 case 8:
3293 if (NumElements > 2)
3294 return SplitVectorStore(Op, DAG);
3295 return SDValue();
3296 case 16:
3297 if (NumElements > 4)
3298 return SplitVectorStore(Op, DAG);
3299 return SDValue();
3300 default:
3301 llvm_unreachable("unsupported private_element_size");
3302 }
3303 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003304 case AMDGPUAS::LOCAL_ADDRESS: {
3305 if (NumElements > 2)
3306 return SplitVectorStore(Op, DAG);
3307
3308 if (NumElements == 2)
3309 return Op;
3310
Matt Arsenault95245662016-02-11 05:32:46 +00003311 // If properly aligned, if we split we might be able to use ds_write_b64.
3312 return SplitVectorStore(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003313 }
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003314 default:
3315 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00003316 }
Tom Stellard81d871d2013-11-13 23:36:50 +00003317}
3318
Matt Arsenaultad14ce82014-07-19 18:44:39 +00003319SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003320 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00003321 EVT VT = Op.getValueType();
3322 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00003323 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003324 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
3325 DAG.getNode(ISD::FMUL, DL, VT, Arg,
3326 DAG.getConstantFP(0.5/M_PI, DL,
3327 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00003328
3329 switch (Op.getOpcode()) {
3330 case ISD::FCOS:
3331 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
3332 case ISD::FSIN:
3333 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
3334 default:
3335 llvm_unreachable("Wrong trig opcode");
3336 }
3337}
3338
Tom Stellard354a43c2016-04-01 18:27:37 +00003339SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
3340 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
3341 assert(AtomicNode->isCompareAndSwap());
3342 unsigned AS = AtomicNode->getAddressSpace();
3343
3344 // No custom lowering required for local address space
3345 if (!isFlatGlobalAddrSpace(AS))
3346 return Op;
3347
3348 // Non-local address space requires custom lowering for atomic compare
3349 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
3350 SDLoc DL(Op);
3351 SDValue ChainIn = Op.getOperand(0);
3352 SDValue Addr = Op.getOperand(1);
3353 SDValue Old = Op.getOperand(2);
3354 SDValue New = Op.getOperand(3);
3355 EVT VT = Op.getValueType();
3356 MVT SimpleVT = VT.getSimpleVT();
3357 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
3358
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003359 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
Tom Stellard354a43c2016-04-01 18:27:37 +00003360 SDValue Ops[] = { ChainIn, Addr, NewOld };
Matt Arsenault88701812016-06-09 23:42:48 +00003361
3362 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(),
3363 Ops, VT, AtomicNode->getMemOperand());
Tom Stellard354a43c2016-04-01 18:27:37 +00003364}
3365
Tom Stellard75aadc22012-12-11 21:25:42 +00003366//===----------------------------------------------------------------------===//
3367// Custom DAG optimizations
3368//===----------------------------------------------------------------------===//
3369
Matt Arsenault364a6742014-06-11 17:50:44 +00003370SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00003371 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00003372 EVT VT = N->getValueType(0);
3373 EVT ScalarVT = VT.getScalarType();
3374 if (ScalarVT != MVT::f32)
3375 return SDValue();
3376
3377 SelectionDAG &DAG = DCI.DAG;
3378 SDLoc DL(N);
3379
3380 SDValue Src = N->getOperand(0);
3381 EVT SrcVT = Src.getValueType();
3382
3383 // TODO: We could try to match extracting the higher bytes, which would be
3384 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
3385 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
3386 // about in practice.
3387 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
3388 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
3389 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
3390 DCI.AddToWorklist(Cvt.getNode());
3391 return Cvt;
3392 }
3393 }
3394
Matt Arsenault364a6742014-06-11 17:50:44 +00003395 return SDValue();
3396}
3397
Eric Christopher6c5b5112015-03-11 18:43:21 +00003398/// \brief Return true if the given offset Size in bytes can be folded into
3399/// the immediate offsets of a memory instruction for the given address space.
3400static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003401 const SISubtarget &STI) {
Eric Christopher6c5b5112015-03-11 18:43:21 +00003402 switch (AS) {
3403 case AMDGPUAS::GLOBAL_ADDRESS: {
3404 // MUBUF instructions a 12-bit offset in bytes.
3405 return isUInt<12>(OffsetSize);
3406 }
3407 case AMDGPUAS::CONSTANT_ADDRESS: {
3408 // SMRD instructions have an 8-bit offset in dwords on SI and
3409 // a 20-bit offset in bytes on VI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003410 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Eric Christopher6c5b5112015-03-11 18:43:21 +00003411 return isUInt<20>(OffsetSize);
3412 else
3413 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
3414 }
3415 case AMDGPUAS::LOCAL_ADDRESS:
3416 case AMDGPUAS::REGION_ADDRESS: {
3417 // The single offset versions have a 16-bit offset in bytes.
3418 return isUInt<16>(OffsetSize);
3419 }
3420 case AMDGPUAS::PRIVATE_ADDRESS:
3421 // Indirect register addressing does not use any offsets.
3422 default:
3423 return 0;
3424 }
3425}
3426
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003427// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
3428
3429// This is a variant of
3430// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
3431//
3432// The normal DAG combiner will do this, but only if the add has one use since
3433// that would increase the number of instructions.
3434//
3435// This prevents us from seeing a constant offset that can be folded into a
3436// memory instruction's addressing mode. If we know the resulting add offset of
3437// a pointer can be folded into an addressing offset, we can replace the pointer
3438// operand with the add of new constant offset. This eliminates one of the uses,
3439// and may allow the remaining use to also be simplified.
3440//
3441SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
3442 unsigned AddrSpace,
3443 DAGCombinerInfo &DCI) const {
3444 SDValue N0 = N->getOperand(0);
3445 SDValue N1 = N->getOperand(1);
3446
3447 if (N0.getOpcode() != ISD::ADD)
3448 return SDValue();
3449
3450 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
3451 if (!CN1)
3452 return SDValue();
3453
3454 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3455 if (!CAdd)
3456 return SDValue();
3457
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003458 // If the resulting offset is too large, we can't fold it into the addressing
3459 // mode offset.
3460 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003461 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget()))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003462 return SDValue();
3463
3464 SelectionDAG &DAG = DCI.DAG;
3465 SDLoc SL(N);
3466 EVT VT = N->getValueType(0);
3467
3468 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003469 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003470
3471 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
3472}
3473
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003474SDValue SITargetLowering::performMemSDNodeCombine(MemSDNode *N,
3475 DAGCombinerInfo &DCI) const {
3476 SDValue Ptr = N->getBasePtr();
3477 SelectionDAG &DAG = DCI.DAG;
3478 SDLoc SL(N);
3479
3480 // TODO: We could also do this for multiplies.
3481 unsigned AS = N->getAddressSpace();
3482 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
3483 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
3484 if (NewPtr) {
3485 SmallVector<SDValue, 8> NewOps(N->op_begin(), N->op_end());
3486
3487 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
3488 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3489 }
3490 }
3491
3492 return SDValue();
3493}
3494
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003495static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) {
3496 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) ||
3497 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) ||
3498 (Opc == ISD::XOR && Val == 0);
3499}
3500
3501// Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This
3502// will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit
3503// integer combine opportunities since most 64-bit operations are decomposed
3504// this way. TODO: We won't want this for SALU especially if it is an inline
3505// immediate.
3506SDValue SITargetLowering::splitBinaryBitConstantOp(
3507 DAGCombinerInfo &DCI,
3508 const SDLoc &SL,
3509 unsigned Opc, SDValue LHS,
3510 const ConstantSDNode *CRHS) const {
3511 uint64_t Val = CRHS->getZExtValue();
3512 uint32_t ValLo = Lo_32(Val);
3513 uint32_t ValHi = Hi_32(Val);
3514 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
3515
3516 if ((bitOpWithConstantIsReducible(Opc, ValLo) ||
3517 bitOpWithConstantIsReducible(Opc, ValHi)) ||
3518 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) {
3519 // If we need to materialize a 64-bit immediate, it will be split up later
3520 // anyway. Avoid creating the harder to understand 64-bit immediate
3521 // materialization.
3522 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi);
3523 }
3524
3525 return SDValue();
3526}
3527
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003528SDValue SITargetLowering::performAndCombine(SDNode *N,
3529 DAGCombinerInfo &DCI) const {
3530 if (DCI.isBeforeLegalize())
3531 return SDValue();
3532
3533 SelectionDAG &DAG = DCI.DAG;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003534 EVT VT = N->getValueType(0);
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003535 SDValue LHS = N->getOperand(0);
3536 SDValue RHS = N->getOperand(1);
3537
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003538
3539 if (VT == MVT::i64) {
3540 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
3541 if (CRHS) {
3542 if (SDValue Split
3543 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS))
3544 return Split;
3545 }
3546 }
3547
3548 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
3549 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
3550 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) {
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003551 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
3552 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
3553
3554 SDValue X = LHS.getOperand(0);
3555 SDValue Y = RHS.getOperand(0);
3556 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
3557 return SDValue();
3558
3559 if (LCC == ISD::SETO) {
3560 if (X != LHS.getOperand(1))
3561 return SDValue();
3562
3563 if (RCC == ISD::SETUNE) {
3564 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
3565 if (!C1 || !C1->isInfinity() || C1->isNegative())
3566 return SDValue();
3567
3568 const uint32_t Mask = SIInstrFlags::N_NORMAL |
3569 SIInstrFlags::N_SUBNORMAL |
3570 SIInstrFlags::N_ZERO |
3571 SIInstrFlags::P_ZERO |
3572 SIInstrFlags::P_SUBNORMAL |
3573 SIInstrFlags::P_NORMAL;
3574
3575 static_assert(((~(SIInstrFlags::S_NAN |
3576 SIInstrFlags::Q_NAN |
3577 SIInstrFlags::N_INFINITY |
3578 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
3579 "mask not equal");
3580
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003581 SDLoc DL(N);
3582 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
3583 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003584 }
3585 }
3586 }
3587
3588 return SDValue();
3589}
3590
Matt Arsenaultf2290332015-01-06 23:00:39 +00003591SDValue SITargetLowering::performOrCombine(SDNode *N,
3592 DAGCombinerInfo &DCI) const {
3593 SelectionDAG &DAG = DCI.DAG;
3594 SDValue LHS = N->getOperand(0);
3595 SDValue RHS = N->getOperand(1);
3596
Matt Arsenault3b082382016-04-12 18:24:38 +00003597 EVT VT = N->getValueType(0);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003598 if (VT == MVT::i1) {
3599 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
3600 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
3601 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
3602 SDValue Src = LHS.getOperand(0);
3603 if (Src != RHS.getOperand(0))
3604 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003605
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003606 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
3607 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
3608 if (!CLHS || !CRHS)
3609 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003610
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003611 // Only 10 bits are used.
3612 static const uint32_t MaxMask = 0x3ff;
Matt Arsenault3b082382016-04-12 18:24:38 +00003613
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003614 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
3615 SDLoc DL(N);
3616 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
3617 Src, DAG.getConstant(NewMask, DL, MVT::i32));
3618 }
Matt Arsenault3b082382016-04-12 18:24:38 +00003619
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003620 return SDValue();
3621 }
3622
3623 if (VT != MVT::i64)
3624 return SDValue();
3625
3626 // TODO: This could be a generic combine with a predicate for extracting the
3627 // high half of an integer being free.
3628
3629 // (or i64:x, (zero_extend i32:y)) ->
3630 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
3631 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
3632 RHS.getOpcode() != ISD::ZERO_EXTEND)
3633 std::swap(LHS, RHS);
3634
3635 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
3636 SDValue ExtSrc = RHS.getOperand(0);
3637 EVT SrcVT = ExtSrc.getValueType();
3638 if (SrcVT == MVT::i32) {
3639 SDLoc SL(N);
3640 SDValue LowLHS, HiBits;
3641 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
3642 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
3643
3644 DCI.AddToWorklist(LowOr.getNode());
3645 DCI.AddToWorklist(HiBits.getNode());
3646
3647 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3648 LowOr, HiBits);
3649 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
Matt Arsenault3b082382016-04-12 18:24:38 +00003650 }
3651 }
3652
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003653 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
3654 if (CRHS) {
3655 if (SDValue Split
3656 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS))
3657 return Split;
3658 }
Matt Arsenaultf2290332015-01-06 23:00:39 +00003659
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003660 return SDValue();
3661}
Matt Arsenaultf2290332015-01-06 23:00:39 +00003662
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003663SDValue SITargetLowering::performXorCombine(SDNode *N,
3664 DAGCombinerInfo &DCI) const {
3665 EVT VT = N->getValueType(0);
3666 if (VT != MVT::i64)
3667 return SDValue();
Matt Arsenaultf2290332015-01-06 23:00:39 +00003668
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003669 SDValue LHS = N->getOperand(0);
3670 SDValue RHS = N->getOperand(1);
3671
3672 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
3673 if (CRHS) {
3674 if (SDValue Split
3675 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS))
3676 return Split;
Matt Arsenaultf2290332015-01-06 23:00:39 +00003677 }
3678
3679 return SDValue();
3680}
3681
3682SDValue SITargetLowering::performClassCombine(SDNode *N,
3683 DAGCombinerInfo &DCI) const {
3684 SelectionDAG &DAG = DCI.DAG;
3685 SDValue Mask = N->getOperand(1);
3686
3687 // fp_class x, 0 -> false
3688 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
3689 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003690 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003691 }
3692
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003693 if (N->getOperand(0).isUndef())
3694 return DAG.getUNDEF(MVT::i1);
3695
Matt Arsenaultf2290332015-01-06 23:00:39 +00003696 return SDValue();
3697}
3698
Matt Arsenault9cd90712016-04-14 01:42:16 +00003699// Constant fold canonicalize.
3700SDValue SITargetLowering::performFCanonicalizeCombine(
3701 SDNode *N,
3702 DAGCombinerInfo &DCI) const {
3703 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
3704 if (!CFP)
3705 return SDValue();
3706
3707 SelectionDAG &DAG = DCI.DAG;
3708 const APFloat &C = CFP->getValueAPF();
3709
3710 // Flush denormals to 0 if not enabled.
3711 if (C.isDenormal()) {
3712 EVT VT = N->getValueType(0);
3713 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
3714 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3715
3716 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
3717 return DAG.getConstantFP(0.0, SDLoc(N), VT);
Matt Arsenaultce841302016-12-22 03:05:37 +00003718
3719 if (VT == MVT::f16 && !Subtarget->hasFP16Denormals())
3720 return DAG.getConstantFP(0.0, SDLoc(N), VT);
Matt Arsenault9cd90712016-04-14 01:42:16 +00003721 }
3722
3723 if (C.isNaN()) {
3724 EVT VT = N->getValueType(0);
3725 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
3726 if (C.isSignaling()) {
3727 // Quiet a signaling NaN.
3728 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3729 }
3730
3731 // Make sure it is the canonical NaN bitpattern.
3732 //
3733 // TODO: Can we use -1 as the canonical NaN value since it's an inline
3734 // immediate?
3735 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
3736 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3737 }
3738
3739 return SDValue(CFP, 0);
3740}
3741
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003742static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
3743 switch (Opc) {
3744 case ISD::FMAXNUM:
3745 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003746 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003747 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003748 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003749 return AMDGPUISD::UMAX3;
3750 case ISD::FMINNUM:
3751 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003752 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003753 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003754 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003755 return AMDGPUISD::UMIN3;
3756 default:
3757 llvm_unreachable("Not a min/max opcode");
3758 }
3759}
3760
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003761static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3762 SDValue Op0, SDValue Op1, bool Signed) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003763 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
3764 if (!K1)
3765 return SDValue();
3766
3767 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
3768 if (!K0)
3769 return SDValue();
3770
Matt Arsenaultf639c322016-01-28 20:53:42 +00003771 if (Signed) {
3772 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
3773 return SDValue();
3774 } else {
3775 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
3776 return SDValue();
3777 }
3778
3779 EVT VT = K0->getValueType(0);
Tom Stellard115a6152016-11-10 16:02:37 +00003780
3781 MVT NVT = MVT::i32;
3782 unsigned ExtOp = Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3783
3784 SDValue Tmp1, Tmp2, Tmp3;
3785 Tmp1 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(0));
3786 Tmp2 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(1));
3787 Tmp3 = DAG.getNode(ExtOp, SL, NVT, Op1);
3788
3789 if (VT == MVT::i16) {
3790 Tmp1 = DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, NVT,
3791 Tmp1, Tmp2, Tmp3);
3792
3793 return DAG.getNode(ISD::TRUNCATE, SL, VT, Tmp1);
3794 } else
3795 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
3796 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
Matt Arsenaultf639c322016-01-28 20:53:42 +00003797}
3798
3799static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
3800 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
3801 return true;
3802
3803 return DAG.isKnownNeverNaN(Op);
3804}
3805
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003806static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3807 SDValue Op0, SDValue Op1) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003808 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
3809 if (!K1)
3810 return SDValue();
3811
3812 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
3813 if (!K0)
3814 return SDValue();
3815
3816 // Ordered >= (although NaN inputs should have folded away by now).
3817 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
3818 if (Cmp == APFloat::cmpGreaterThan)
3819 return SDValue();
3820
3821 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
3822 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
3823 // give the other result, which is different from med3 with a NaN input.
3824 SDValue Var = Op0.getOperand(0);
3825 if (!isKnownNeverSNan(DAG, Var))
3826 return SDValue();
3827
3828 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
3829 Var, SDValue(K0, 0), SDValue(K1, 0));
3830}
3831
3832SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
3833 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003834 SelectionDAG &DAG = DCI.DAG;
3835
3836 unsigned Opc = N->getOpcode();
3837 SDValue Op0 = N->getOperand(0);
3838 SDValue Op1 = N->getOperand(1);
3839
3840 // Only do this if the inner op has one use since this will just increases
3841 // register pressure for no benefit.
3842
Matt Arsenault5b39b342016-01-28 20:53:48 +00003843 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
3844 // max(max(a, b), c) -> max3(a, b, c)
3845 // min(min(a, b), c) -> min3(a, b, c)
3846 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
3847 SDLoc DL(N);
3848 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3849 DL,
3850 N->getValueType(0),
3851 Op0.getOperand(0),
3852 Op0.getOperand(1),
3853 Op1);
3854 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003855
Matt Arsenault5b39b342016-01-28 20:53:48 +00003856 // Try commuted.
3857 // max(a, max(b, c)) -> max3(a, b, c)
3858 // min(a, min(b, c)) -> min3(a, b, c)
3859 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
3860 SDLoc DL(N);
3861 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3862 DL,
3863 N->getValueType(0),
3864 Op0,
3865 Op1.getOperand(0),
3866 Op1.getOperand(1));
3867 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003868 }
3869
Matt Arsenaultf639c322016-01-28 20:53:42 +00003870 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
3871 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
3872 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
3873 return Med3;
3874 }
3875
3876 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
3877 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
3878 return Med3;
3879 }
3880
3881 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00003882 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
3883 (Opc == AMDGPUISD::FMIN_LEGACY &&
3884 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00003885 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
3886 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
3887 return Res;
3888 }
3889
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003890 return SDValue();
3891}
3892
Matt Arsenault46e6b7a2016-12-22 04:03:35 +00003893unsigned SITargetLowering::getFusedOpcode(const SelectionDAG &DAG,
3894 const SDNode *N0,
3895 const SDNode *N1) const {
3896 EVT VT = N0->getValueType(0);
3897
Matt Arsenault770ec862016-12-22 03:55:35 +00003898 // Only do this if we are not trying to support denormals. v_mad_f32 does not
3899 // support denormals ever.
3900 if ((VT == MVT::f32 && !Subtarget->hasFP32Denormals()) ||
3901 (VT == MVT::f16 && !Subtarget->hasFP16Denormals()))
3902 return ISD::FMAD;
3903
3904 const TargetOptions &Options = DAG.getTarget().Options;
Matt Arsenault46e6b7a2016-12-22 04:03:35 +00003905 if ((Options.AllowFPOpFusion == FPOpFusion::Fast ||
3906 Options.UnsafeFPMath ||
3907 (cast<BinaryWithFlagsSDNode>(N0)->Flags.hasUnsafeAlgebra() &&
3908 cast<BinaryWithFlagsSDNode>(N1)->Flags.hasUnsafeAlgebra())) &&
Matt Arsenault770ec862016-12-22 03:55:35 +00003909 isFMAFasterThanFMulAndFAdd(VT)) {
3910 return ISD::FMA;
3911 }
3912
3913 return 0;
3914}
3915
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003916SDValue SITargetLowering::performFAddCombine(SDNode *N,
3917 DAGCombinerInfo &DCI) const {
3918 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3919 return SDValue();
3920
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003921 SelectionDAG &DAG = DCI.DAG;
Matt Arsenault770ec862016-12-22 03:55:35 +00003922 EVT VT = N->getValueType(0);
3923 assert(!VT.isVector());
3924
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003925 SDLoc SL(N);
3926 SDValue LHS = N->getOperand(0);
3927 SDValue RHS = N->getOperand(1);
3928
3929 // These should really be instruction patterns, but writing patterns with
3930 // source modiifiers is a pain.
3931
3932 // fadd (fadd (a, a), b) -> mad 2.0, a, b
3933 if (LHS.getOpcode() == ISD::FADD) {
3934 SDValue A = LHS.getOperand(0);
3935 if (A == LHS.getOperand(1)) {
Matt Arsenault46e6b7a2016-12-22 04:03:35 +00003936 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode());
Matt Arsenault770ec862016-12-22 03:55:35 +00003937 if (FusedOp != 0) {
3938 const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
Matt Arsenaulte7d8ed32016-12-22 04:03:40 +00003939 return DAG.getNode(FusedOp, SL, VT, A, Two, RHS);
Matt Arsenault770ec862016-12-22 03:55:35 +00003940 }
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003941 }
3942 }
3943
3944 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
3945 if (RHS.getOpcode() == ISD::FADD) {
3946 SDValue A = RHS.getOperand(0);
3947 if (A == RHS.getOperand(1)) {
Matt Arsenault46e6b7a2016-12-22 04:03:35 +00003948 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode());
Matt Arsenault770ec862016-12-22 03:55:35 +00003949 if (FusedOp != 0) {
3950 const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
Matt Arsenaulte7d8ed32016-12-22 04:03:40 +00003951 return DAG.getNode(FusedOp, SL, VT, A, Two, LHS);
Matt Arsenault770ec862016-12-22 03:55:35 +00003952 }
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003953 }
3954 }
3955
3956 return SDValue();
3957}
3958
3959SDValue SITargetLowering::performFSubCombine(SDNode *N,
3960 DAGCombinerInfo &DCI) const {
3961 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3962 return SDValue();
3963
3964 SelectionDAG &DAG = DCI.DAG;
3965 SDLoc SL(N);
3966 EVT VT = N->getValueType(0);
3967 assert(!VT.isVector());
3968
3969 // Try to get the fneg to fold into the source modifier. This undoes generic
3970 // DAG combines and folds them into the mad.
3971 //
3972 // Only do this if we are not trying to support denormals. v_mad_f32 does
3973 // not support denormals ever.
Matt Arsenault770ec862016-12-22 03:55:35 +00003974 SDValue LHS = N->getOperand(0);
3975 SDValue RHS = N->getOperand(1);
3976 if (LHS.getOpcode() == ISD::FADD) {
3977 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
3978 SDValue A = LHS.getOperand(0);
3979 if (A == LHS.getOperand(1)) {
Matt Arsenault46e6b7a2016-12-22 04:03:35 +00003980 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode());
Matt Arsenault770ec862016-12-22 03:55:35 +00003981 if (FusedOp != 0){
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003982 const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
3983 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3984
Matt Arsenaulte7d8ed32016-12-22 04:03:40 +00003985 return DAG.getNode(FusedOp, SL, VT, A, Two, NegRHS);
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003986 }
3987 }
Matt Arsenault770ec862016-12-22 03:55:35 +00003988 }
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003989
Matt Arsenault770ec862016-12-22 03:55:35 +00003990 if (RHS.getOpcode() == ISD::FADD) {
3991 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003992
Matt Arsenault770ec862016-12-22 03:55:35 +00003993 SDValue A = RHS.getOperand(0);
3994 if (A == RHS.getOperand(1)) {
Matt Arsenault46e6b7a2016-12-22 04:03:35 +00003995 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode());
Matt Arsenault770ec862016-12-22 03:55:35 +00003996 if (FusedOp != 0){
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003997 const SDValue NegTwo = DAG.getConstantFP(-2.0, SL, VT);
Matt Arsenaulte7d8ed32016-12-22 04:03:40 +00003998 return DAG.getNode(FusedOp, SL, VT, A, NegTwo, LHS);
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003999 }
4000 }
4001 }
4002
4003 return SDValue();
4004}
4005
Matt Arsenault6f6233d2015-01-06 23:00:41 +00004006SDValue SITargetLowering::performSetCCCombine(SDNode *N,
4007 DAGCombinerInfo &DCI) const {
4008 SelectionDAG &DAG = DCI.DAG;
4009 SDLoc SL(N);
4010
4011 SDValue LHS = N->getOperand(0);
4012 SDValue RHS = N->getOperand(1);
4013 EVT VT = LHS.getValueType();
4014
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00004015 if (VT != MVT::f32 && VT != MVT::f64 && (Subtarget->has16BitInsts() &&
4016 VT != MVT::f16))
Matt Arsenault6f6233d2015-01-06 23:00:41 +00004017 return SDValue();
4018
4019 // Match isinf pattern
4020 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
4021 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
4022 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
4023 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
4024 if (!CRHS)
4025 return SDValue();
4026
4027 const APFloat &APF = CRHS->getValueAPF();
4028 if (APF.isInfinity() && !APF.isNegative()) {
4029 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004030 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
4031 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00004032 }
4033 }
4034
4035 return SDValue();
4036}
4037
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00004038SDValue SITargetLowering::performCvtF32UByteNCombine(SDNode *N,
4039 DAGCombinerInfo &DCI) const {
4040 SelectionDAG &DAG = DCI.DAG;
4041 SDLoc SL(N);
4042 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
4043
4044 SDValue Src = N->getOperand(0);
4045 SDValue Srl = N->getOperand(0);
4046 if (Srl.getOpcode() == ISD::ZERO_EXTEND)
4047 Srl = Srl.getOperand(0);
4048
4049 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero.
4050 if (Srl.getOpcode() == ISD::SRL) {
4051 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
4052 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
4053 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
4054
4055 if (const ConstantSDNode *C =
4056 dyn_cast<ConstantSDNode>(Srl.getOperand(1))) {
4057 Srl = DAG.getZExtOrTrunc(Srl.getOperand(0), SDLoc(Srl.getOperand(0)),
4058 EVT(MVT::i32));
4059
4060 unsigned SrcOffset = C->getZExtValue() + 8 * Offset;
4061 if (SrcOffset < 32 && SrcOffset % 8 == 0) {
4062 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, SL,
4063 MVT::f32, Srl);
4064 }
4065 }
4066 }
4067
4068 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
4069
4070 APInt KnownZero, KnownOne;
4071 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
4072 !DCI.isBeforeLegalizeOps());
4073 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4074 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
4075 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
4076 DCI.CommitTargetLoweringOpt(TLO);
4077 }
4078
4079 return SDValue();
4080}
4081
Tom Stellard75aadc22012-12-11 21:25:42 +00004082SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
4083 DAGCombinerInfo &DCI) const {
Tom Stellard75aadc22012-12-11 21:25:42 +00004084 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00004085 default:
4086 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00004087 case ISD::FADD:
4088 return performFAddCombine(N, DCI);
4089 case ISD::FSUB:
4090 return performFSubCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00004091 case ISD::SETCC:
4092 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00004093 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00004094 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00004095 case ISD::SMAX:
4096 case ISD::SMIN:
4097 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00004098 case ISD::UMIN:
4099 case AMDGPUISD::FMIN_LEGACY:
4100 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00004101 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00004102 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00004103 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00004104 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00004105 break;
4106 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00004107 case ISD::LOAD:
4108 case ISD::STORE:
4109 case ISD::ATOMIC_LOAD:
4110 case ISD::ATOMIC_STORE:
4111 case ISD::ATOMIC_CMP_SWAP:
4112 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
4113 case ISD::ATOMIC_SWAP:
4114 case ISD::ATOMIC_LOAD_ADD:
4115 case ISD::ATOMIC_LOAD_SUB:
4116 case ISD::ATOMIC_LOAD_AND:
4117 case ISD::ATOMIC_LOAD_OR:
4118 case ISD::ATOMIC_LOAD_XOR:
4119 case ISD::ATOMIC_LOAD_NAND:
4120 case ISD::ATOMIC_LOAD_MIN:
4121 case ISD::ATOMIC_LOAD_MAX:
4122 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00004123 case ISD::ATOMIC_LOAD_UMAX:
4124 case AMDGPUISD::ATOMIC_INC:
4125 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00004126 if (DCI.isBeforeLegalize())
4127 break;
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00004128 return performMemSDNodeCombine(cast<MemSDNode>(N), DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00004129 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00004130 case ISD::AND:
4131 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00004132 case ISD::OR:
4133 return performOrCombine(N, DCI);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00004134 case ISD::XOR:
4135 return performXorCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00004136 case AMDGPUISD::FP_CLASS:
4137 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00004138 case ISD::FCANONICALIZE:
4139 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00004140 case AMDGPUISD::FRACT:
4141 case AMDGPUISD::RCP:
4142 case AMDGPUISD::RSQ:
Matt Arsenault32fc5272016-07-26 16:45:45 +00004143 case AMDGPUISD::RCP_LEGACY:
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00004144 case AMDGPUISD::RSQ_LEGACY:
4145 case AMDGPUISD::RSQ_CLAMP:
4146 case AMDGPUISD::LDEXP: {
4147 SDValue Src = N->getOperand(0);
4148 if (Src.isUndef())
4149 return Src;
4150 break;
4151 }
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00004152 case ISD::SINT_TO_FP:
4153 case ISD::UINT_TO_FP:
4154 return performUCharToFloatCombine(N, DCI);
4155 case AMDGPUISD::CVT_F32_UBYTE0:
4156 case AMDGPUISD::CVT_F32_UBYTE1:
4157 case AMDGPUISD::CVT_F32_UBYTE2:
4158 case AMDGPUISD::CVT_F32_UBYTE3:
4159 return performCvtF32UByteNCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00004160 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00004161 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00004162}
Christian Konigd910b7d2013-02-26 17:52:16 +00004163
Christian Konig8e06e2a2013-04-10 08:39:08 +00004164/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00004165static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00004166 switch (Idx) {
4167 default: return 0;
4168 case AMDGPU::sub0: return 0;
4169 case AMDGPU::sub1: return 1;
4170 case AMDGPU::sub2: return 2;
4171 case AMDGPU::sub3: return 3;
4172 }
4173}
4174
4175/// \brief Adjust the writemask of MIMG instructions
4176void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
4177 SelectionDAG &DAG) const {
4178 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00004179 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00004180 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
4181 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00004182 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00004183
4184 // Try to figure out the used register components
4185 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
4186 I != E; ++I) {
4187
4188 // Abort if we can't understand the usage
4189 if (!I->isMachineOpcode() ||
4190 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
4191 return;
4192
Tom Stellard54774e52013-10-23 02:53:47 +00004193 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
4194 // Note that subregs are packed, i.e. Lane==0 is the first bit set
4195 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
4196 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00004197 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00004198
Tom Stellard54774e52013-10-23 02:53:47 +00004199 // Set which texture component corresponds to the lane.
4200 unsigned Comp;
4201 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
4202 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00004203 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00004204 Dmask &= ~(1 << Comp);
4205 }
4206
Christian Konig8e06e2a2013-04-10 08:39:08 +00004207 // Abort if we have more than one user per component
4208 if (Users[Lane])
4209 return;
4210
4211 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00004212 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00004213 }
4214
Tom Stellard54774e52013-10-23 02:53:47 +00004215 // Abort if there's no change
4216 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00004217 return;
4218
4219 // Adjust the writemask in the node
4220 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00004221 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004222 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00004223 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004224 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00004225
Christian Konig8b1ed282013-04-10 08:39:16 +00004226 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00004227 // (if NewDmask has only one bit set...)
4228 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004229 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
4230 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00004231 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004232 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00004233 SDValue(Node, 0), RC);
4234 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
4235 return;
4236 }
4237
Christian Konig8e06e2a2013-04-10 08:39:08 +00004238 // Update the users of the node with the new indices
4239 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
4240
4241 SDNode *User = Users[i];
4242 if (!User)
4243 continue;
4244
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004245 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00004246 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
4247
4248 switch (Idx) {
4249 default: break;
4250 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
4251 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
4252 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
4253 }
4254 }
4255}
4256
Tom Stellardc98ee202015-07-16 19:40:07 +00004257static bool isFrameIndexOp(SDValue Op) {
4258 if (Op.getOpcode() == ISD::AssertZext)
4259 Op = Op.getOperand(0);
4260
4261 return isa<FrameIndexSDNode>(Op);
4262}
4263
Tom Stellard3457a842014-10-09 19:06:00 +00004264/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
4265/// with frame index operands.
4266/// LLVM assumes that inputs are to these instructions are registers.
4267void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
4268 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00004269
4270 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00004271 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00004272 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00004273 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00004274 continue;
4275 }
4276
Tom Stellard3457a842014-10-09 19:06:00 +00004277 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00004278 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00004279 Node->getOperand(i).getValueType(),
4280 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00004281 }
4282
Tom Stellard3457a842014-10-09 19:06:00 +00004283 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00004284}
4285
Matt Arsenault08d84942014-06-03 23:06:13 +00004286/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00004287SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
4288 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00004289 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00004290 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00004291
Nicolai Haehnlec06bfa12016-07-11 21:59:43 +00004292 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() &&
4293 !TII->isGather4(Opcode))
Christian Konig8e06e2a2013-04-10 08:39:08 +00004294 adjustWritemask(Node, DAG);
4295
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00004296 if (Opcode == AMDGPU::INSERT_SUBREG ||
4297 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00004298 legalizeTargetIndependentNode(Node, DAG);
4299 return Node;
4300 }
Tom Stellard654d6692015-01-08 15:08:17 +00004301 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00004302}
Christian Konig8b1ed282013-04-10 08:39:16 +00004303
4304/// \brief Assign the register class depending on the number of
4305/// bits set in the writemask
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004306void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Christian Konig8b1ed282013-04-10 08:39:16 +00004307 SDNode *Node) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00004308 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004309
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004310 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00004311
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004312 if (TII->isVOP3(MI.getOpcode())) {
Matt Arsenault6005fcb2015-10-21 21:51:02 +00004313 // Make sure constant bus requirements are respected.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004314 TII->legalizeOperandsVOP3(MRI, MI);
Matt Arsenault6005fcb2015-10-21 21:51:02 +00004315 return;
4316 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00004317
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004318 if (TII->isMIMG(MI)) {
4319 unsigned VReg = MI.getOperand(0).getReg();
Changpeng Fang8236fe12016-11-14 18:33:18 +00004320 const TargetRegisterClass *RC = MRI.getRegClass(VReg);
4321 // TODO: Need mapping tables to handle other cases (register classes).
4322 if (RC != &AMDGPU::VReg_128RegClass)
4323 return;
4324
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004325 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4;
4326 unsigned Writemask = MI.getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004327 unsigned BitsSet = 0;
4328 for (unsigned i = 0; i < 4; ++i)
4329 BitsSet += Writemask & (1 << i) ? 1 : 0;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004330 switch (BitsSet) {
4331 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00004332 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004333 case 2: RC = &AMDGPU::VReg_64RegClass; break;
4334 case 3: RC = &AMDGPU::VReg_96RegClass; break;
4335 }
4336
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004337 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet);
4338 MI.setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004339 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00004340 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00004341 }
4342
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004343 // Replace unused atomics with the no return version.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004344 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004345 if (NoRetAtomicOp != -1) {
4346 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004347 MI.setDesc(TII->get(NoRetAtomicOp));
4348 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00004349 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004350 }
4351
Tom Stellard354a43c2016-04-01 18:27:37 +00004352 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
4353 // instruction, because the return type of these instructions is a vec2 of
4354 // the memory type, so it can be tied to the input operand.
4355 // This means these instructions always have a use, so we need to add a
4356 // special case to check if the atomic has only one extract_subreg use,
4357 // which itself has no uses.
4358 if ((Node->hasNUsesOfValue(1, 0) &&
Nicolai Haehnle750082d2016-04-15 14:42:36 +00004359 Node->use_begin()->isMachineOpcode() &&
Tom Stellard354a43c2016-04-01 18:27:37 +00004360 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
4361 !Node->use_begin()->hasAnyUseOfValue(0))) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004362 unsigned Def = MI.getOperand(0).getReg();
Tom Stellard354a43c2016-04-01 18:27:37 +00004363
4364 // Change this into a noret atomic.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004365 MI.setDesc(TII->get(NoRetAtomicOp));
4366 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00004367
4368 // If we only remove the def operand from the atomic instruction, the
4369 // extract_subreg will be left with a use of a vreg without a def.
4370 // So we need to insert an implicit_def to avoid machine verifier
4371 // errors.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004372 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
Tom Stellard354a43c2016-04-01 18:27:37 +00004373 TII->get(AMDGPU::IMPLICIT_DEF), Def);
4374 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004375 return;
4376 }
Christian Konig8b1ed282013-04-10 08:39:16 +00004377}
Tom Stellard0518ff82013-06-03 17:39:58 +00004378
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004379static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL,
4380 uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004381 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00004382 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
4383}
4384
4385MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004386 const SDLoc &DL,
Matt Arsenault485defe2014-11-05 19:01:17 +00004387 SDValue Ptr) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00004388 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault485defe2014-11-05 19:01:17 +00004389
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004390 // Build the half of the subregister with the constants before building the
4391 // full 128-bit register. If we are building multiple resource descriptors,
4392 // this will allow CSEing of the 2-component register.
4393 const SDValue Ops0[] = {
4394 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
4395 buildSMovImm32(DAG, DL, 0),
4396 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
4397 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
4398 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
4399 };
Matt Arsenault485defe2014-11-05 19:01:17 +00004400
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004401 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
4402 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00004403
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004404 // Combine the constants and the pointer.
4405 const SDValue Ops1[] = {
4406 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
4407 Ptr,
4408 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
4409 SubRegHi,
4410 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
4411 };
Matt Arsenault485defe2014-11-05 19:01:17 +00004412
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004413 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00004414}
4415
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004416/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00004417/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
4418/// of the resource descriptor) to create an offset, which is added to
4419/// the resource pointer.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004420MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL,
4421 SDValue Ptr, uint32_t RsrcDword1,
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004422 uint64_t RsrcDword2And3) const {
4423 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
4424 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
4425 if (RsrcDword1) {
4426 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004427 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
4428 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004429 }
4430
4431 SDValue DataLo = buildSMovImm32(DAG, DL,
4432 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
4433 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
4434
4435 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004436 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004437 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004438 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004439 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004440 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004441 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004442 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004443 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004444 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004445 };
4446
4447 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
4448}
4449
Tom Stellard94593ee2013-06-03 17:40:18 +00004450SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
4451 const TargetRegisterClass *RC,
4452 unsigned Reg, EVT VT) const {
4453 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
4454
4455 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
4456 cast<RegisterSDNode>(VReg)->getReg(), VT);
4457}
Tom Stellardd7e6f132015-04-08 01:09:26 +00004458
4459//===----------------------------------------------------------------------===//
4460// SI Inline Assembly Support
4461//===----------------------------------------------------------------------===//
4462
4463std::pair<unsigned, const TargetRegisterClass *>
4464SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00004465 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00004466 MVT VT) const {
Matt Arsenault742deb22016-11-18 04:42:57 +00004467 if (!isTypeLegal(VT))
4468 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004469
4470 if (Constraint.size() == 1) {
4471 switch (Constraint[0]) {
4472 case 's':
4473 case 'r':
4474 switch (VT.getSizeInBits()) {
4475 default:
4476 return std::make_pair(0U, nullptr);
4477 case 32:
Matt Arsenault9e910142016-12-20 19:06:12 +00004478 case 16:
Marek Olsak79c05872016-11-25 17:37:09 +00004479 return std::make_pair(0U, &AMDGPU::SReg_32_XM0RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004480 case 64:
4481 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
4482 case 128:
4483 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
4484 case 256:
4485 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
4486 }
4487
4488 case 'v':
4489 switch (VT.getSizeInBits()) {
4490 default:
4491 return std::make_pair(0U, nullptr);
4492 case 32:
Matt Arsenault9e910142016-12-20 19:06:12 +00004493 case 16:
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004494 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
4495 case 64:
4496 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
4497 case 96:
4498 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
4499 case 128:
4500 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
4501 case 256:
4502 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
4503 case 512:
4504 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
4505 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00004506 }
4507 }
4508
4509 if (Constraint.size() > 1) {
4510 const TargetRegisterClass *RC = nullptr;
4511 if (Constraint[1] == 'v') {
4512 RC = &AMDGPU::VGPR_32RegClass;
4513 } else if (Constraint[1] == 's') {
4514 RC = &AMDGPU::SGPR_32RegClass;
4515 }
4516
4517 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00004518 uint32_t Idx;
4519 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
4520 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00004521 return std::make_pair(RC->getRegister(Idx), RC);
4522 }
4523 }
4524 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
4525}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004526
4527SITargetLowering::ConstraintType
4528SITargetLowering::getConstraintType(StringRef Constraint) const {
4529 if (Constraint.size() == 1) {
4530 switch (Constraint[0]) {
4531 default: break;
4532 case 's':
4533 case 'v':
4534 return C_RegisterClass;
4535 }
4536 }
4537 return TargetLowering::getConstraintType(Constraint);
4538}