blob: eddec320c02eef322375ea466bbeac2b4c53da5b [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
Matt Arsenault71e66762016-05-21 02:27:49 +0000102 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
103 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000104 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
105
106 setOperationAction(ISD::SELECT, MVT::i1, Promote);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000107 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +0000108 setOperationAction(ISD::SELECT, MVT::f64, Promote);
109 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000110
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000111 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
112 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
113 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
114 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Matt Arsenault71e66762016-05-21 02:27:49 +0000115 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000116
Tom Stellardd1efda82016-01-20 21:48:24 +0000117 setOperationAction(ISD::SETCC, MVT::i1, Promote);
Tom Stellard83747202013-07-18 21:43:53 +0000118 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
119 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
120
Matt Arsenault71e66762016-05-21 02:27:49 +0000121 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
122 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000123
Matt Arsenault4e466652014-04-16 01:41:30 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
125 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
127 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000128 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
129 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000130 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
131
Tom Stellard9fa17912013-08-14 23:24:45 +0000132 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000133 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000134 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
135
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000136 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000137 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000138 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
139 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
140 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
141 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000142
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000143 // We only support LOAD/STORE and vector manipulation ops for vectors
144 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000145 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000146 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000147 switch (Op) {
Tom Stellard967bf582014-02-13 23:34:15 +0000148 case ISD::LOAD:
149 case ISD::STORE:
150 case ISD::BUILD_VECTOR:
151 case ISD::BITCAST:
152 case ISD::EXTRACT_VECTOR_ELT:
153 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000154 case ISD::INSERT_SUBVECTOR:
155 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000156 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000157 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000158 case ISD::CONCAT_VECTORS:
159 setOperationAction(Op, VT, Custom);
160 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000161 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000162 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000163 break;
164 }
165 }
166 }
167
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000168 // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that
169 // is expanded to avoid having two separate loops in case the index is a VGPR.
170
Matt Arsenault61001bb2015-11-25 19:58:34 +0000171 // Most operations are naturally 32-bit vector operations. We only support
172 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
173 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
174 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
175 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
176
177 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
178 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
179
180 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
181 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
182
183 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
184 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
185 }
186
Matt Arsenault71e66762016-05-21 02:27:49 +0000187 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
188 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
189 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
190 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000191
Tom Stellard354a43c2016-04-01 18:27:37 +0000192 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
193 // and output demarshalling
194 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
195 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
196
197 // We can't return success/failure, only the old value,
198 // let LLVM add the comparison
199 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand);
200 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand);
201
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000202 if (getSubtarget()->hasFlatAddressSpace()) {
Matt Arsenault99c14522016-04-25 19:27:24 +0000203 setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom);
204 setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom);
205 }
206
Matt Arsenault71e66762016-05-21 02:27:49 +0000207 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
208 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
209
210 // On SI this is s_memtime and s_memrealtime on VI.
211 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
Matt Arsenault0bb294b2016-06-17 22:27:03 +0000212 setOperationAction(ISD::TRAP, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000213
214 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
215 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
216
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000217 if (Subtarget->getGeneration() >= SISubtarget::SEA_ISLANDS) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000218 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
219 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
220 setOperationAction(ISD::FRINT, MVT::f64, Legal);
221 }
222
223 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
224
225 setOperationAction(ISD::FSIN, MVT::f32, Custom);
226 setOperationAction(ISD::FCOS, MVT::f32, Custom);
227 setOperationAction(ISD::FDIV, MVT::f32, Custom);
228 setOperationAction(ISD::FDIV, MVT::f64, Custom);
229
Tom Stellard115a6152016-11-10 16:02:37 +0000230 if (Subtarget->has16BitInsts()) {
231 setOperationAction(ISD::Constant, MVT::i16, Legal);
232
233 setOperationAction(ISD::SMIN, MVT::i16, Legal);
234 setOperationAction(ISD::SMAX, MVT::i16, Legal);
235
236 setOperationAction(ISD::UMIN, MVT::i16, Legal);
237 setOperationAction(ISD::UMAX, MVT::i16, Legal);
238
239 setOperationAction(ISD::SETCC, MVT::i16, Promote);
240 AddPromotedToType(ISD::SETCC, MVT::i16, MVT::i32);
241
242 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Promote);
243 AddPromotedToType(ISD::SIGN_EXTEND, MVT::i16, MVT::i32);
244
245 setOperationAction(ISD::ROTR, MVT::i16, Promote);
246 setOperationAction(ISD::ROTL, MVT::i16, Promote);
247
248 setOperationAction(ISD::SDIV, MVT::i16, Promote);
249 setOperationAction(ISD::UDIV, MVT::i16, Promote);
250 setOperationAction(ISD::SREM, MVT::i16, Promote);
251 setOperationAction(ISD::UREM, MVT::i16, Promote);
252
253 setOperationAction(ISD::BSWAP, MVT::i16, Promote);
254 setOperationAction(ISD::BITREVERSE, MVT::i16, Promote);
255
256 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
257 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Promote);
258 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
259 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Promote);
260
261 setOperationAction(ISD::SELECT_CC, MVT::i16, Expand);
262
263 setOperationAction(ISD::BR_CC, MVT::i16, Expand);
264
265 setOperationAction(ISD::LOAD, MVT::i16, Custom);
266
267 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
268
Tom Stellard115a6152016-11-10 16:02:37 +0000269 setOperationAction(ISD::FP16_TO_FP, MVT::i16, Promote);
270 AddPromotedToType(ISD::FP16_TO_FP, MVT::i16, MVT::i32);
271 setOperationAction(ISD::FP_TO_FP16, MVT::i16, Promote);
272 AddPromotedToType(ISD::FP_TO_FP16, MVT::i16, MVT::i32);
Tom Stellardb4c8e8e2016-11-12 00:19:11 +0000273
Konstantin Zhuravlyov3f0cdc72016-11-17 04:00:46 +0000274 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
275 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
276 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
277 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
Tom Stellardb4c8e8e2016-11-12 00:19:11 +0000278
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000279 // F16 - Constant Actions.
Matt Arsenaulte96d0372016-12-08 20:14:46 +0000280 setOperationAction(ISD::ConstantFP, MVT::f16, Legal);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000281
282 // F16 - Load/Store Actions.
283 setOperationAction(ISD::LOAD, MVT::f16, Promote);
284 AddPromotedToType(ISD::LOAD, MVT::f16, MVT::i16);
285 setOperationAction(ISD::STORE, MVT::f16, Promote);
286 AddPromotedToType(ISD::STORE, MVT::f16, MVT::i16);
287
288 // F16 - VOP1 Actions.
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +0000289 setOperationAction(ISD::FP_ROUND, MVT::f16, Custom);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000290 setOperationAction(ISD::FCOS, MVT::f16, Promote);
291 setOperationAction(ISD::FSIN, MVT::f16, Promote);
Konstantin Zhuravlyov3f0cdc72016-11-17 04:00:46 +0000292 setOperationAction(ISD::FP_TO_SINT, MVT::f16, Promote);
293 setOperationAction(ISD::FP_TO_UINT, MVT::f16, Promote);
294 setOperationAction(ISD::SINT_TO_FP, MVT::f16, Promote);
295 setOperationAction(ISD::UINT_TO_FP, MVT::f16, Promote);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000296
297 // F16 - VOP2 Actions.
Konstantin Zhuravlyov662e01d2016-11-17 03:49:01 +0000298 setOperationAction(ISD::BR_CC, MVT::f16, Expand);
Konstantin Zhuravlyov2a87a422016-11-16 03:16:26 +0000299 setOperationAction(ISD::SELECT_CC, MVT::f16, Expand);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000300 setOperationAction(ISD::FMAXNUM, MVT::f16, Legal);
301 setOperationAction(ISD::FMINNUM, MVT::f16, Legal);
Matt Arsenault4052a572016-12-22 03:05:41 +0000302 setOperationAction(ISD::FDIV, MVT::f16, Custom);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000303
304 // F16 - VOP3 Actions.
305 setOperationAction(ISD::FMA, MVT::f16, Legal);
306 if (!Subtarget->hasFP16Denormals())
307 setOperationAction(ISD::FMAD, MVT::f16, Legal);
Tom Stellard115a6152016-11-10 16:02:37 +0000308 }
309
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000310 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000311 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000312 setTargetDAGCombine(ISD::FMINNUM);
313 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000314 setTargetDAGCombine(ISD::SMIN);
315 setTargetDAGCombine(ISD::SMAX);
316 setTargetDAGCombine(ISD::UMIN);
317 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000318 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000319 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000320 setTargetDAGCombine(ISD::OR);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000321 setTargetDAGCombine(ISD::XOR);
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +0000322 setTargetDAGCombine(ISD::SINT_TO_FP);
Matt Arsenault364a6742014-06-11 17:50:44 +0000323 setTargetDAGCombine(ISD::UINT_TO_FP);
Matt Arsenault9cd90712016-04-14 01:42:16 +0000324 setTargetDAGCombine(ISD::FCANONICALIZE);
Matt Arsenault364a6742014-06-11 17:50:44 +0000325
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000326 // All memory operations. Some folding on the pointer operand is done to help
327 // matching the constant offsets in the addressing modes.
328 setTargetDAGCombine(ISD::LOAD);
329 setTargetDAGCombine(ISD::STORE);
330 setTargetDAGCombine(ISD::ATOMIC_LOAD);
331 setTargetDAGCombine(ISD::ATOMIC_STORE);
332 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
333 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
334 setTargetDAGCombine(ISD::ATOMIC_SWAP);
335 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
336 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
337 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
338 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
339 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
340 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
341 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
342 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
343 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
344 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
345
Christian Konigeecebd02013-03-26 14:04:02 +0000346 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000347}
348
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000349const SISubtarget *SITargetLowering::getSubtarget() const {
350 return static_cast<const SISubtarget *>(Subtarget);
351}
352
Tom Stellard0125f2a2013-06-25 02:39:35 +0000353//===----------------------------------------------------------------------===//
354// TargetLowering queries
355//===----------------------------------------------------------------------===//
356
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000357bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
358 const CallInst &CI,
359 unsigned IntrID) const {
360 switch (IntrID) {
361 case Intrinsic::amdgcn_atomic_inc:
362 case Intrinsic::amdgcn_atomic_dec:
363 Info.opc = ISD::INTRINSIC_W_CHAIN;
364 Info.memVT = MVT::getVT(CI.getType());
365 Info.ptrVal = CI.getOperand(0);
366 Info.align = 0;
367 Info.vol = false;
368 Info.readMem = true;
369 Info.writeMem = true;
370 return true;
371 default:
372 return false;
373 }
374}
375
Matt Arsenaulte306a322014-10-21 16:25:08 +0000376bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
377 EVT) const {
378 // SI has some legal vector types, but no legal vector operations. Say no
379 // shuffles are legal in order to prefer scalarizing some vector operations.
380 return false;
381}
382
Tom Stellard70580f82015-07-20 14:28:41 +0000383bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
384 // Flat instructions do not have offsets, and only have the register
385 // address.
386 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
387}
388
Matt Arsenault711b3902015-08-07 20:18:34 +0000389bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
390 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
391 // additionally can do r + r + i with addr64. 32-bit has more addressing
392 // mode options. Depending on the resource constant, it can also do
393 // (i64 r0) + (i32 r1) * (i14 i).
394 //
395 // Private arrays end up using a scratch buffer most of the time, so also
396 // assume those use MUBUF instructions. Scratch loads / stores are currently
397 // implemented as mubuf instructions with offen bit set, so slightly
398 // different than the normal addr64.
399 if (!isUInt<12>(AM.BaseOffs))
400 return false;
401
402 // FIXME: Since we can split immediate into soffset and immediate offset,
403 // would it make sense to allow any immediate?
404
405 switch (AM.Scale) {
406 case 0: // r + i or just i, depending on HasBaseReg.
407 return true;
408 case 1:
409 return true; // We have r + r or r + i.
410 case 2:
411 if (AM.HasBaseReg) {
412 // Reject 2 * r + r.
413 return false;
414 }
415
416 // Allow 2 * r as r + r
417 // Or 2 * r + i is allowed as r + r + i.
418 return true;
419 default: // Don't allow n * r
420 return false;
421 }
422}
423
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000424bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
425 const AddrMode &AM, Type *Ty,
426 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000427 // No global is ever allowed as a base.
428 if (AM.BaseGV)
429 return false;
430
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000431 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000432 case AMDGPUAS::GLOBAL_ADDRESS: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000433 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) {
Tom Stellard70580f82015-07-20 14:28:41 +0000434 // Assume the we will use FLAT for all global memory accesses
435 // on VI.
436 // FIXME: This assumption is currently wrong. On VI we still use
437 // MUBUF instructions for the r + i addressing mode. As currently
438 // implemented, the MUBUF instructions only work on buffer < 4GB.
439 // It may be possible to support > 4GB buffers with MUBUF instructions,
440 // by setting the stride value in the resource descriptor which would
441 // increase the size limit to (stride * 4GB). However, this is risky,
442 // because it has never been validated.
443 return isLegalFlatAddressingMode(AM);
444 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000445
Matt Arsenault711b3902015-08-07 20:18:34 +0000446 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000447 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000448 case AMDGPUAS::CONSTANT_ADDRESS: {
449 // If the offset isn't a multiple of 4, it probably isn't going to be
450 // correctly aligned.
Matt Arsenault3cc1e002016-08-13 01:43:51 +0000451 // FIXME: Can we get the real alignment here?
Matt Arsenault711b3902015-08-07 20:18:34 +0000452 if (AM.BaseOffs % 4 != 0)
453 return isLegalMUBUFAddressingMode(AM);
454
455 // There are no SMRD extloads, so if we have to do a small type access we
456 // will use a MUBUF load.
457 // FIXME?: We also need to do this if unaligned, but we don't know the
458 // alignment here.
459 if (DL.getTypeStoreSize(Ty) < 4)
460 return isLegalMUBUFAddressingMode(AM);
461
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000462 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000463 // SMRD instructions have an 8-bit, dword offset on SI.
464 if (!isUInt<8>(AM.BaseOffs / 4))
465 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000466 } else if (Subtarget->getGeneration() == SISubtarget::SEA_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000467 // On CI+, this can also be a 32-bit literal constant offset. If it fits
468 // in 8-bits, it can use a smaller encoding.
469 if (!isUInt<32>(AM.BaseOffs / 4))
470 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000471 } else if (Subtarget->getGeneration() == SISubtarget::VOLCANIC_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000472 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
473 if (!isUInt<20>(AM.BaseOffs))
474 return false;
475 } else
476 llvm_unreachable("unhandled generation");
477
478 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
479 return true;
480
481 if (AM.Scale == 1 && AM.HasBaseReg)
482 return true;
483
484 return false;
485 }
486
487 case AMDGPUAS::PRIVATE_ADDRESS:
Matt Arsenault711b3902015-08-07 20:18:34 +0000488 return isLegalMUBUFAddressingMode(AM);
489
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000490 case AMDGPUAS::LOCAL_ADDRESS:
491 case AMDGPUAS::REGION_ADDRESS: {
492 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
493 // field.
494 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
495 // an 8-bit dword offset but we don't know the alignment here.
496 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000497 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000498
499 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
500 return true;
501
502 if (AM.Scale == 1 && AM.HasBaseReg)
503 return true;
504
Matt Arsenault5015a892014-08-15 17:17:07 +0000505 return false;
506 }
Tom Stellard70580f82015-07-20 14:28:41 +0000507 case AMDGPUAS::FLAT_ADDRESS:
Matt Arsenault7d1b6c82016-04-29 06:25:10 +0000508 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
509 // For an unknown address space, this usually means that this is for some
510 // reason being used for pure arithmetic, and not based on some addressing
511 // computation. We don't have instructions that compute pointers with any
512 // addressing modes, so treat them as having no offset like flat
513 // instructions.
Tom Stellard70580f82015-07-20 14:28:41 +0000514 return isLegalFlatAddressingMode(AM);
515
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000516 default:
517 llvm_unreachable("unhandled address space");
518 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000519}
520
Matt Arsenaulte6986632015-01-14 01:35:22 +0000521bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000522 unsigned AddrSpace,
523 unsigned Align,
524 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000525 if (IsFast)
526 *IsFast = false;
527
Matt Arsenault1018c892014-04-24 17:08:26 +0000528 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
529 // which isn't a simple VT.
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000530 // Until MVT is extended to handle this, simply check for the size and
531 // rely on the condition below: allow accesses if the size is a multiple of 4.
532 if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 &&
533 VT.getStoreSize() > 16)) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000534 return false;
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000535 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000536
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000537 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
538 AddrSpace == AMDGPUAS::REGION_ADDRESS) {
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000539 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
540 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
541 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000542 bool AlignedBy4 = (Align % 4 == 0);
543 if (IsFast)
544 *IsFast = AlignedBy4;
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000545
Sanjay Patelce74db92015-09-03 15:03:19 +0000546 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000547 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000548
Tom Stellard64a9d082016-10-14 18:10:39 +0000549 // FIXME: We have to be conservative here and assume that flat operations
550 // will access scratch. If we had access to the IR function, then we
551 // could determine if any private memory was used in the function.
552 if (!Subtarget->hasUnalignedScratchAccess() &&
553 (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS ||
554 AddrSpace == AMDGPUAS::FLAT_ADDRESS)) {
555 return false;
556 }
557
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000558 if (Subtarget->hasUnalignedBufferAccess()) {
559 // If we have an uniform constant load, it still requires using a slow
560 // buffer instruction if unaligned.
561 if (IsFast) {
562 *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) ?
563 (Align % 4 == 0) : true;
564 }
565
566 return true;
567 }
568
Tom Stellard33e64c62015-02-04 20:49:52 +0000569 // Smaller than dword value must be aligned.
Tom Stellard33e64c62015-02-04 20:49:52 +0000570 if (VT.bitsLT(MVT::i32))
571 return false;
572
Matt Arsenault1018c892014-04-24 17:08:26 +0000573 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
574 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000575 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000576 if (IsFast)
577 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000578
579 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000580}
581
Matt Arsenault46645fa2014-07-28 17:49:26 +0000582EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
583 unsigned SrcAlign, bool IsMemset,
584 bool ZeroMemset,
585 bool MemcpyStrSrc,
586 MachineFunction &MF) const {
587 // FIXME: Should account for address space here.
588
589 // The default fallback uses the private pointer size as a guess for a type to
590 // use. Make sure we switch these to 64-bit accesses.
591
592 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
593 return MVT::v4i32;
594
595 if (Size >= 8 && DstAlign >= 4)
596 return MVT::v2i32;
597
598 // Use the default.
599 return MVT::Other;
600}
601
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000602static bool isFlatGlobalAddrSpace(unsigned AS) {
603 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
Matt Arsenaultd4da0ed2016-12-02 18:12:53 +0000604 AS == AMDGPUAS::FLAT_ADDRESS ||
605 AS == AMDGPUAS::CONSTANT_ADDRESS;
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000606}
607
608bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
609 unsigned DestAS) const {
Matt Arsenault37fefd62016-06-10 02:18:02 +0000610 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000611}
612
Alexander Timofeev18009562016-12-08 17:28:47 +0000613bool SITargetLowering::isMemOpHasNoClobberedMemOperand(const SDNode *N) const {
614 const MemSDNode *MemNode = cast<MemSDNode>(N);
615 const Value *Ptr = MemNode->getMemOperand()->getValue();
616 const Instruction *I = dyn_cast<Instruction>(Ptr);
617 return I && I->getMetadata("amdgpu.noclobber");
618}
619
Matt Arsenaultd4da0ed2016-12-02 18:12:53 +0000620bool SITargetLowering::isCheapAddrSpaceCast(unsigned SrcAS,
621 unsigned DestAS) const {
622 // Flat -> private/local is a simple truncate.
623 // Flat -> global is no-op
624 if (SrcAS == AMDGPUAS::FLAT_ADDRESS)
625 return true;
626
627 return isNoopAddrSpaceCast(SrcAS, DestAS);
628}
629
Tom Stellarda6f24c62015-12-15 20:55:55 +0000630bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
631 const MemSDNode *MemNode = cast<MemSDNode>(N);
632 const Value *Ptr = MemNode->getMemOperand()->getValue();
633
634 // UndefValue means this is a load of a kernel input. These are uniform.
Tom Stellard418beb72016-07-13 14:23:33 +0000635 // Sometimes LDS instructions have constant pointers.
636 // If Ptr is null, then that means this mem operand contains a
637 // PseudoSourceValue like GOT.
638 if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) ||
639 isa<Constant>(Ptr) || isa<GlobalValue>(Ptr))
Tom Stellarda6f24c62015-12-15 20:55:55 +0000640 return true;
641
Tom Stellard418beb72016-07-13 14:23:33 +0000642 const Instruction *I = dyn_cast<Instruction>(Ptr);
Tom Stellarda6f24c62015-12-15 20:55:55 +0000643 return I && I->getMetadata("amdgpu.uniform");
644}
645
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000646TargetLoweringBase::LegalizeTypeAction
647SITargetLowering::getPreferredVectorAction(EVT VT) const {
648 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
649 return TypeSplitVector;
650
651 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000652}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000653
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000654bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
655 Type *Ty) const {
Matt Arsenault749035b2016-07-30 01:40:36 +0000656 // FIXME: Could be smarter if called for vector constants.
657 return true;
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000658}
659
Tom Stellard2e045bb2016-01-20 00:13:22 +0000660bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
Matt Arsenault7b00cf42016-12-09 17:57:43 +0000661 if (Subtarget->has16BitInsts() && VT == MVT::i16) {
662 switch (Op) {
663 case ISD::LOAD:
664 case ISD::STORE:
Tom Stellard2e045bb2016-01-20 00:13:22 +0000665
Matt Arsenault7b00cf42016-12-09 17:57:43 +0000666 // These operations are done with 32-bit instructions anyway.
667 case ISD::AND:
668 case ISD::OR:
669 case ISD::XOR:
670 case ISD::SELECT:
671 // TODO: Extensions?
672 return true;
673 default:
674 return false;
675 }
676 }
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000677
Tom Stellard2e045bb2016-01-20 00:13:22 +0000678 // SimplifySetCC uses this function to determine whether or not it should
679 // create setcc with i1 operands. We don't have instructions for i1 setcc.
680 if (VT == MVT::i1 && Op == ISD::SETCC)
681 return false;
682
683 return TargetLowering::isTypeDesirableForOp(Op, VT);
684}
685
Jan Veselyfea814d2016-06-21 20:46:20 +0000686SDValue SITargetLowering::LowerParameterPtr(SelectionDAG &DAG,
687 const SDLoc &SL, SDValue Chain,
688 unsigned Offset) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000689 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000690 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000691 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaultac234b62015-11-30 21:15:57 +0000692 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000693
Matt Arsenault86033ca2014-07-28 17:31:39 +0000694 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000695 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000696 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
697 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
Jan Veselyfea814d2016-06-21 20:46:20 +0000698 return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
699 DAG.getConstant(Offset, SL, PtrVT));
700}
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000701
Jan Veselyfea814d2016-06-21 20:46:20 +0000702SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
703 const SDLoc &SL, SDValue Chain,
704 unsigned Offset, bool Signed) const {
705 const DataLayout &DL = DAG.getDataLayout();
Tom Stellard083f1622016-10-17 16:56:19 +0000706 Type *Ty = MemVT.getTypeForEVT(*DAG.getContext());
Jan Veselyfea814d2016-06-21 20:46:20 +0000707 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000708 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
709
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000710 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000711
Jan Veselyfea814d2016-06-21 20:46:20 +0000712 SDValue Ptr = LowerParameterPtr(DAG, SL, Chain, Offset);
Tom Stellardbc6c5232016-10-17 16:21:45 +0000713 SDValue Load = DAG.getLoad(MemVT, SL, Chain, Ptr, PtrInfo, Align,
714 MachineMemOperand::MONonTemporal |
715 MachineMemOperand::MODereferenceable |
716 MachineMemOperand::MOInvariant);
717
718 SDValue Val;
719 if (MemVT.isFloatingPoint())
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000720 Val = getFPExtOrFPTrunc(DAG, Load, SL, VT);
Tom Stellardbc6c5232016-10-17 16:21:45 +0000721 else if (Signed)
722 Val = DAG.getSExtOrTrunc(Load, SL, VT);
723 else
724 Val = DAG.getZExtOrTrunc(Load, SL, VT);
725
726 SDValue Ops[] = {
727 Val,
728 Load.getValue(1)
729 };
730
731 return DAG.getMergeValues(Ops, SL);
Tom Stellard94593ee2013-06-03 17:40:18 +0000732}
733
Christian Konig2c8f6d52013-03-07 09:03:52 +0000734SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000735 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000736 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
737 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000738 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000739
740 MachineFunction &MF = DAG.getMachineFunction();
741 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000742 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000743 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000744
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000745 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) {
Matt Arsenaultd48da142015-11-02 23:23:02 +0000746 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000747 DiagnosticInfoUnsupported NoGraphicsHSA(
748 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000749 DAG.getContext()->diagnose(NoGraphicsHSA);
Diana Picus81bc3172016-05-26 15:24:55 +0000750 return DAG.getEntryNode();
Matt Arsenaultd48da142015-11-02 23:23:02 +0000751 }
752
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000753 // Create stack objects that are used for emitting debugger prologue if
754 // "amdgpu-debugger-emit-prologue" attribute was specified.
755 if (ST.debuggerEmitPrologue())
756 createDebuggerPrologueStackObjects(MF);
757
Christian Konig2c8f6d52013-03-07 09:03:52 +0000758 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000759 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000760
761 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000762 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000763
764 // First check if it's a PS input addr
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000765 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000766 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000767
Marek Olsakfccabaf2016-01-13 11:45:36 +0000768 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000769 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000770 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000771 ++PSInputNum;
772 continue;
773 }
774
Marek Olsakfccabaf2016-01-13 11:45:36 +0000775 Info->markPSInputAllocated(PSInputNum);
776 if (Arg.Used)
777 Info->PSInputEna |= 1 << PSInputNum;
778
779 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000780 }
781
Matt Arsenault539ca882016-05-05 20:27:02 +0000782 if (AMDGPU::isShader(CallConv)) {
783 // Second split vertices into their elements
784 if (Arg.VT.isVector()) {
785 ISD::InputArg NewArg = Arg;
786 NewArg.Flags.setSplit();
787 NewArg.VT = Arg.VT.getVectorElementType();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000788
Matt Arsenault539ca882016-05-05 20:27:02 +0000789 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
790 // three or five element vertex only needs three or five registers,
791 // NOT four or eight.
792 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
793 unsigned NumElements = ParamType->getVectorNumElements();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000794
Matt Arsenault539ca882016-05-05 20:27:02 +0000795 for (unsigned j = 0; j != NumElements; ++j) {
796 Splits.push_back(NewArg);
797 NewArg.PartOffset += NewArg.VT.getStoreSize();
798 }
799 } else {
800 Splits.push_back(Arg);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000801 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000802 }
803 }
804
805 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000806 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
807 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000808
Christian Konig99ee0f42013-03-07 09:04:14 +0000809 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000810 //
811 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
812 // PSInputAddr, the user wants to enable some bits after the compilation
813 // based on run-time states. Since we can't know what the final PSInputEna
814 // will look like, so we shouldn't do anything here and the user should take
815 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000816 //
817 // Otherwise, the following restrictions apply:
818 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
819 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
820 // enabled too.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000821 if (CallConv == CallingConv::AMDGPU_PS &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000822 ((Info->getPSInputAddr() & 0x7F) == 0 ||
NAKAMURA Takumife1202c2016-06-20 00:37:41 +0000823 ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000824 CCInfo.AllocateReg(AMDGPU::VGPR0);
825 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000826 Info->markPSInputAllocated(0);
827 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000828 }
829
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000830 if (!AMDGPU::isShader(CallConv)) {
Tom Stellardf110f8f2016-04-14 16:27:03 +0000831 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
832 } else {
833 assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() &&
834 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() &&
835 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
836 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
837 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
838 !Info->hasWorkItemIDZ());
Tom Stellardaf775432013-10-23 00:44:32 +0000839 }
840
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000841 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
842 if (Info->hasPrivateSegmentBuffer()) {
843 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
844 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
845 CCInfo.AllocateReg(PrivateSegmentBufferReg);
846 }
847
848 if (Info->hasDispatchPtr()) {
849 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000850 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000851 CCInfo.AllocateReg(DispatchPtrReg);
852 }
853
Matt Arsenault48ab5262016-04-25 19:27:18 +0000854 if (Info->hasQueuePtr()) {
855 unsigned QueuePtrReg = Info->addQueuePtr(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000856 MF.addLiveIn(QueuePtrReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault48ab5262016-04-25 19:27:18 +0000857 CCInfo.AllocateReg(QueuePtrReg);
858 }
859
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000860 if (Info->hasKernargSegmentPtr()) {
861 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000862 MF.addLiveIn(InputPtrReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000863 CCInfo.AllocateReg(InputPtrReg);
864 }
865
Matt Arsenault8d718dc2016-07-22 17:01:30 +0000866 if (Info->hasDispatchID()) {
867 unsigned DispatchIDReg = Info->addDispatchID(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000868 MF.addLiveIn(DispatchIDReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault8d718dc2016-07-22 17:01:30 +0000869 CCInfo.AllocateReg(DispatchIDReg);
870 }
871
Matt Arsenault296b8492016-02-12 06:31:30 +0000872 if (Info->hasFlatScratchInit()) {
873 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000874 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault296b8492016-02-12 06:31:30 +0000875 CCInfo.AllocateReg(FlatScratchInitReg);
876 }
877
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000878 if (!AMDGPU::isShader(CallConv))
879 analyzeFormalArgumentsCompute(CCInfo, Ins);
880 else
881 AnalyzeFormalArguments(CCInfo, Splits);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000882
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000883 SmallVector<SDValue, 16> Chains;
884
Christian Konig2c8f6d52013-03-07 09:03:52 +0000885 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
886
Christian Konigb7be72d2013-05-17 09:46:48 +0000887 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000888 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000889 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000890 continue;
891 }
892
Christian Konig2c8f6d52013-03-07 09:03:52 +0000893 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000894 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000895
896 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000897 VT = Ins[i].VT;
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000898 EVT MemVT = VA.getLocVT();
Tom Stellardb5798b02015-06-26 21:15:03 +0000899 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
900 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000901 // The first 36 bytes of the input buffer contains information about
902 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000903 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000904 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000905 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000906
Craig Toppere3dcce92015-08-01 22:20:21 +0000907 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000908 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000909 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Tom Stellardca7ecf32014-08-22 18:49:31 +0000910 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
911 // On SI local pointers are just offsets into LDS, so they are always
912 // less than 16-bits. On CI and newer they could potentially be
913 // real pointers, so we can't guarantee their size.
914 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
915 DAG.getValueType(MVT::i16));
916 }
917
Tom Stellarded882c22013-06-03 17:40:11 +0000918 InVals.push_back(Arg);
Matt Arsenault52ef4012016-07-26 16:45:58 +0000919 Info->setABIArgOffset(Offset + MemVT.getStoreSize());
Tom Stellarded882c22013-06-03 17:40:11 +0000920 continue;
921 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000922 assert(VA.isRegLoc() && "Parameter must be in a register!");
923
924 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000925
926 if (VT == MVT::i64) {
927 // For now assume it is a pointer
928 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000929 &AMDGPU::SGPR_64RegClass);
930 Reg = MF.addLiveIn(Reg, &AMDGPU::SGPR_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000931 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
932 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000933 continue;
934 }
935
936 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
937
938 Reg = MF.addLiveIn(Reg, RC);
939 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
940
Christian Konig2c8f6d52013-03-07 09:03:52 +0000941 if (Arg.VT.isVector()) {
942
943 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000944 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000945 unsigned NumElements = ParamType->getVectorNumElements();
946
947 SmallVector<SDValue, 4> Regs;
948 Regs.push_back(Val);
949 for (unsigned j = 1; j != NumElements; ++j) {
950 Reg = ArgLocs[ArgIdx++].getLocReg();
951 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000952
953 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
954 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000955 }
956
957 // Fill up the missing vector elements
958 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000959 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000960
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000961 InVals.push_back(DAG.getBuildVector(Arg.VT, DL, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000962 continue;
963 }
964
965 InVals.push_back(Val);
966 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000967
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000968 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
969 // these from the dispatch pointer.
970
971 // Start adding system SGPRs.
972 if (Info->hasWorkGroupIDX()) {
973 unsigned Reg = Info->addWorkGroupIDX();
Marek Olsak79c05872016-11-25 17:37:09 +0000974 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000975 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000976 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000977
978 if (Info->hasWorkGroupIDY()) {
979 unsigned Reg = Info->addWorkGroupIDY();
Marek Olsak79c05872016-11-25 17:37:09 +0000980 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000981 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000982 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000983
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000984 if (Info->hasWorkGroupIDZ()) {
985 unsigned Reg = Info->addWorkGroupIDZ();
Marek Olsak79c05872016-11-25 17:37:09 +0000986 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000987 CCInfo.AllocateReg(Reg);
988 }
989
990 if (Info->hasWorkGroupInfo()) {
991 unsigned Reg = Info->addWorkGroupInfo();
Marek Olsak79c05872016-11-25 17:37:09 +0000992 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000993 CCInfo.AllocateReg(Reg);
994 }
995
996 if (Info->hasPrivateSegmentWaveByteOffset()) {
997 // Scratch wave offset passed in system SGPR.
Tom Stellardf110f8f2016-04-14 16:27:03 +0000998 unsigned PrivateSegmentWaveByteOffsetReg;
999
1000 if (AMDGPU::isShader(CallConv)) {
1001 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
1002 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
1003 } else
1004 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset();
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001005
1006 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
1007 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
1008 }
1009
1010 // Now that we've figured out where the scratch register inputs are, see if
1011 // should reserve the arguments and use them directly.
Matthias Braun941a7052016-07-28 18:40:00 +00001012 bool HasStackObjects = MF.getFrameInfo().hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +00001013 // Record that we know we have non-spill stack objects so we don't need to
1014 // check all stack objects later.
1015 if (HasStackObjects)
1016 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001017
Matt Arsenault253640e2016-10-13 13:10:00 +00001018 // Everything live out of a block is spilled with fast regalloc, so it's
1019 // almost certain that spilling will be required.
1020 if (getTargetMachine().getOptLevel() == CodeGenOpt::None)
1021 HasStackObjects = true;
1022
Tom Stellard0b76fc4c2016-09-16 21:34:26 +00001023 if (ST.isAmdCodeObjectV2()) {
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001024 if (HasStackObjects) {
1025 // If we have stack objects, we unquestionably need the private buffer
Tom Stellard0b76fc4c2016-09-16 21:34:26 +00001026 // resource. For the Code Object V2 ABI, this will be the first 4 user
1027 // SGPR inputs. We can reserve those and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001028
1029 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
1030 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
1031 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
1032
1033 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
1034 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
1035 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
1036 } else {
1037 unsigned ReservedBufferReg
1038 = TRI->reservedPrivateSegmentBufferReg(MF);
1039 unsigned ReservedOffsetReg
1040 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
1041
1042 // We tentatively reserve the last registers (skipping the last two
1043 // which may contain VCC). After register allocation, we'll replace
1044 // these with the ones immediately after those which were really
1045 // allocated. In the prologue copies will be inserted from the argument
1046 // to these reserved registers.
1047 Info->setScratchRSrcReg(ReservedBufferReg);
1048 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
1049 }
1050 } else {
1051 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
1052
1053 // Without HSA, relocations are used for the scratch pointer and the
1054 // buffer resource setup is always inserted in the prologue. Scratch wave
1055 // offset is still in an input SGPR.
1056 Info->setScratchRSrcReg(ReservedBufferReg);
1057
1058 if (HasStackObjects) {
1059 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
1060 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
1061 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
1062 } else {
1063 unsigned ReservedOffsetReg
1064 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
1065 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
1066 }
1067 }
1068
1069 if (Info->hasWorkItemIDX()) {
1070 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
1071 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1072 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +00001073 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001074
1075 if (Info->hasWorkItemIDY()) {
1076 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
1077 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1078 CCInfo.AllocateReg(Reg);
1079 }
1080
1081 if (Info->hasWorkItemIDZ()) {
1082 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
1083 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1084 CCInfo.AllocateReg(Reg);
1085 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +00001086
Matt Arsenaultcf13d182015-07-10 22:51:36 +00001087 if (Chains.empty())
1088 return Chain;
1089
1090 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +00001091}
1092
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001093SDValue
1094SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1095 bool isVarArg,
1096 const SmallVectorImpl<ISD::OutputArg> &Outs,
1097 const SmallVectorImpl<SDValue> &OutVals,
1098 const SDLoc &DL, SelectionDAG &DAG) const {
Marek Olsak8a0f3352016-01-13 17:23:04 +00001099 MachineFunction &MF = DAG.getMachineFunction();
1100 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1101
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001102 if (!AMDGPU::isShader(CallConv))
Marek Olsak8a0f3352016-01-13 17:23:04 +00001103 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
1104 OutVals, DL, DAG);
1105
Marek Olsak8e9cc632016-01-13 17:23:09 +00001106 Info->setIfReturnsVoid(Outs.size() == 0);
1107
Marek Olsak8a0f3352016-01-13 17:23:04 +00001108 SmallVector<ISD::OutputArg, 48> Splits;
1109 SmallVector<SDValue, 48> SplitVals;
1110
1111 // Split vectors into their elements.
1112 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1113 const ISD::OutputArg &Out = Outs[i];
1114
1115 if (Out.VT.isVector()) {
1116 MVT VT = Out.VT.getVectorElementType();
1117 ISD::OutputArg NewOut = Out;
1118 NewOut.Flags.setSplit();
1119 NewOut.VT = VT;
1120
1121 // We want the original number of vector elements here, e.g.
1122 // three or five, not four or eight.
1123 unsigned NumElements = Out.ArgVT.getVectorNumElements();
1124
1125 for (unsigned j = 0; j != NumElements; ++j) {
1126 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
1127 DAG.getConstant(j, DL, MVT::i32));
1128 SplitVals.push_back(Elem);
1129 Splits.push_back(NewOut);
1130 NewOut.PartOffset += NewOut.VT.getStoreSize();
1131 }
1132 } else {
1133 SplitVals.push_back(OutVals[i]);
1134 Splits.push_back(Out);
1135 }
1136 }
1137
1138 // CCValAssign - represent the assignment of the return value to a location.
1139 SmallVector<CCValAssign, 48> RVLocs;
1140
1141 // CCState - Info about the registers and stack slots.
1142 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1143 *DAG.getContext());
1144
1145 // Analyze outgoing return values.
1146 AnalyzeReturn(CCInfo, Splits);
1147
1148 SDValue Flag;
1149 SmallVector<SDValue, 48> RetOps;
1150 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1151
1152 // Copy the result values into the output registers.
1153 for (unsigned i = 0, realRVLocIdx = 0;
1154 i != RVLocs.size();
1155 ++i, ++realRVLocIdx) {
1156 CCValAssign &VA = RVLocs[i];
1157 assert(VA.isRegLoc() && "Can only return in registers!");
1158
1159 SDValue Arg = SplitVals[realRVLocIdx];
1160
1161 // Copied from other backends.
1162 switch (VA.getLocInfo()) {
1163 default: llvm_unreachable("Unknown loc info!");
1164 case CCValAssign::Full:
1165 break;
1166 case CCValAssign::BCvt:
1167 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1168 break;
1169 }
1170
1171 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
1172 Flag = Chain.getValue(1);
1173 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1174 }
1175
1176 // Update chain and glue.
1177 RetOps[0] = Chain;
1178 if (Flag.getNode())
1179 RetOps.push_back(Flag);
1180
Matt Arsenault9babdf42016-06-22 20:15:28 +00001181 unsigned Opc = Info->returnsVoid() ? AMDGPUISD::ENDPGM : AMDGPUISD::RETURN;
1182 return DAG.getNode(Opc, DL, MVT::Other, RetOps);
Marek Olsak8a0f3352016-01-13 17:23:04 +00001183}
1184
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001185unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1186 SelectionDAG &DAG) const {
1187 unsigned Reg = StringSwitch<unsigned>(RegName)
1188 .Case("m0", AMDGPU::M0)
1189 .Case("exec", AMDGPU::EXEC)
1190 .Case("exec_lo", AMDGPU::EXEC_LO)
1191 .Case("exec_hi", AMDGPU::EXEC_HI)
1192 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1193 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1194 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1195 .Default(AMDGPU::NoRegister);
1196
1197 if (Reg == AMDGPU::NoRegister) {
1198 report_fatal_error(Twine("invalid register name \""
1199 + StringRef(RegName) + "\"."));
1200
1201 }
1202
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001203 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001204 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1205 report_fatal_error(Twine("invalid register \""
1206 + StringRef(RegName) + "\" for subtarget."));
1207 }
1208
1209 switch (Reg) {
1210 case AMDGPU::M0:
1211 case AMDGPU::EXEC_LO:
1212 case AMDGPU::EXEC_HI:
1213 case AMDGPU::FLAT_SCR_LO:
1214 case AMDGPU::FLAT_SCR_HI:
1215 if (VT.getSizeInBits() == 32)
1216 return Reg;
1217 break;
1218 case AMDGPU::EXEC:
1219 case AMDGPU::FLAT_SCR:
1220 if (VT.getSizeInBits() == 64)
1221 return Reg;
1222 break;
1223 default:
1224 llvm_unreachable("missing register type checking");
1225 }
1226
1227 report_fatal_error(Twine("invalid type for register \""
1228 + StringRef(RegName) + "\"."));
1229}
1230
Matt Arsenault786724a2016-07-12 21:41:32 +00001231// If kill is not the last instruction, split the block so kill is always a
1232// proper terminator.
1233MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI,
1234 MachineBasicBlock *BB) const {
1235 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1236
1237 MachineBasicBlock::iterator SplitPoint(&MI);
1238 ++SplitPoint;
1239
1240 if (SplitPoint == BB->end()) {
1241 // Don't bother with a new block.
1242 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1243 return BB;
1244 }
1245
1246 MachineFunction *MF = BB->getParent();
1247 MachineBasicBlock *SplitBB
1248 = MF->CreateMachineBasicBlock(BB->getBasicBlock());
1249
Matt Arsenault786724a2016-07-12 21:41:32 +00001250 MF->insert(++MachineFunction::iterator(BB), SplitBB);
1251 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end());
1252
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001253 SplitBB->transferSuccessorsAndUpdatePHIs(BB);
Matt Arsenault786724a2016-07-12 21:41:32 +00001254 BB->addSuccessor(SplitBB);
1255
1256 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1257 return SplitBB;
1258}
1259
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001260// Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the
1261// wavefront. If the value is uniform and just happens to be in a VGPR, this
1262// will only do one iteration. In the worst case, this will loop 64 times.
1263//
1264// TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001265static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop(
1266 const SIInstrInfo *TII,
1267 MachineRegisterInfo &MRI,
1268 MachineBasicBlock &OrigBB,
1269 MachineBasicBlock &LoopBB,
1270 const DebugLoc &DL,
1271 const MachineOperand &IdxReg,
1272 unsigned InitReg,
1273 unsigned ResultReg,
1274 unsigned PhiReg,
1275 unsigned InitSaveExecReg,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001276 int Offset,
1277 bool UseGPRIdxMode) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001278 MachineBasicBlock::iterator I = LoopBB.begin();
1279
1280 unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1281 unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1282 unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1283 unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1284
1285 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg)
1286 .addReg(InitReg)
1287 .addMBB(&OrigBB)
1288 .addReg(ResultReg)
1289 .addMBB(&LoopBB);
1290
1291 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec)
1292 .addReg(InitSaveExecReg)
1293 .addMBB(&OrigBB)
1294 .addReg(NewExec)
1295 .addMBB(&LoopBB);
1296
1297 // Read the next variant <- also loop target.
1298 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg)
1299 .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef()));
1300
1301 // Compare the just read M0 value to all possible Idx values.
1302 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg)
1303 .addReg(CurrentIdxReg)
Matt Arsenaultf0ba86a2016-07-21 09:40:57 +00001304 .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg());
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001305
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001306 if (UseGPRIdxMode) {
1307 unsigned IdxReg;
1308 if (Offset == 0) {
1309 IdxReg = CurrentIdxReg;
1310 } else {
1311 IdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1312 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), IdxReg)
1313 .addReg(CurrentIdxReg, RegState::Kill)
1314 .addImm(Offset);
1315 }
1316
1317 MachineInstr *SetIdx =
1318 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_IDX))
1319 .addReg(IdxReg, RegState::Kill);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001320 SetIdx->getOperand(2).setIsUndef();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001321 } else {
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001322 // Move index from VCC into M0
1323 if (Offset == 0) {
1324 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1325 .addReg(CurrentIdxReg, RegState::Kill);
1326 } else {
1327 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1328 .addReg(CurrentIdxReg, RegState::Kill)
1329 .addImm(Offset);
1330 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001331 }
1332
1333 // Update EXEC, save the original EXEC value to VCC.
1334 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec)
1335 .addReg(CondReg, RegState::Kill);
1336
1337 MRI.setSimpleHint(NewExec, CondReg);
1338
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001339 // Update EXEC, switch all done bits to 0 and all todo bits to 1.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001340 MachineInstr *InsertPt =
1341 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001342 .addReg(AMDGPU::EXEC)
1343 .addReg(NewExec);
1344
1345 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use
1346 // s_cbranch_scc0?
1347
1348 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover.
1349 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
1350 .addMBB(&LoopBB);
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001351
1352 return InsertPt->getIterator();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001353}
1354
1355// This has slightly sub-optimal regalloc when the source vector is killed by
1356// the read. The register allocator does not understand that the kill is
1357// per-workitem, so is kept alive for the whole loop so we end up not re-using a
1358// subregister from it, using 1 more VGPR than necessary. This was saved when
1359// this was expanded after register allocation.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001360static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII,
1361 MachineBasicBlock &MBB,
1362 MachineInstr &MI,
1363 unsigned InitResultReg,
1364 unsigned PhiReg,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001365 int Offset,
1366 bool UseGPRIdxMode) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001367 MachineFunction *MF = MBB.getParent();
1368 MachineRegisterInfo &MRI = MF->getRegInfo();
1369 const DebugLoc &DL = MI.getDebugLoc();
1370 MachineBasicBlock::iterator I(&MI);
1371
1372 unsigned DstReg = MI.getOperand(0).getReg();
1373 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1374 unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1375
1376 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec);
1377
1378 // Save the EXEC mask
1379 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec)
1380 .addReg(AMDGPU::EXEC);
1381
1382 // To insert the loop we need to split the block. Move everything after this
1383 // point to a new block, and insert a new empty block between the two.
1384 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock();
1385 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
1386 MachineFunction::iterator MBBI(MBB);
1387 ++MBBI;
1388
1389 MF->insert(MBBI, LoopBB);
1390 MF->insert(MBBI, RemainderBB);
1391
1392 LoopBB->addSuccessor(LoopBB);
1393 LoopBB->addSuccessor(RemainderBB);
1394
1395 // Move the rest of the block into a new block.
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001396 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001397 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
1398
1399 MBB.addSuccessor(LoopBB);
1400
1401 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1402
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001403 auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx,
1404 InitResultReg, DstReg, PhiReg, TmpExec,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001405 Offset, UseGPRIdxMode);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001406
1407 MachineBasicBlock::iterator First = RemainderBB->begin();
1408 BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
1409 .addReg(SaveExec);
1410
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001411 return InsPt;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001412}
1413
1414// Returns subreg index, offset
1415static std::pair<unsigned, int>
1416computeIndirectRegAndOffset(const SIRegisterInfo &TRI,
1417 const TargetRegisterClass *SuperRC,
1418 unsigned VecReg,
1419 int Offset) {
1420 int NumElts = SuperRC->getSize() / 4;
1421
1422 // Skip out of bounds offsets, or else we would end up using an undefined
1423 // register.
1424 if (Offset >= NumElts || Offset < 0)
1425 return std::make_pair(AMDGPU::sub0, Offset);
1426
1427 return std::make_pair(AMDGPU::sub0 + Offset, 0);
1428}
1429
1430// Return true if the index is an SGPR and was set.
1431static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII,
1432 MachineRegisterInfo &MRI,
1433 MachineInstr &MI,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001434 int Offset,
1435 bool UseGPRIdxMode,
1436 bool IsIndirectSrc) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001437 MachineBasicBlock *MBB = MI.getParent();
1438 const DebugLoc &DL = MI.getDebugLoc();
1439 MachineBasicBlock::iterator I(&MI);
1440
1441 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1442 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
1443
1444 assert(Idx->getReg() != AMDGPU::NoRegister);
1445
1446 if (!TII->getRegisterInfo().isSGPRClass(IdxRC))
1447 return false;
1448
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001449 if (UseGPRIdxMode) {
1450 unsigned IdxMode = IsIndirectSrc ?
1451 VGPRIndexMode::SRC0_ENABLE : VGPRIndexMode::DST_ENABLE;
1452 if (Offset == 0) {
1453 MachineInstr *SetOn =
1454 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1455 .addOperand(*Idx)
1456 .addImm(IdxMode);
1457
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001458 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001459 } else {
1460 unsigned Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
1461 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp)
1462 .addOperand(*Idx)
1463 .addImm(Offset);
1464 MachineInstr *SetOn =
1465 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1466 .addReg(Tmp, RegState::Kill)
1467 .addImm(IdxMode);
1468
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001469 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001470 }
1471
1472 return true;
1473 }
1474
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001475 if (Offset == 0) {
1476 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1477 .addOperand(*Idx);
1478 } else {
1479 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1480 .addOperand(*Idx)
1481 .addImm(Offset);
1482 }
1483
1484 return true;
1485}
1486
1487// Control flow needs to be inserted if indexing with a VGPR.
1488static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI,
1489 MachineBasicBlock &MBB,
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001490 const SISubtarget &ST) {
1491 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001492 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1493 MachineFunction *MF = MBB.getParent();
1494 MachineRegisterInfo &MRI = MF->getRegInfo();
1495
1496 unsigned Dst = MI.getOperand(0).getReg();
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001497 unsigned SrcReg = TII->getNamedOperand(MI, AMDGPU::OpName::src)->getReg();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001498 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1499
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001500 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcReg);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001501
1502 unsigned SubReg;
1503 std::tie(SubReg, Offset)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001504 = computeIndirectRegAndOffset(TRI, VecRC, SrcReg, Offset);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001505
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001506 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode;
1507
1508 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, true)) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001509 MachineBasicBlock::iterator I(&MI);
1510 const DebugLoc &DL = MI.getDebugLoc();
1511
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001512 if (UseGPRIdxMode) {
1513 // TODO: Look at the uses to avoid the copy. This may require rescheduling
1514 // to avoid interfering with other uses, so probably requires a new
1515 // optimization pass.
1516 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001517 .addReg(SrcReg, RegState::Undef, SubReg)
1518 .addReg(SrcReg, RegState::Implicit)
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001519 .addReg(AMDGPU::M0, RegState::Implicit);
1520 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1521 } else {
1522 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001523 .addReg(SrcReg, RegState::Undef, SubReg)
1524 .addReg(SrcReg, RegState::Implicit);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001525 }
1526
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001527 MI.eraseFromParent();
1528
1529 return &MBB;
1530 }
1531
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001532
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001533 const DebugLoc &DL = MI.getDebugLoc();
1534 MachineBasicBlock::iterator I(&MI);
1535
1536 unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1537 unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1538
1539 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg);
1540
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001541 if (UseGPRIdxMode) {
1542 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1543 .addImm(0) // Reset inside loop.
1544 .addImm(VGPRIndexMode::SRC0_ENABLE);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001545 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001546
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001547 // Disable again after the loop.
1548 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1549 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001550
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001551 auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset, UseGPRIdxMode);
1552 MachineBasicBlock *LoopBB = InsPt->getParent();
1553
1554 if (UseGPRIdxMode) {
1555 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001556 .addReg(SrcReg, RegState::Undef, SubReg)
1557 .addReg(SrcReg, RegState::Implicit)
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001558 .addReg(AMDGPU::M0, RegState::Implicit);
1559 } else {
1560 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001561 .addReg(SrcReg, RegState::Undef, SubReg)
1562 .addReg(SrcReg, RegState::Implicit);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001563 }
1564
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001565 MI.eraseFromParent();
1566
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001567 return LoopBB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001568}
1569
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001570static unsigned getMOVRELDPseudo(const TargetRegisterClass *VecRC) {
1571 switch (VecRC->getSize()) {
1572 case 4:
1573 return AMDGPU::V_MOVRELD_B32_V1;
1574 case 8:
1575 return AMDGPU::V_MOVRELD_B32_V2;
1576 case 16:
1577 return AMDGPU::V_MOVRELD_B32_V4;
1578 case 32:
1579 return AMDGPU::V_MOVRELD_B32_V8;
1580 case 64:
1581 return AMDGPU::V_MOVRELD_B32_V16;
1582 default:
1583 llvm_unreachable("unsupported size for MOVRELD pseudos");
1584 }
1585}
1586
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001587static MachineBasicBlock *emitIndirectDst(MachineInstr &MI,
1588 MachineBasicBlock &MBB,
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001589 const SISubtarget &ST) {
1590 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001591 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1592 MachineFunction *MF = MBB.getParent();
1593 MachineRegisterInfo &MRI = MF->getRegInfo();
1594
1595 unsigned Dst = MI.getOperand(0).getReg();
1596 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
1597 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1598 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val);
1599 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1600 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
1601
1602 // This can be an immediate, but will be folded later.
1603 assert(Val->getReg());
1604
1605 unsigned SubReg;
1606 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC,
1607 SrcVec->getReg(),
1608 Offset);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001609 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode;
1610
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001611 if (Idx->getReg() == AMDGPU::NoRegister) {
1612 MachineBasicBlock::iterator I(&MI);
1613 const DebugLoc &DL = MI.getDebugLoc();
1614
1615 assert(Offset == 0);
1616
1617 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst)
1618 .addOperand(*SrcVec)
1619 .addOperand(*Val)
1620 .addImm(SubReg);
1621
1622 MI.eraseFromParent();
1623 return &MBB;
1624 }
1625
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001626 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, false)) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001627 MachineBasicBlock::iterator I(&MI);
1628 const DebugLoc &DL = MI.getDebugLoc();
1629
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001630 if (UseGPRIdxMode) {
1631 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_indirect))
1632 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst
1633 .addOperand(*Val)
1634 .addReg(Dst, RegState::ImplicitDefine)
1635 .addReg(SrcVec->getReg(), RegState::Implicit)
1636 .addReg(AMDGPU::M0, RegState::Implicit);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001637
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001638 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1639 } else {
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001640 const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(VecRC));
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001641
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001642 BuildMI(MBB, I, DL, MovRelDesc)
1643 .addReg(Dst, RegState::Define)
1644 .addReg(SrcVec->getReg())
1645 .addOperand(*Val)
1646 .addImm(SubReg - AMDGPU::sub0);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001647 }
1648
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001649 MI.eraseFromParent();
1650 return &MBB;
1651 }
1652
1653 if (Val->isReg())
1654 MRI.clearKillFlags(Val->getReg());
1655
1656 const DebugLoc &DL = MI.getDebugLoc();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001657
1658 if (UseGPRIdxMode) {
1659 MachineBasicBlock::iterator I(&MI);
1660
1661 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1662 .addImm(0) // Reset inside loop.
1663 .addImm(VGPRIndexMode::DST_ENABLE);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001664 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001665
1666 // Disable again after the loop.
1667 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1668 }
1669
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001670 unsigned PhiReg = MRI.createVirtualRegister(VecRC);
1671
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001672 auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg,
1673 Offset, UseGPRIdxMode);
1674 MachineBasicBlock *LoopBB = InsPt->getParent();
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001675
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001676 if (UseGPRIdxMode) {
1677 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_indirect))
1678 .addReg(PhiReg, RegState::Undef, SubReg) // vdst
1679 .addOperand(*Val) // src0
1680 .addReg(Dst, RegState::ImplicitDefine)
1681 .addReg(PhiReg, RegState::Implicit)
1682 .addReg(AMDGPU::M0, RegState::Implicit);
1683 } else {
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001684 const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(VecRC));
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001685
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001686 BuildMI(*LoopBB, InsPt, DL, MovRelDesc)
1687 .addReg(Dst, RegState::Define)
1688 .addReg(PhiReg)
1689 .addOperand(*Val)
1690 .addImm(SubReg - AMDGPU::sub0);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001691 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001692
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001693 MI.eraseFromParent();
1694
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001695 return LoopBB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001696}
1697
Matt Arsenault786724a2016-07-12 21:41:32 +00001698MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
1699 MachineInstr &MI, MachineBasicBlock *BB) const {
Tom Stellard244891d2016-12-20 15:52:17 +00001700
1701 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1702 MachineFunction *MF = BB->getParent();
1703 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1704
1705 if (TII->isMIMG(MI)) {
1706 if (!MI.memoperands_empty())
1707 return BB;
1708 // Add a memoperand for mimg instructions so that they aren't assumed to
1709 // be ordered memory instuctions.
1710
1711 MachinePointerInfo PtrInfo(MFI->getImagePSV());
1712 MachineMemOperand::Flags Flags = MachineMemOperand::MODereferenceable;
1713 if (MI.mayStore())
1714 Flags |= MachineMemOperand::MOStore;
1715
1716 if (MI.mayLoad())
1717 Flags |= MachineMemOperand::MOLoad;
1718
1719 auto MMO = MF->getMachineMemOperand(PtrInfo, Flags, 0, 0);
1720 MI.addMemOperand(*MF, MMO);
1721 return BB;
1722 }
1723
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001724 switch (MI.getOpcode()) {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001725 case AMDGPU::SI_INIT_M0: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001726 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001727 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001728 .addOperand(MI.getOperand(0));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001729 MI.eraseFromParent();
Matt Arsenault20711b72015-02-20 22:10:45 +00001730 return BB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001731 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001732 case AMDGPU::GET_GROUPSTATICSIZE: {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001733 DebugLoc DL = MI.getDebugLoc();
Matt Arsenault3c07c812016-07-22 17:01:33 +00001734 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32))
1735 .addOperand(MI.getOperand(0))
Matt Arsenault52ef4012016-07-26 16:45:58 +00001736 .addImm(MFI->getLDSSize());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001737 MI.eraseFromParent();
Changpeng Fang01f60622016-03-15 17:28:44 +00001738 return BB;
1739 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001740 case AMDGPU::SI_INDIRECT_SRC_V1:
1741 case AMDGPU::SI_INDIRECT_SRC_V2:
1742 case AMDGPU::SI_INDIRECT_SRC_V4:
1743 case AMDGPU::SI_INDIRECT_SRC_V8:
1744 case AMDGPU::SI_INDIRECT_SRC_V16:
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001745 return emitIndirectSrc(MI, *BB, *getSubtarget());
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001746 case AMDGPU::SI_INDIRECT_DST_V1:
1747 case AMDGPU::SI_INDIRECT_DST_V2:
1748 case AMDGPU::SI_INDIRECT_DST_V4:
1749 case AMDGPU::SI_INDIRECT_DST_V8:
1750 case AMDGPU::SI_INDIRECT_DST_V16:
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001751 return emitIndirectDst(MI, *BB, *getSubtarget());
Matt Arsenault786724a2016-07-12 21:41:32 +00001752 case AMDGPU::SI_KILL:
1753 return splitKillBlock(MI, BB);
Matt Arsenault22e41792016-08-27 01:00:37 +00001754 case AMDGPU::V_CNDMASK_B64_PSEUDO: {
1755 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Matt Arsenault22e41792016-08-27 01:00:37 +00001756
1757 unsigned Dst = MI.getOperand(0).getReg();
1758 unsigned Src0 = MI.getOperand(1).getReg();
1759 unsigned Src1 = MI.getOperand(2).getReg();
1760 const DebugLoc &DL = MI.getDebugLoc();
1761 unsigned SrcCond = MI.getOperand(3).getReg();
1762
1763 unsigned DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1764 unsigned DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1765
1766 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo)
1767 .addReg(Src0, 0, AMDGPU::sub0)
1768 .addReg(Src1, 0, AMDGPU::sub0)
1769 .addReg(SrcCond);
1770 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi)
1771 .addReg(Src0, 0, AMDGPU::sub1)
1772 .addReg(Src1, 0, AMDGPU::sub1)
1773 .addReg(SrcCond);
1774
1775 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst)
1776 .addReg(DstLo)
1777 .addImm(AMDGPU::sub0)
1778 .addReg(DstHi)
1779 .addImm(AMDGPU::sub1);
1780 MI.eraseFromParent();
1781 return BB;
1782 }
Matt Arsenault327188a2016-12-15 21:57:11 +00001783 case AMDGPU::SI_BR_UNDEF: {
1784 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1785 const DebugLoc &DL = MI.getDebugLoc();
1786 MachineInstr *Br = BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_CBRANCH_SCC1))
1787 .addOperand(MI.getOperand(0));
1788 Br->getOperand(1).setIsUndef(true); // read undef SCC
1789 MI.eraseFromParent();
1790 return BB;
1791 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001792 default:
1793 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001794 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001795}
1796
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001797bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1798 // This currently forces unfolding various combinations of fsub into fma with
1799 // free fneg'd operands. As long as we have fast FMA (controlled by
1800 // isFMAFasterThanFMulAndFAdd), we should perform these.
1801
1802 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1803 // most of these combines appear to be cycle neutral but save on instruction
1804 // count / code size.
1805 return true;
1806}
1807
Mehdi Amini44ede332015-07-09 02:09:04 +00001808EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1809 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001810 if (!VT.isVector()) {
1811 return MVT::i1;
1812 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001813 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001814}
1815
Mehdi Aminieaabc512015-07-09 15:12:23 +00001816MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001817 return MVT::i32;
1818}
1819
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001820// Answering this is somewhat tricky and depends on the specific device which
1821// have different rates for fma or all f64 operations.
1822//
1823// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1824// regardless of which device (although the number of cycles differs between
1825// devices), so it is always profitable for f64.
1826//
1827// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1828// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1829// which we can always do even without fused FP ops since it returns the same
1830// result as the separate operations and since it is always full
1831// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1832// however does not support denormals, so we do report fma as faster if we have
1833// a fast fma device and require denormals.
1834//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001835bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1836 VT = VT.getScalarType();
1837
1838 if (!VT.isSimple())
1839 return false;
1840
1841 switch (VT.getSimpleVT().SimpleTy) {
1842 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001843 // This is as fast on some subtargets. However, we always have full rate f32
1844 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001845 // which we should prefer over fma. We can't use this if we want to support
1846 // denormals, so only report this in these cases.
1847 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001848 case MVT::f64:
1849 return true;
Matt Arsenault9e22bc22016-12-22 03:21:48 +00001850 case MVT::f16:
1851 return Subtarget->has16BitInsts() && Subtarget->hasFP16Denormals();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001852 default:
1853 break;
1854 }
1855
1856 return false;
1857}
1858
Tom Stellard75aadc22012-12-11 21:25:42 +00001859//===----------------------------------------------------------------------===//
1860// Custom DAG Lowering Operations
1861//===----------------------------------------------------------------------===//
1862
1863SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1864 switch (Op.getOpcode()) {
1865 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001866 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001867 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001868 SDValue Result = LowerLOAD(Op, DAG);
1869 assert((!Result.getNode() ||
1870 Result.getNode()->getNumValues() == 2) &&
1871 "Load should return a value and a chain");
1872 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001873 }
Tom Stellardaf775432013-10-23 00:44:32 +00001874
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001875 case ISD::FSIN:
1876 case ISD::FCOS:
1877 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001878 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001879 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001880 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001881 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001882 case ISD::GlobalAddress: {
1883 MachineFunction &MF = DAG.getMachineFunction();
1884 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1885 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001886 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001887 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001888 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001889 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Matt Arsenault99c14522016-04-25 19:27:24 +00001890 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG);
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001891 case ISD::TRAP: return lowerTRAP(Op, DAG);
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00001892 case ISD::FP_ROUND:
1893 return lowerFP_ROUND(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001894 }
1895 return SDValue();
1896}
1897
Tom Stellardf8794352012-12-19 22:10:31 +00001898/// \brief Helper function for LowerBRCOND
1899static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001900
Tom Stellardf8794352012-12-19 22:10:31 +00001901 SDNode *Parent = Value.getNode();
1902 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1903 I != E; ++I) {
1904
1905 if (I.getUse().get() != Value)
1906 continue;
1907
1908 if (I->getOpcode() == Opcode)
1909 return *I;
1910 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001911 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001912}
1913
Tom Stellardbc4497b2016-02-12 23:45:29 +00001914bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault6408c912016-09-16 22:11:18 +00001915 if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
1916 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1917 case AMDGPUIntrinsic::amdgcn_if:
1918 case AMDGPUIntrinsic::amdgcn_else:
1919 case AMDGPUIntrinsic::amdgcn_end_cf:
1920 case AMDGPUIntrinsic::amdgcn_loop:
1921 return true;
1922 default:
1923 return false;
1924 }
Tom Stellardbc4497b2016-02-12 23:45:29 +00001925 }
Matt Arsenault6408c912016-09-16 22:11:18 +00001926
1927 if (Intr->getOpcode() == ISD::INTRINSIC_WO_CHAIN) {
1928 switch (cast<ConstantSDNode>(Intr->getOperand(0))->getZExtValue()) {
1929 case AMDGPUIntrinsic::amdgcn_break:
1930 case AMDGPUIntrinsic::amdgcn_if_break:
1931 case AMDGPUIntrinsic::amdgcn_else_break:
1932 return true;
1933 default:
1934 return false;
1935 }
1936 }
1937
1938 return false;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001939}
1940
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001941void SITargetLowering::createDebuggerPrologueStackObjects(
1942 MachineFunction &MF) const {
1943 // Create stack objects that are used for emitting debugger prologue.
1944 //
1945 // Debugger prologue writes work group IDs and work item IDs to scratch memory
1946 // at fixed location in the following format:
1947 // offset 0: work group ID x
1948 // offset 4: work group ID y
1949 // offset 8: work group ID z
1950 // offset 16: work item ID x
1951 // offset 20: work item ID y
1952 // offset 24: work item ID z
1953 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1954 int ObjectIdx = 0;
1955
1956 // For each dimension:
1957 for (unsigned i = 0; i < 3; ++i) {
1958 // Create fixed stack object for work group ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001959 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001960 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx);
1961 // Create fixed stack object for work item ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001962 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001963 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx);
1964 }
1965}
1966
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00001967bool SITargetLowering::shouldEmitFixup(const GlobalValue *GV) const {
1968 const Triple &TT = getTargetMachine().getTargetTriple();
1969 return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS &&
1970 AMDGPU::shouldEmitConstantsToTextSection(TT);
1971}
1972
1973bool SITargetLowering::shouldEmitGOTReloc(const GlobalValue *GV) const {
1974 return (GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS ||
1975 GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) &&
1976 !shouldEmitFixup(GV) &&
1977 !getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
1978}
1979
1980bool SITargetLowering::shouldEmitPCReloc(const GlobalValue *GV) const {
1981 return !shouldEmitFixup(GV) && !shouldEmitGOTReloc(GV);
1982}
1983
Tom Stellardf8794352012-12-19 22:10:31 +00001984/// This transforms the control flow intrinsics to get the branch destination as
1985/// last parameter, also switches branch target with BR if the need arise
1986SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1987 SelectionDAG &DAG) const {
1988
Andrew Trickef9de2a2013-05-25 02:42:55 +00001989 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001990
1991 SDNode *Intr = BRCOND.getOperand(1).getNode();
1992 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001993 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001994 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001995
1996 if (Intr->getOpcode() == ISD::SETCC) {
1997 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001998 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001999 Intr = SetCC->getOperand(0).getNode();
2000
2001 } else {
2002 // Get the target from BR if we don't negate the condition
2003 BR = findUser(BRCOND, ISD::BR);
2004 Target = BR->getOperand(1);
2005 }
2006
Matt Arsenault6408c912016-09-16 22:11:18 +00002007 // FIXME: This changes the types of the intrinsics instead of introducing new
2008 // nodes with the correct types.
2009 // e.g. llvm.amdgcn.loop
2010
2011 // eg: i1,ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3
2012 // => t9: ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3, BasicBlock:ch<bb1 0x7fee5286d088>
2013
Nicolai Haehnleffbd56a2016-05-05 17:36:36 +00002014 if (!isCFIntrinsic(Intr)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00002015 // This is a uniform branch so we don't need to legalize.
2016 return BRCOND;
2017 }
2018
Matt Arsenault6408c912016-09-16 22:11:18 +00002019 bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID ||
2020 Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN;
2021
Tom Stellardbc4497b2016-02-12 23:45:29 +00002022 assert(!SetCC ||
2023 (SetCC->getConstantOperandVal(1) == 1 &&
Tom Stellardbc4497b2016-02-12 23:45:29 +00002024 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
2025 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00002026
Tom Stellardf8794352012-12-19 22:10:31 +00002027 // operands of the new intrinsic call
2028 SmallVector<SDValue, 4> Ops;
Matt Arsenault6408c912016-09-16 22:11:18 +00002029 if (HaveChain)
2030 Ops.push_back(BRCOND.getOperand(0));
2031
2032 Ops.append(Intr->op_begin() + (HaveChain ? 1 : 0), Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00002033 Ops.push_back(Target);
2034
Matt Arsenault6408c912016-09-16 22:11:18 +00002035 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
2036
Tom Stellardf8794352012-12-19 22:10:31 +00002037 // build the new intrinsic call
2038 SDNode *Result = DAG.getNode(
2039 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00002040 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00002041
Matt Arsenault6408c912016-09-16 22:11:18 +00002042 if (!HaveChain) {
2043 SDValue Ops[] = {
2044 SDValue(Result, 0),
2045 BRCOND.getOperand(0)
2046 };
2047
2048 Result = DAG.getMergeValues(Ops, DL).getNode();
2049 }
2050
Tom Stellardf8794352012-12-19 22:10:31 +00002051 if (BR) {
2052 // Give the branch instruction our target
2053 SDValue Ops[] = {
2054 BR->getOperand(0),
2055 BRCOND.getOperand(2)
2056 };
Chandler Carruth356665a2014-08-01 22:09:43 +00002057 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
2058 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
2059 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00002060 }
2061
2062 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
2063
2064 // Copy the intrinsic results to registers
2065 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
2066 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
2067 if (!CopyToReg)
2068 continue;
2069
2070 Chain = DAG.getCopyToReg(
2071 Chain, DL,
2072 CopyToReg->getOperand(1),
2073 SDValue(Result, i - 1),
2074 SDValue());
2075
2076 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
2077 }
2078
2079 // Remove the old intrinsic from the chain
2080 DAG.ReplaceAllUsesOfValueWith(
2081 SDValue(Intr, Intr->getNumValues() - 1),
2082 Intr->getOperand(0));
2083
2084 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00002085}
2086
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002087SDValue SITargetLowering::getFPExtOrFPTrunc(SelectionDAG &DAG,
2088 SDValue Op,
2089 const SDLoc &DL,
2090 EVT VT) const {
2091 return Op.getValueType().bitsLE(VT) ?
2092 DAG.getNode(ISD::FP_EXTEND, DL, VT, Op) :
2093 DAG.getNode(ISD::FTRUNC, DL, VT, Op);
2094}
2095
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00002096SDValue SITargetLowering::lowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaultafe614c2016-11-18 18:33:36 +00002097 assert(Op.getValueType() == MVT::f16 &&
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00002098 "Do not know how to custom lower FP_ROUND for non-f16 type");
2099
Matt Arsenaultafe614c2016-11-18 18:33:36 +00002100 SDValue Src = Op.getOperand(0);
2101 EVT SrcVT = Src.getValueType();
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00002102 if (SrcVT != MVT::f64)
2103 return Op;
2104
2105 SDLoc DL(Op);
Matt Arsenaultafe614c2016-11-18 18:33:36 +00002106
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00002107 SDValue FpToFp16 = DAG.getNode(ISD::FP_TO_FP16, DL, MVT::i32, Src);
2108 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToFp16);
2109 return DAG.getNode(ISD::BITCAST, DL, MVT::f16, Trunc);;
2110}
2111
Matt Arsenault99c14522016-04-25 19:27:24 +00002112SDValue SITargetLowering::getSegmentAperture(unsigned AS,
2113 SelectionDAG &DAG) const {
2114 SDLoc SL;
2115 MachineFunction &MF = DAG.getMachineFunction();
2116 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00002117 unsigned UserSGPR = Info->getQueuePtrUserSGPR();
2118 assert(UserSGPR != AMDGPU::NoRegister);
2119
Matt Arsenault99c14522016-04-25 19:27:24 +00002120 SDValue QueuePtr = CreateLiveInRegister(
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00002121 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
Matt Arsenault99c14522016-04-25 19:27:24 +00002122
2123 // Offset into amd_queue_t for group_segment_aperture_base_hi /
2124 // private_segment_aperture_base_hi.
2125 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
2126
2127 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr,
2128 DAG.getConstant(StructOffset, SL, MVT::i64));
2129
2130 // TODO: Use custom target PseudoSourceValue.
2131 // TODO: We should use the value from the IR intrinsic call, but it might not
2132 // be available and how do we get it?
2133 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()),
2134 AMDGPUAS::CONSTANT_ADDRESS));
2135
2136 MachinePointerInfo PtrInfo(V, StructOffset);
Justin Lebar9c375812016-07-15 18:27:10 +00002137 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo,
2138 MinAlign(64, StructOffset),
Justin Lebaradbf09e2016-09-11 01:38:58 +00002139 MachineMemOperand::MODereferenceable |
2140 MachineMemOperand::MOInvariant);
Matt Arsenault99c14522016-04-25 19:27:24 +00002141}
2142
2143SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
2144 SelectionDAG &DAG) const {
2145 SDLoc SL(Op);
2146 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op);
2147
2148 SDValue Src = ASC->getOperand(0);
2149
2150 // FIXME: Really support non-0 null pointers.
2151 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32);
2152 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
2153
2154 // flat -> local/private
2155 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
2156 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
2157 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
2158 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
2159 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
2160
2161 return DAG.getNode(ISD::SELECT, SL, MVT::i32,
2162 NonNull, Ptr, SegmentNullPtr);
2163 }
2164 }
2165
2166 // local/private -> flat
2167 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
2168 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
2169 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
2170 SDValue NonNull
2171 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
2172
2173 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG);
2174 SDValue CvtPtr
2175 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
2176
2177 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull,
2178 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr),
2179 FlatNullPtr);
2180 }
2181 }
2182
2183 // global <-> flat are no-ops and never emitted.
2184
2185 const MachineFunction &MF = DAG.getMachineFunction();
2186 DiagnosticInfoUnsupported InvalidAddrSpaceCast(
2187 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc());
2188 DAG.getContext()->diagnose(InvalidAddrSpaceCast);
2189
2190 return DAG.getUNDEF(ASC->getValueType(0));
2191}
2192
Tom Stellard418beb72016-07-13 14:23:33 +00002193bool
2194SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2195 // We can fold offsets for anything that doesn't require a GOT relocation.
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00002196 return (GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS ||
2197 GA->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) &&
2198 !shouldEmitGOTReloc(GA->getGlobal());
Tom Stellard418beb72016-07-13 14:23:33 +00002199}
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002200
Tom Stellard418beb72016-07-13 14:23:33 +00002201static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV,
2202 SDLoc DL, unsigned Offset, EVT PtrVT,
2203 unsigned GAFlags = SIInstrInfo::MO_NONE) {
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002204 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
2205 // lowered to the following code sequence:
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002206 //
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002207 // For constant address space:
2208 // s_getpc_b64 s[0:1]
2209 // s_add_u32 s0, s0, $symbol
2210 // s_addc_u32 s1, s1, 0
2211 //
2212 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
2213 // a fixup or relocation is emitted to replace $symbol with a literal
2214 // constant, which is a pc-relative offset from the encoding of the $symbol
2215 // operand to the global variable.
2216 //
2217 // For global address space:
2218 // s_getpc_b64 s[0:1]
2219 // s_add_u32 s0, s0, $symbol@{gotpc}rel32@lo
2220 // s_addc_u32 s1, s1, $symbol@{gotpc}rel32@hi
2221 //
2222 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
2223 // fixups or relocations are emitted to replace $symbol@*@lo and
2224 // $symbol@*@hi with lower 32 bits and higher 32 bits of a literal constant,
2225 // which is a 64-bit pc-relative offset from the encoding of the $symbol
2226 // operand to the global variable.
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002227 //
2228 // What we want here is an offset from the value returned by s_getpc
2229 // (which is the address of the s_add_u32 instruction) to the global
2230 // variable, but since the encoding of $symbol starts 4 bytes after the start
2231 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too
2232 // small. This requires us to add 4 to the global variable offset in order to
2233 // compute the correct address.
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002234 SDValue PtrLo = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
2235 GAFlags);
2236 SDValue PtrHi = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
2237 GAFlags == SIInstrInfo::MO_NONE ?
2238 GAFlags : GAFlags + 1);
2239 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, PtrLo, PtrHi);
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002240}
2241
Tom Stellard418beb72016-07-13 14:23:33 +00002242SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
2243 SDValue Op,
2244 SelectionDAG &DAG) const {
2245 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
2246
2247 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS &&
2248 GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS)
2249 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
2250
2251 SDLoc DL(GSD);
2252 const GlobalValue *GV = GSD->getGlobal();
2253 EVT PtrVT = Op.getValueType();
2254
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00002255 if (shouldEmitFixup(GV))
Tom Stellard418beb72016-07-13 14:23:33 +00002256 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00002257 else if (shouldEmitPCReloc(GV))
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002258 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT,
2259 SIInstrInfo::MO_REL32);
Tom Stellard418beb72016-07-13 14:23:33 +00002260
2261 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002262 SIInstrInfo::MO_GOTPCREL32);
Tom Stellard418beb72016-07-13 14:23:33 +00002263
2264 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext());
2265 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
2266 const DataLayout &DataLayout = DAG.getDataLayout();
2267 unsigned Align = DataLayout.getABITypeAlignment(PtrTy);
2268 // FIXME: Use a PseudoSourceValue once those can be assigned an address space.
2269 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
2270
Justin Lebar9c375812016-07-15 18:27:10 +00002271 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align,
Justin Lebaradbf09e2016-09-11 01:38:58 +00002272 MachineMemOperand::MODereferenceable |
2273 MachineMemOperand::MOInvariant);
Tom Stellard418beb72016-07-13 14:23:33 +00002274}
2275
Matt Arsenault0bb294b2016-06-17 22:27:03 +00002276SDValue SITargetLowering::lowerTRAP(SDValue Op,
2277 SelectionDAG &DAG) const {
2278 const MachineFunction &MF = DAG.getMachineFunction();
2279 DiagnosticInfoUnsupported NoTrap(*MF.getFunction(),
2280 "trap handler not supported",
2281 Op.getDebugLoc(),
2282 DS_Warning);
2283 DAG.getContext()->diagnose(NoTrap);
2284
2285 // Emit s_endpgm.
2286
2287 // FIXME: This should really be selected to s_trap, but that requires
2288 // setting up the trap handler for it o do anything.
Matt Arsenault9babdf42016-06-22 20:15:28 +00002289 return DAG.getNode(AMDGPUISD::ENDPGM, SDLoc(Op), MVT::Other,
2290 Op.getOperand(0));
Matt Arsenault0bb294b2016-06-17 22:27:03 +00002291}
2292
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002293SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain,
2294 const SDLoc &DL, SDValue V) const {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00002295 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
2296 // the destination register.
2297 //
Tom Stellardfc92e772015-05-12 14:18:14 +00002298 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
2299 // so we will end up with redundant moves to m0.
2300 //
Matt Arsenault4ac341c2016-04-14 21:58:15 +00002301 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
2302
2303 // A Null SDValue creates a glue result.
2304 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
2305 V, Chain);
2306 return SDValue(M0, 0);
Tom Stellardfc92e772015-05-12 14:18:14 +00002307}
2308
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002309SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
2310 SDValue Op,
2311 MVT VT,
2312 unsigned Offset) const {
2313 SDLoc SL(Op);
2314 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
2315 DAG.getEntryNode(), Offset, false);
2316 // The local size values will have the hi 16-bits as zero.
2317 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
2318 DAG.getValueType(VT));
2319}
2320
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002321static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
Matt Arsenaulte0132462016-01-30 05:19:45 +00002322 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002323 "non-hsa intrinsic with hsa target",
2324 DL.getDebugLoc());
2325 DAG.getContext()->diagnose(BadIntrin);
2326 return DAG.getUNDEF(VT);
2327}
2328
2329static SDValue emitRemovedIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
2330 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
2331 "intrinsic not supported on subtarget",
2332 DL.getDebugLoc());
Matt Arsenaulte0132462016-01-30 05:19:45 +00002333 DAG.getContext()->diagnose(BadIntrin);
2334 return DAG.getUNDEF(VT);
2335}
2336
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002337SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2338 SelectionDAG &DAG) const {
2339 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00002340 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002341 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002342
2343 EVT VT = Op.getValueType();
2344 SDLoc DL(Op);
2345 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2346
Sanjay Patela2607012015-09-16 16:31:21 +00002347 // TODO: Should this propagate fast-math-flags?
2348
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002349 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00002350 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault48ab5262016-04-25 19:27:18 +00002351 case Intrinsic::amdgcn_queue_ptr: {
Tom Stellard0b76fc4c2016-09-16 21:34:26 +00002352 if (!Subtarget->isAmdCodeObjectV2()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00002353 DiagnosticInfoUnsupported BadIntrin(
2354 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
2355 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00002356 DAG.getContext()->diagnose(BadIntrin);
2357 return DAG.getUNDEF(VT);
2358 }
2359
Matt Arsenault48ab5262016-04-25 19:27:18 +00002360 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
2361 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00002362 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
Matt Arsenault48ab5262016-04-25 19:27:18 +00002363 TRI->getPreloadedValue(MF, Reg), VT);
2364 }
Jan Veselyfea814d2016-06-21 20:46:20 +00002365 case Intrinsic::amdgcn_implicitarg_ptr: {
2366 unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
2367 return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset);
2368 }
Matt Arsenaultdc4ebad2016-04-29 21:16:52 +00002369 case Intrinsic::amdgcn_kernarg_segment_ptr: {
2370 unsigned Reg
2371 = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
2372 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2373 }
Matt Arsenault8d718dc2016-07-22 17:01:30 +00002374 case Intrinsic::amdgcn_dispatch_id: {
2375 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_ID);
2376 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2377 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002378 case Intrinsic::amdgcn_rcp:
2379 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
2380 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00002381 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002382 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002383 case Intrinsic::amdgcn_rsq_legacy: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002384 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002385 return emitRemovedIntrinsicError(DAG, DL, VT);
2386
2387 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
2388 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002389 case Intrinsic::amdgcn_rcp_legacy: {
2390 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
2391 return emitRemovedIntrinsicError(DAG, DL, VT);
2392 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1));
2393 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +00002394 case Intrinsic::amdgcn_rsq_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002395 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00002396 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00002397
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002398 Type *Type = VT.getTypeForEVT(*DAG.getContext());
2399 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
2400 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
2401
2402 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
2403 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
2404 DAG.getConstantFP(Max, DL, VT));
2405 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
2406 DAG.getConstantFP(Min, DL, VT));
2407 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002408 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002409 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002410 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002411
Tom Stellardec2e43c2014-09-22 15:35:29 +00002412 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2413 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002414 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002415 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002416 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002417
Tom Stellardec2e43c2014-09-22 15:35:29 +00002418 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2419 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002420 case Intrinsic::r600_read_ngroups_z:
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_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002426 case Intrinsic::r600_read_global_size_x:
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::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002432 case Intrinsic::r600_read_global_size_y:
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::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002438 case Intrinsic::r600_read_global_size_z:
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_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002444 case Intrinsic::r600_read_local_size_x:
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
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002448 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2449 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002450 case Intrinsic::r600_read_local_size_y:
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
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002454 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2455 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002456 case Intrinsic::r600_read_local_size_z:
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_Z);
Matt Arsenault43976df2016-01-30 04:25:19 +00002462 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002463 case Intrinsic::r600_read_tgid_x:
Marek Olsak79c05872016-11-25 17:37:09 +00002464 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002465 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002466 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002467 case Intrinsic::r600_read_tgid_y:
Marek Olsak79c05872016-11-25 17:37:09 +00002468 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002469 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002470 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002471 case Intrinsic::r600_read_tgid_z:
Marek Olsak79c05872016-11-25 17:37:09 +00002472 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002473 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002474 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002475 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002476 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002477 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002478 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002479 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002480 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002481 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002482 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002483 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002484 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002485 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002486 case AMDGPUIntrinsic::SI_load_const: {
2487 SDValue Ops[] = {
2488 Op.getOperand(1),
2489 Op.getOperand(2)
2490 };
2491
2492 MachineMemOperand *MMO = MF.getMachineMemOperand(
Justin Lebaradbf09e2016-09-11 01:38:58 +00002493 MachinePointerInfo(),
2494 MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable |
2495 MachineMemOperand::MOInvariant,
2496 VT.getStoreSize(), 4);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002497 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
2498 Op->getVTList(), Ops, VT, MMO);
2499 }
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002500 case AMDGPUIntrinsic::amdgcn_fdiv_fast: {
2501 return lowerFDIV_FAST(Op, DAG);
2502 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002503 case AMDGPUIntrinsic::SI_vs_load_input:
2504 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
2505 Op.getOperand(1),
2506 Op.getOperand(2),
2507 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00002508
Tom Stellard2a9d9472015-05-12 15:00:46 +00002509 case AMDGPUIntrinsic::SI_fs_constant: {
2510 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2511 SDValue Glue = M0.getValue(1);
2512 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
2513 DAG.getConstant(2, DL, MVT::i32), // P0
2514 Op.getOperand(1), Op.getOperand(2), Glue);
2515 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00002516 case AMDGPUIntrinsic::SI_packf16:
2517 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
2518 return DAG.getUNDEF(MVT::i32);
2519 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00002520 case AMDGPUIntrinsic::SI_fs_interp: {
2521 SDValue IJ = Op.getOperand(4);
2522 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2523 DAG.getConstant(0, DL, MVT::i32));
2524 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2525 DAG.getConstant(1, DL, MVT::i32));
Tom Stellard1473f072016-11-26 02:26:04 +00002526 I = DAG.getNode(ISD::BITCAST, DL, MVT::f32, I);
2527 J = DAG.getNode(ISD::BITCAST, DL, MVT::f32, J);
Tom Stellard2a9d9472015-05-12 15:00:46 +00002528 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2529 SDValue Glue = M0.getValue(1);
2530 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
2531 DAG.getVTList(MVT::f32, MVT::Glue),
2532 I, Op.getOperand(1), Op.getOperand(2), Glue);
2533 Glue = SDValue(P1.getNode(), 1);
2534 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
2535 Op.getOperand(1), Op.getOperand(2), Glue);
2536 }
Tom Stellard2187bb82016-12-06 23:52:13 +00002537 case Intrinsic::amdgcn_interp_mov: {
2538 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
2539 SDValue Glue = M0.getValue(1);
2540 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32, Op.getOperand(1),
2541 Op.getOperand(2), Op.getOperand(3), Glue);
2542 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00002543 case Intrinsic::amdgcn_interp_p1: {
2544 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
2545 SDValue Glue = M0.getValue(1);
2546 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
2547 Op.getOperand(2), Op.getOperand(3), Glue);
2548 }
2549 case Intrinsic::amdgcn_interp_p2: {
2550 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
2551 SDValue Glue = SDValue(M0.getNode(), 1);
2552 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
2553 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
2554 Glue);
2555 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002556 case Intrinsic::amdgcn_sin:
2557 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
2558
2559 case Intrinsic::amdgcn_cos:
2560 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
2561
2562 case Intrinsic::amdgcn_log_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002563 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002564 return SDValue();
2565
2566 DiagnosticInfoUnsupported BadIntrin(
2567 *MF.getFunction(), "intrinsic not supported on subtarget",
2568 DL.getDebugLoc());
2569 DAG.getContext()->diagnose(BadIntrin);
2570 return DAG.getUNDEF(VT);
2571 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002572 case Intrinsic::amdgcn_ldexp:
2573 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
2574 Op.getOperand(1), Op.getOperand(2));
Matt Arsenault74015162016-05-28 00:19:52 +00002575
2576 case Intrinsic::amdgcn_fract:
2577 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
2578
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002579 case Intrinsic::amdgcn_class:
2580 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
2581 Op.getOperand(1), Op.getOperand(2));
2582 case Intrinsic::amdgcn_div_fmas:
2583 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
2584 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
2585 Op.getOperand(4));
2586
2587 case Intrinsic::amdgcn_div_fixup:
2588 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
2589 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
2590
2591 case Intrinsic::amdgcn_trig_preop:
2592 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
2593 Op.getOperand(1), Op.getOperand(2));
2594 case Intrinsic::amdgcn_div_scale: {
2595 // 3rd parameter required to be a constant.
2596 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2597 if (!Param)
2598 return DAG.getUNDEF(VT);
2599
2600 // Translate to the operands expected by the machine instruction. The
2601 // first parameter must be the same as the first instruction.
2602 SDValue Numerator = Op.getOperand(1);
2603 SDValue Denominator = Op.getOperand(2);
2604
2605 // Note this order is opposite of the machine instruction's operations,
2606 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
2607 // intrinsic has the numerator as the first operand to match a normal
2608 // division operation.
2609
2610 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
2611
2612 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
2613 Denominator, Numerator);
2614 }
Wei Ding07e03712016-07-28 16:42:13 +00002615 case Intrinsic::amdgcn_icmp: {
2616 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2617 int CondCode = CD->getSExtValue();
2618
2619 if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002620 CondCode >= ICmpInst::Predicate::BAD_ICMP_PREDICATE)
Wei Ding07e03712016-07-28 16:42:13 +00002621 return DAG.getUNDEF(VT);
2622
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002623 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002624 ISD::CondCode CCOpcode = getICmpCondCode(IcInput);
2625 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2626 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2627 }
2628 case Intrinsic::amdgcn_fcmp: {
2629 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2630 int CondCode = CD->getSExtValue();
2631
2632 if (CondCode <= FCmpInst::Predicate::FCMP_FALSE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002633 CondCode >= FCmpInst::Predicate::FCMP_TRUE)
Wei Ding07e03712016-07-28 16:42:13 +00002634 return DAG.getUNDEF(VT);
2635
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002636 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002637 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput);
2638 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2639 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2640 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002641 case Intrinsic::amdgcn_fmul_legacy:
2642 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT,
2643 Op.getOperand(1), Op.getOperand(2));
Matt Arsenaultc96e1de2016-07-18 18:35:05 +00002644 case Intrinsic::amdgcn_sffbh:
2645 case AMDGPUIntrinsic::AMDGPU_flbit_i32: // Legacy name.
2646 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002647 default:
2648 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
2649 }
2650}
2651
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002652SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
2653 SelectionDAG &DAG) const {
2654 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Tom Stellard6f9ef142016-12-20 17:19:44 +00002655 SDLoc DL(Op);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002656 switch (IntrID) {
2657 case Intrinsic::amdgcn_atomic_inc:
2658 case Intrinsic::amdgcn_atomic_dec: {
2659 MemSDNode *M = cast<MemSDNode>(Op);
2660 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
2661 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
2662 SDValue Ops[] = {
2663 M->getOperand(0), // Chain
2664 M->getOperand(2), // Ptr
2665 M->getOperand(3) // Value
2666 };
2667
2668 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
2669 M->getMemoryVT(), M->getMemOperand());
2670 }
Tom Stellard6f9ef142016-12-20 17:19:44 +00002671 case Intrinsic::amdgcn_buffer_load:
2672 case Intrinsic::amdgcn_buffer_load_format: {
2673 SDValue Ops[] = {
2674 Op.getOperand(0), // Chain
2675 Op.getOperand(2), // rsrc
2676 Op.getOperand(3), // vindex
2677 Op.getOperand(4), // offset
2678 Op.getOperand(5), // glc
2679 Op.getOperand(6) // slc
2680 };
2681 MachineFunction &MF = DAG.getMachineFunction();
2682 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
2683
2684 unsigned Opc = (IntrID == Intrinsic::amdgcn_buffer_load) ?
2685 AMDGPUISD::BUFFER_LOAD : AMDGPUISD::BUFFER_LOAD_FORMAT;
2686 EVT VT = Op.getValueType();
2687 EVT IntVT = VT.changeTypeToInteger();
2688
2689 MachineMemOperand *MMO = MF.getMachineMemOperand(
2690 MachinePointerInfo(MFI->getBufferPSV()),
2691 MachineMemOperand::MOLoad,
2692 VT.getStoreSize(), VT.getStoreSize());
2693
2694 return DAG.getMemIntrinsicNode(Opc, DL, Op->getVTList(), Ops, IntVT, MMO);
2695 }
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002696 default:
2697 return SDValue();
2698 }
2699}
2700
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002701SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
2702 SelectionDAG &DAG) const {
2703 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00002704 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002705 SDValue Chain = Op.getOperand(0);
2706 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2707
2708 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00002709 case AMDGPUIntrinsic::SI_sendmsg: {
2710 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
2711 SDValue Glue = Chain.getValue(1);
2712 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
2713 Op.getOperand(2), Glue);
2714 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002715 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002716 SDValue Ops[] = {
2717 Chain,
2718 Op.getOperand(2),
2719 Op.getOperand(3),
2720 Op.getOperand(4),
2721 Op.getOperand(5),
2722 Op.getOperand(6),
2723 Op.getOperand(7),
2724 Op.getOperand(8),
2725 Op.getOperand(9),
2726 Op.getOperand(10),
2727 Op.getOperand(11),
2728 Op.getOperand(12),
2729 Op.getOperand(13),
2730 Op.getOperand(14)
2731 };
2732
2733 EVT VT = Op.getOperand(3).getValueType();
2734
2735 MachineMemOperand *MMO = MF.getMachineMemOperand(
2736 MachinePointerInfo(),
2737 MachineMemOperand::MOStore,
2738 VT.getStoreSize(), 4);
2739 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
2740 Op->getVTList(), Ops, VT, MMO);
2741 }
Matt Arsenault00568682016-07-13 06:04:22 +00002742 case AMDGPUIntrinsic::AMDGPU_kill: {
Matt Arsenault03006fd2016-07-19 16:27:56 +00002743 SDValue Src = Op.getOperand(2);
2744 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) {
Matt Arsenault00568682016-07-13 06:04:22 +00002745 if (!K->isNegative())
2746 return Chain;
Matt Arsenault03006fd2016-07-19 16:27:56 +00002747
2748 SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32);
2749 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne);
Matt Arsenault00568682016-07-13 06:04:22 +00002750 }
2751
Matt Arsenault03006fd2016-07-19 16:27:56 +00002752 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src);
2753 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast);
Matt Arsenault00568682016-07-13 06:04:22 +00002754 }
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00002755 case AMDGPUIntrinsic::SI_export: {
2756 const ConstantSDNode *En = cast<ConstantSDNode>(Op.getOperand(2));
2757 const ConstantSDNode *VM = cast<ConstantSDNode>(Op.getOperand(3));
2758 const ConstantSDNode *Done = cast<ConstantSDNode>(Op.getOperand(4));
2759 const ConstantSDNode *Tgt = cast<ConstantSDNode>(Op.getOperand(5));
2760 const ConstantSDNode *Compr = cast<ConstantSDNode>(Op.getOperand(6));
2761
2762 const SDValue Ops[] = {
2763 Chain,
2764 DAG.getTargetConstant(En->getZExtValue(), DL, MVT::i8),
2765 DAG.getTargetConstant(VM->getZExtValue(), DL, MVT::i1),
2766 DAG.getTargetConstant(Tgt->getZExtValue(), DL, MVT::i8),
2767 DAG.getTargetConstant(Compr->getZExtValue(), DL, MVT::i1),
2768 Op.getOperand(7), // src0
2769 Op.getOperand(8), // src1
2770 Op.getOperand(9), // src2
2771 Op.getOperand(10) // src3
2772 };
2773
2774 unsigned Opc = Done->isNullValue() ?
2775 AMDGPUISD::EXPORT : AMDGPUISD::EXPORT_DONE;
2776 return DAG.getNode(Opc, DL, Op->getVTList(), Ops);
2777 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002778 default:
2779 return SDValue();
2780 }
2781}
2782
Tom Stellard81d871d2013-11-13 23:36:50 +00002783SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2784 SDLoc DL(Op);
2785 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00002786 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00002787 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00002788
Matt Arsenaulta1436412016-02-10 18:21:45 +00002789 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
Matt Arsenault6dfda962016-02-10 18:21:39 +00002790 // FIXME: Copied from PPC
2791 // First, load into 32 bits, then truncate to 1 bit.
2792
2793 SDValue Chain = Load->getChain();
2794 SDValue BasePtr = Load->getBasePtr();
2795 MachineMemOperand *MMO = Load->getMemOperand();
2796
Tom Stellard115a6152016-11-10 16:02:37 +00002797 EVT RealMemVT = (MemVT == MVT::i1) ? MVT::i8 : MVT::i16;
2798
Matt Arsenault6dfda962016-02-10 18:21:39 +00002799 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
Tom Stellard115a6152016-11-10 16:02:37 +00002800 BasePtr, RealMemVT, MMO);
Matt Arsenault6dfda962016-02-10 18:21:39 +00002801
2802 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002803 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00002804 NewLD.getValue(1)
2805 };
2806
2807 return DAG.getMergeValues(Ops, DL);
2808 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002809
Matt Arsenaulta1436412016-02-10 18:21:45 +00002810 if (!MemVT.isVector())
2811 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002812
Matt Arsenaulta1436412016-02-10 18:21:45 +00002813 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
2814 "Custom lowering for non-i32 vectors hasn't been implemented.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002815
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002816 unsigned AS = Load->getAddressSpace();
2817 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
2818 AS, Load->getAlignment())) {
2819 SDValue Ops[2];
2820 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
2821 return DAG.getMergeValues(Ops, DL);
2822 }
2823
Tom Stellardf8e6eaf2016-10-26 14:38:47 +00002824 MachineFunction &MF = DAG.getMachineFunction();
2825 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
2826 // If there is a possibilty that flat instruction access scratch memory
2827 // then we need to use the same legalization rules we use for private.
2828 if (AS == AMDGPUAS::FLAT_ADDRESS)
2829 AS = MFI->hasFlatScratchInit() ?
2830 AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS;
2831
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002832 unsigned NumElements = MemVT.getVectorNumElements();
2833 switch (AS) {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002834 case AMDGPUAS::CONSTANT_ADDRESS:
2835 if (isMemOpUniform(Load))
2836 return SDValue();
2837 // Non-uniform loads will be selected to MUBUF instructions, so they
Alexander Timofeev18009562016-12-08 17:28:47 +00002838 // have the same legalization requirements as global and private
Matt Arsenaulta1436412016-02-10 18:21:45 +00002839 // loads.
2840 //
Justin Bognerb03fd122016-08-17 05:10:15 +00002841 LLVM_FALLTHROUGH;
Alexander Timofeev18009562016-12-08 17:28:47 +00002842 case AMDGPUAS::GLOBAL_ADDRESS: {
Alexander Timofeeva57511c2016-12-15 15:17:19 +00002843 if (Subtarget->getScalarizeGlobalBehavior() && isMemOpUniform(Load) &&
2844 isMemOpHasNoClobberedMemOperand(Load))
Alexander Timofeev18009562016-12-08 17:28:47 +00002845 return SDValue();
2846 // Non-uniform loads will be selected to MUBUF instructions, so they
2847 // have the same legalization requirements as global and private
2848 // loads.
2849 //
2850 }
2851 LLVM_FALLTHROUGH;
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002852 case AMDGPUAS::FLAT_ADDRESS:
2853 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00002854 return SplitVectorLoad(Op, DAG);
2855 // v4 loads are supported for private and global memory.
2856 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002857 case AMDGPUAS::PRIVATE_ADDRESS: {
2858 // Depending on the setting of the private_element_size field in the
2859 // resource descriptor, we can only make private accesses up to a certain
2860 // size.
2861 switch (Subtarget->getMaxPrivateElementSize()) {
2862 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002863 return scalarizeVectorLoad(Load, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002864 case 8:
2865 if (NumElements > 2)
2866 return SplitVectorLoad(Op, DAG);
2867 return SDValue();
2868 case 16:
2869 // Same as global/flat
2870 if (NumElements > 4)
2871 return SplitVectorLoad(Op, DAG);
2872 return SDValue();
2873 default:
2874 llvm_unreachable("unsupported private_element_size");
2875 }
2876 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002877 case AMDGPUAS::LOCAL_ADDRESS: {
2878 if (NumElements > 2)
2879 return SplitVectorLoad(Op, DAG);
2880
2881 if (NumElements == 2)
2882 return SDValue();
2883
Matt Arsenaulta1436412016-02-10 18:21:45 +00002884 // If properly aligned, if we split we might be able to use ds_read_b64.
2885 return SplitVectorLoad(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002886 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00002887 default:
2888 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00002889 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002890}
2891
Tom Stellard0ec134f2014-02-04 17:18:40 +00002892SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2893 if (Op.getValueType() != MVT::i64)
2894 return SDValue();
2895
2896 SDLoc DL(Op);
2897 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002898
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002899 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2900 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002901
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002902 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
2903 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
2904
2905 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
2906 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002907
2908 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
2909
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002910 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
2911 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002912
2913 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
2914
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002915 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002916 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002917}
2918
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002919// Catch division cases where we can use shortcuts with rcp and rsq
2920// instructions.
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002921SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op,
2922 SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002923 SDLoc SL(Op);
2924 SDValue LHS = Op.getOperand(0);
2925 SDValue RHS = Op.getOperand(1);
2926 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002927 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002928
2929 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenaultcdff21b2016-12-22 03:05:44 +00002930 if (Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals()) ||
2931 VT == MVT::f16) {
Matt Arsenault979902b2016-08-02 22:25:04 +00002932 if (CLHS->isExactlyValue(1.0)) {
2933 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
2934 // the CI documentation has a worst case error of 1 ulp.
2935 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
2936 // use it as long as we aren't trying to use denormals.
Matt Arsenaultcdff21b2016-12-22 03:05:44 +00002937 //
2938 // v_rcp_f16 and v_rsq_f16 DO support denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002939
Matt Arsenault979902b2016-08-02 22:25:04 +00002940 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenaultcdff21b2016-12-22 03:05:44 +00002941
Matt Arsenault979902b2016-08-02 22:25:04 +00002942 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
2943 // error seems really high at 2^29 ULP.
2944 if (RHS.getOpcode() == ISD::FSQRT)
2945 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
2946
2947 // 1.0 / x -> rcp(x)
2948 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
2949 }
2950
2951 // Same as for 1.0, but expand the sign out of the constant.
2952 if (CLHS->isExactlyValue(-1.0)) {
2953 // -1.0 / x -> rcp (fneg x)
2954 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2955 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS);
2956 }
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002957 }
2958 }
2959
Wei Dinged0f97f2016-06-09 19:17:15 +00002960 const SDNodeFlags *Flags = Op->getFlags();
2961
2962 if (Unsafe || Flags->hasAllowReciprocal()) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002963 // Turn into multiply by the reciprocal.
2964 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00002965 SDNodeFlags Flags;
2966 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002967 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00002968 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002969 }
2970
2971 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002972}
2973
Tom Stellard8485fa02016-12-07 02:42:15 +00002974static SDValue getFPBinOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL,
2975 EVT VT, SDValue A, SDValue B, SDValue GlueChain) {
2976 if (GlueChain->getNumValues() <= 1) {
2977 return DAG.getNode(Opcode, SL, VT, A, B);
2978 }
2979
2980 assert(GlueChain->getNumValues() == 3);
2981
2982 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue);
2983 switch (Opcode) {
2984 default: llvm_unreachable("no chain equivalent for opcode");
2985 case ISD::FMUL:
2986 Opcode = AMDGPUISD::FMUL_W_CHAIN;
2987 break;
2988 }
2989
2990 return DAG.getNode(Opcode, SL, VTList, GlueChain.getValue(1), A, B,
2991 GlueChain.getValue(2));
2992}
2993
2994static SDValue getFPTernOp(SelectionDAG &DAG, unsigned Opcode, const SDLoc &SL,
2995 EVT VT, SDValue A, SDValue B, SDValue C,
2996 SDValue GlueChain) {
2997 if (GlueChain->getNumValues() <= 1) {
2998 return DAG.getNode(Opcode, SL, VT, A, B, C);
2999 }
3000
3001 assert(GlueChain->getNumValues() == 3);
3002
3003 SDVTList VTList = DAG.getVTList(VT, MVT::Other, MVT::Glue);
3004 switch (Opcode) {
3005 default: llvm_unreachable("no chain equivalent for opcode");
3006 case ISD::FMA:
3007 Opcode = AMDGPUISD::FMA_W_CHAIN;
3008 break;
3009 }
3010
3011 return DAG.getNode(Opcode, SL, VTList, GlueChain.getValue(1), A, B, C,
3012 GlueChain.getValue(2));
3013}
3014
Matt Arsenault4052a572016-12-22 03:05:41 +00003015SDValue SITargetLowering::LowerFDIV16(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaultcdff21b2016-12-22 03:05:44 +00003016 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
3017 return FastLowered;
3018
Matt Arsenault4052a572016-12-22 03:05:41 +00003019 SDLoc SL(Op);
3020 SDValue Src0 = Op.getOperand(0);
3021 SDValue Src1 = Op.getOperand(1);
3022
3023 SDValue CvtSrc0 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src0);
3024 SDValue CvtSrc1 = DAG.getNode(ISD::FP_EXTEND, SL, MVT::f32, Src1);
3025
3026 SDValue RcpSrc1 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, CvtSrc1);
3027 SDValue Quot = DAG.getNode(ISD::FMUL, SL, MVT::f32, CvtSrc0, RcpSrc1);
3028
3029 SDValue FPRoundFlag = DAG.getTargetConstant(0, SL, MVT::i32);
3030 SDValue BestQuot = DAG.getNode(ISD::FP_ROUND, SL, MVT::f16, Quot, FPRoundFlag);
3031
3032 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f16, BestQuot, Src1, Src0);
3033}
3034
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00003035// Faster 2.5 ULP division that does not support denormals.
3036SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const {
3037 SDLoc SL(Op);
3038 SDValue LHS = Op.getOperand(1);
3039 SDValue RHS = Op.getOperand(2);
3040
3041 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
3042
3043 const APFloat K0Val(BitsToFloat(0x6f800000));
3044 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
3045
3046 const APFloat K1Val(BitsToFloat(0x2f800000));
3047 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
3048
3049 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
3050
3051 EVT SetCCVT =
3052 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
3053
3054 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
3055
3056 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
3057
3058 // TODO: Should this propagate fast-math-flags?
3059 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
3060
3061 // rcp does not support denormals.
3062 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
3063
3064 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
3065
3066 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
3067}
3068
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00003069SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00003070 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
Eric Christopher538d09d02016-06-07 20:27:12 +00003071 return FastLowered;
Matt Arsenault22ca3f82014-07-15 23:50:10 +00003072
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00003073 SDLoc SL(Op);
3074 SDValue LHS = Op.getOperand(0);
3075 SDValue RHS = Op.getOperand(1);
3076
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003077 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003078
Wei Dinged0f97f2016-06-09 19:17:15 +00003079 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003080
Tom Stellard8485fa02016-12-07 02:42:15 +00003081 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT,
3082 RHS, RHS, LHS);
3083 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT,
3084 LHS, RHS, LHS);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003085
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00003086 // Denominator is scaled to not be denormal, so using rcp is ok.
Tom Stellard8485fa02016-12-07 02:42:15 +00003087 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32,
3088 DenominatorScaled);
3089 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32,
3090 DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003091
Tom Stellard8485fa02016-12-07 02:42:15 +00003092 const unsigned Denorm32Reg = AMDGPU::Hwreg::ID_MODE |
3093 (4 << AMDGPU::Hwreg::OFFSET_SHIFT_) |
3094 (1 << AMDGPU::Hwreg::WIDTH_M1_SHIFT_);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003095
Tom Stellard8485fa02016-12-07 02:42:15 +00003096 const SDValue BitField = DAG.getTargetConstant(Denorm32Reg, SL, MVT::i16);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003097
Tom Stellard8485fa02016-12-07 02:42:15 +00003098 if (!Subtarget->hasFP32Denormals()) {
3099 SDVTList BindParamVTs = DAG.getVTList(MVT::Other, MVT::Glue);
3100 const SDValue EnableDenormValue = DAG.getConstant(FP_DENORM_FLUSH_NONE,
3101 SL, MVT::i32);
3102 SDValue EnableDenorm = DAG.getNode(AMDGPUISD::SETREG, SL, BindParamVTs,
3103 DAG.getEntryNode(),
3104 EnableDenormValue, BitField);
3105 SDValue Ops[3] = {
3106 NegDivScale0,
3107 EnableDenorm.getValue(0),
3108 EnableDenorm.getValue(1)
3109 };
Matt Arsenault37fefd62016-06-10 02:18:02 +00003110
Tom Stellard8485fa02016-12-07 02:42:15 +00003111 NegDivScale0 = DAG.getMergeValues(Ops, SL);
3112 }
3113
3114 SDValue Fma0 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0,
3115 ApproxRcp, One, NegDivScale0);
3116
3117 SDValue Fma1 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp,
3118 ApproxRcp, Fma0);
3119
3120 SDValue Mul = getFPBinOp(DAG, ISD::FMUL, SL, MVT::f32, NumeratorScaled,
3121 Fma1, Fma1);
3122
3123 SDValue Fma2 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Mul,
3124 NumeratorScaled, Mul);
3125
3126 SDValue Fma3 = getFPTernOp(DAG, ISD::FMA,SL, MVT::f32, Fma2, Fma1, Mul, Fma2);
3127
3128 SDValue Fma4 = getFPTernOp(DAG, ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3,
3129 NumeratorScaled, Fma3);
3130
3131 if (!Subtarget->hasFP32Denormals()) {
3132 const SDValue DisableDenormValue =
3133 DAG.getConstant(FP_DENORM_FLUSH_IN_FLUSH_OUT, SL, MVT::i32);
3134 SDValue DisableDenorm = DAG.getNode(AMDGPUISD::SETREG, SL, MVT::Other,
3135 Fma4.getValue(1),
3136 DisableDenormValue,
3137 BitField,
3138 Fma4.getValue(2));
3139
3140 SDValue OutputChain = DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
3141 DisableDenorm, DAG.getRoot());
3142 DAG.setRoot(OutputChain);
3143 }
Matt Arsenault37fefd62016-06-10 02:18:02 +00003144
Wei Dinged0f97f2016-06-09 19:17:15 +00003145 SDValue Scale = NumeratorScaled.getValue(1);
Tom Stellard8485fa02016-12-07 02:42:15 +00003146 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32,
3147 Fma4, Fma1, Fma3, Scale);
Matt Arsenault37fefd62016-06-10 02:18:02 +00003148
Wei Dinged0f97f2016-06-09 19:17:15 +00003149 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00003150}
3151
3152SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00003153 if (DAG.getTarget().Options.UnsafeFPMath)
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00003154 return lowerFastUnsafeFDIV(Op, DAG);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00003155
3156 SDLoc SL(Op);
3157 SDValue X = Op.getOperand(0);
3158 SDValue Y = Op.getOperand(1);
3159
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003160 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00003161
3162 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
3163
3164 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
3165
3166 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
3167
3168 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
3169
3170 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
3171
3172 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
3173
3174 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
3175
3176 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
3177
3178 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
3179 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
3180
3181 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
3182 NegDivScale0, Mul, DivScale1);
3183
3184 SDValue Scale;
3185
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003186 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00003187 // Workaround a hardware bug on SI where the condition output from div_scale
3188 // is not usable.
3189
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003190 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00003191
3192 // Figure out if the scale to use for div_fmas.
3193 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
3194 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
3195 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
3196 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
3197
3198 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
3199 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
3200
3201 SDValue Scale0Hi
3202 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
3203 SDValue Scale1Hi
3204 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
3205
3206 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
3207 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
3208 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
3209 } else {
3210 Scale = DivScale1.getValue(1);
3211 }
3212
3213 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
3214 Fma4, Fma3, Mul, Scale);
3215
3216 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00003217}
3218
3219SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
3220 EVT VT = Op.getValueType();
3221
3222 if (VT == MVT::f32)
3223 return LowerFDIV32(Op, DAG);
3224
3225 if (VT == MVT::f64)
3226 return LowerFDIV64(Op, DAG);
3227
Matt Arsenault4052a572016-12-22 03:05:41 +00003228 if (VT == MVT::f16)
3229 return LowerFDIV16(Op, DAG);
3230
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00003231 llvm_unreachable("Unexpected type for fdiv");
3232}
3233
Tom Stellard81d871d2013-11-13 23:36:50 +00003234SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
3235 SDLoc DL(Op);
3236 StoreSDNode *Store = cast<StoreSDNode>(Op);
3237 EVT VT = Store->getMemoryVT();
3238
Matt Arsenault95245662016-02-11 05:32:46 +00003239 if (VT == MVT::i1) {
3240 return DAG.getTruncStore(Store->getChain(), DL,
3241 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
3242 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00003243 }
3244
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003245 assert(VT.isVector() &&
3246 Store->getValue().getValueType().getScalarType() == MVT::i32);
3247
3248 unsigned AS = Store->getAddressSpace();
3249 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
3250 AS, Store->getAlignment())) {
3251 return expandUnalignedStore(Store, DAG);
3252 }
Tom Stellard81d871d2013-11-13 23:36:50 +00003253
Tom Stellardf8e6eaf2016-10-26 14:38:47 +00003254 MachineFunction &MF = DAG.getMachineFunction();
3255 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
3256 // If there is a possibilty that flat instruction access scratch memory
3257 // then we need to use the same legalization rules we use for private.
3258 if (AS == AMDGPUAS::FLAT_ADDRESS)
3259 AS = MFI->hasFlatScratchInit() ?
3260 AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS;
3261
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003262 unsigned NumElements = VT.getVectorNumElements();
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003263 switch (AS) {
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003264 case AMDGPUAS::GLOBAL_ADDRESS:
3265 case AMDGPUAS::FLAT_ADDRESS:
3266 if (NumElements > 4)
3267 return SplitVectorStore(Op, DAG);
3268 return SDValue();
3269 case AMDGPUAS::PRIVATE_ADDRESS: {
3270 switch (Subtarget->getMaxPrivateElementSize()) {
3271 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00003272 return scalarizeVectorStore(Store, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003273 case 8:
3274 if (NumElements > 2)
3275 return SplitVectorStore(Op, DAG);
3276 return SDValue();
3277 case 16:
3278 if (NumElements > 4)
3279 return SplitVectorStore(Op, DAG);
3280 return SDValue();
3281 default:
3282 llvm_unreachable("unsupported private_element_size");
3283 }
3284 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003285 case AMDGPUAS::LOCAL_ADDRESS: {
3286 if (NumElements > 2)
3287 return SplitVectorStore(Op, DAG);
3288
3289 if (NumElements == 2)
3290 return Op;
3291
Matt Arsenault95245662016-02-11 05:32:46 +00003292 // If properly aligned, if we split we might be able to use ds_write_b64.
3293 return SplitVectorStore(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003294 }
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003295 default:
3296 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00003297 }
Tom Stellard81d871d2013-11-13 23:36:50 +00003298}
3299
Matt Arsenaultad14ce82014-07-19 18:44:39 +00003300SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003301 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00003302 EVT VT = Op.getValueType();
3303 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00003304 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003305 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
3306 DAG.getNode(ISD::FMUL, DL, VT, Arg,
3307 DAG.getConstantFP(0.5/M_PI, DL,
3308 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00003309
3310 switch (Op.getOpcode()) {
3311 case ISD::FCOS:
3312 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
3313 case ISD::FSIN:
3314 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
3315 default:
3316 llvm_unreachable("Wrong trig opcode");
3317 }
3318}
3319
Tom Stellard354a43c2016-04-01 18:27:37 +00003320SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
3321 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
3322 assert(AtomicNode->isCompareAndSwap());
3323 unsigned AS = AtomicNode->getAddressSpace();
3324
3325 // No custom lowering required for local address space
3326 if (!isFlatGlobalAddrSpace(AS))
3327 return Op;
3328
3329 // Non-local address space requires custom lowering for atomic compare
3330 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
3331 SDLoc DL(Op);
3332 SDValue ChainIn = Op.getOperand(0);
3333 SDValue Addr = Op.getOperand(1);
3334 SDValue Old = Op.getOperand(2);
3335 SDValue New = Op.getOperand(3);
3336 EVT VT = Op.getValueType();
3337 MVT SimpleVT = VT.getSimpleVT();
3338 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
3339
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003340 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
Tom Stellard354a43c2016-04-01 18:27:37 +00003341 SDValue Ops[] = { ChainIn, Addr, NewOld };
Matt Arsenault88701812016-06-09 23:42:48 +00003342
3343 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(),
3344 Ops, VT, AtomicNode->getMemOperand());
Tom Stellard354a43c2016-04-01 18:27:37 +00003345}
3346
Tom Stellard75aadc22012-12-11 21:25:42 +00003347//===----------------------------------------------------------------------===//
3348// Custom DAG optimizations
3349//===----------------------------------------------------------------------===//
3350
Matt Arsenault364a6742014-06-11 17:50:44 +00003351SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00003352 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00003353 EVT VT = N->getValueType(0);
3354 EVT ScalarVT = VT.getScalarType();
3355 if (ScalarVT != MVT::f32)
3356 return SDValue();
3357
3358 SelectionDAG &DAG = DCI.DAG;
3359 SDLoc DL(N);
3360
3361 SDValue Src = N->getOperand(0);
3362 EVT SrcVT = Src.getValueType();
3363
3364 // TODO: We could try to match extracting the higher bytes, which would be
3365 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
3366 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
3367 // about in practice.
3368 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
3369 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
3370 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
3371 DCI.AddToWorklist(Cvt.getNode());
3372 return Cvt;
3373 }
3374 }
3375
Matt Arsenault364a6742014-06-11 17:50:44 +00003376 return SDValue();
3377}
3378
Eric Christopher6c5b5112015-03-11 18:43:21 +00003379/// \brief Return true if the given offset Size in bytes can be folded into
3380/// the immediate offsets of a memory instruction for the given address space.
3381static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003382 const SISubtarget &STI) {
Eric Christopher6c5b5112015-03-11 18:43:21 +00003383 switch (AS) {
3384 case AMDGPUAS::GLOBAL_ADDRESS: {
3385 // MUBUF instructions a 12-bit offset in bytes.
3386 return isUInt<12>(OffsetSize);
3387 }
3388 case AMDGPUAS::CONSTANT_ADDRESS: {
3389 // SMRD instructions have an 8-bit offset in dwords on SI and
3390 // a 20-bit offset in bytes on VI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003391 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Eric Christopher6c5b5112015-03-11 18:43:21 +00003392 return isUInt<20>(OffsetSize);
3393 else
3394 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
3395 }
3396 case AMDGPUAS::LOCAL_ADDRESS:
3397 case AMDGPUAS::REGION_ADDRESS: {
3398 // The single offset versions have a 16-bit offset in bytes.
3399 return isUInt<16>(OffsetSize);
3400 }
3401 case AMDGPUAS::PRIVATE_ADDRESS:
3402 // Indirect register addressing does not use any offsets.
3403 default:
3404 return 0;
3405 }
3406}
3407
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003408// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
3409
3410// This is a variant of
3411// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
3412//
3413// The normal DAG combiner will do this, but only if the add has one use since
3414// that would increase the number of instructions.
3415//
3416// This prevents us from seeing a constant offset that can be folded into a
3417// memory instruction's addressing mode. If we know the resulting add offset of
3418// a pointer can be folded into an addressing offset, we can replace the pointer
3419// operand with the add of new constant offset. This eliminates one of the uses,
3420// and may allow the remaining use to also be simplified.
3421//
3422SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
3423 unsigned AddrSpace,
3424 DAGCombinerInfo &DCI) const {
3425 SDValue N0 = N->getOperand(0);
3426 SDValue N1 = N->getOperand(1);
3427
3428 if (N0.getOpcode() != ISD::ADD)
3429 return SDValue();
3430
3431 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
3432 if (!CN1)
3433 return SDValue();
3434
3435 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3436 if (!CAdd)
3437 return SDValue();
3438
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003439 // If the resulting offset is too large, we can't fold it into the addressing
3440 // mode offset.
3441 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003442 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget()))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003443 return SDValue();
3444
3445 SelectionDAG &DAG = DCI.DAG;
3446 SDLoc SL(N);
3447 EVT VT = N->getValueType(0);
3448
3449 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003450 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003451
3452 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
3453}
3454
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003455SDValue SITargetLowering::performMemSDNodeCombine(MemSDNode *N,
3456 DAGCombinerInfo &DCI) const {
3457 SDValue Ptr = N->getBasePtr();
3458 SelectionDAG &DAG = DCI.DAG;
3459 SDLoc SL(N);
3460
3461 // TODO: We could also do this for multiplies.
3462 unsigned AS = N->getAddressSpace();
3463 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
3464 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
3465 if (NewPtr) {
3466 SmallVector<SDValue, 8> NewOps(N->op_begin(), N->op_end());
3467
3468 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
3469 return SDValue(DAG.UpdateNodeOperands(N, NewOps), 0);
3470 }
3471 }
3472
3473 return SDValue();
3474}
3475
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003476static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) {
3477 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) ||
3478 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) ||
3479 (Opc == ISD::XOR && Val == 0);
3480}
3481
3482// Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This
3483// will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit
3484// integer combine opportunities since most 64-bit operations are decomposed
3485// this way. TODO: We won't want this for SALU especially if it is an inline
3486// immediate.
3487SDValue SITargetLowering::splitBinaryBitConstantOp(
3488 DAGCombinerInfo &DCI,
3489 const SDLoc &SL,
3490 unsigned Opc, SDValue LHS,
3491 const ConstantSDNode *CRHS) const {
3492 uint64_t Val = CRHS->getZExtValue();
3493 uint32_t ValLo = Lo_32(Val);
3494 uint32_t ValHi = Hi_32(Val);
3495 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
3496
3497 if ((bitOpWithConstantIsReducible(Opc, ValLo) ||
3498 bitOpWithConstantIsReducible(Opc, ValHi)) ||
3499 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) {
3500 // If we need to materialize a 64-bit immediate, it will be split up later
3501 // anyway. Avoid creating the harder to understand 64-bit immediate
3502 // materialization.
3503 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi);
3504 }
3505
3506 return SDValue();
3507}
3508
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003509SDValue SITargetLowering::performAndCombine(SDNode *N,
3510 DAGCombinerInfo &DCI) const {
3511 if (DCI.isBeforeLegalize())
3512 return SDValue();
3513
3514 SelectionDAG &DAG = DCI.DAG;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003515 EVT VT = N->getValueType(0);
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003516 SDValue LHS = N->getOperand(0);
3517 SDValue RHS = N->getOperand(1);
3518
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003519
3520 if (VT == MVT::i64) {
3521 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
3522 if (CRHS) {
3523 if (SDValue Split
3524 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS))
3525 return Split;
3526 }
3527 }
3528
3529 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
3530 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
3531 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) {
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003532 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
3533 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
3534
3535 SDValue X = LHS.getOperand(0);
3536 SDValue Y = RHS.getOperand(0);
3537 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
3538 return SDValue();
3539
3540 if (LCC == ISD::SETO) {
3541 if (X != LHS.getOperand(1))
3542 return SDValue();
3543
3544 if (RCC == ISD::SETUNE) {
3545 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
3546 if (!C1 || !C1->isInfinity() || C1->isNegative())
3547 return SDValue();
3548
3549 const uint32_t Mask = SIInstrFlags::N_NORMAL |
3550 SIInstrFlags::N_SUBNORMAL |
3551 SIInstrFlags::N_ZERO |
3552 SIInstrFlags::P_ZERO |
3553 SIInstrFlags::P_SUBNORMAL |
3554 SIInstrFlags::P_NORMAL;
3555
3556 static_assert(((~(SIInstrFlags::S_NAN |
3557 SIInstrFlags::Q_NAN |
3558 SIInstrFlags::N_INFINITY |
3559 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
3560 "mask not equal");
3561
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003562 SDLoc DL(N);
3563 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
3564 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003565 }
3566 }
3567 }
3568
3569 return SDValue();
3570}
3571
Matt Arsenaultf2290332015-01-06 23:00:39 +00003572SDValue SITargetLowering::performOrCombine(SDNode *N,
3573 DAGCombinerInfo &DCI) const {
3574 SelectionDAG &DAG = DCI.DAG;
3575 SDValue LHS = N->getOperand(0);
3576 SDValue RHS = N->getOperand(1);
3577
Matt Arsenault3b082382016-04-12 18:24:38 +00003578 EVT VT = N->getValueType(0);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003579 if (VT == MVT::i1) {
3580 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
3581 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
3582 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
3583 SDValue Src = LHS.getOperand(0);
3584 if (Src != RHS.getOperand(0))
3585 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003586
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003587 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
3588 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
3589 if (!CLHS || !CRHS)
3590 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003591
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003592 // Only 10 bits are used.
3593 static const uint32_t MaxMask = 0x3ff;
Matt Arsenault3b082382016-04-12 18:24:38 +00003594
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003595 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
3596 SDLoc DL(N);
3597 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
3598 Src, DAG.getConstant(NewMask, DL, MVT::i32));
3599 }
Matt Arsenault3b082382016-04-12 18:24:38 +00003600
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003601 return SDValue();
3602 }
3603
3604 if (VT != MVT::i64)
3605 return SDValue();
3606
3607 // TODO: This could be a generic combine with a predicate for extracting the
3608 // high half of an integer being free.
3609
3610 // (or i64:x, (zero_extend i32:y)) ->
3611 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
3612 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
3613 RHS.getOpcode() != ISD::ZERO_EXTEND)
3614 std::swap(LHS, RHS);
3615
3616 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
3617 SDValue ExtSrc = RHS.getOperand(0);
3618 EVT SrcVT = ExtSrc.getValueType();
3619 if (SrcVT == MVT::i32) {
3620 SDLoc SL(N);
3621 SDValue LowLHS, HiBits;
3622 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
3623 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
3624
3625 DCI.AddToWorklist(LowOr.getNode());
3626 DCI.AddToWorklist(HiBits.getNode());
3627
3628 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3629 LowOr, HiBits);
3630 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
Matt Arsenault3b082382016-04-12 18:24:38 +00003631 }
3632 }
3633
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003634 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
3635 if (CRHS) {
3636 if (SDValue Split
3637 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS))
3638 return Split;
3639 }
Matt Arsenaultf2290332015-01-06 23:00:39 +00003640
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003641 return SDValue();
3642}
Matt Arsenaultf2290332015-01-06 23:00:39 +00003643
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003644SDValue SITargetLowering::performXorCombine(SDNode *N,
3645 DAGCombinerInfo &DCI) const {
3646 EVT VT = N->getValueType(0);
3647 if (VT != MVT::i64)
3648 return SDValue();
Matt Arsenaultf2290332015-01-06 23:00:39 +00003649
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003650 SDValue LHS = N->getOperand(0);
3651 SDValue RHS = N->getOperand(1);
3652
3653 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
3654 if (CRHS) {
3655 if (SDValue Split
3656 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS))
3657 return Split;
Matt Arsenaultf2290332015-01-06 23:00:39 +00003658 }
3659
3660 return SDValue();
3661}
3662
3663SDValue SITargetLowering::performClassCombine(SDNode *N,
3664 DAGCombinerInfo &DCI) const {
3665 SelectionDAG &DAG = DCI.DAG;
3666 SDValue Mask = N->getOperand(1);
3667
3668 // fp_class x, 0 -> false
3669 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
3670 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003671 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003672 }
3673
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003674 if (N->getOperand(0).isUndef())
3675 return DAG.getUNDEF(MVT::i1);
3676
Matt Arsenaultf2290332015-01-06 23:00:39 +00003677 return SDValue();
3678}
3679
Matt Arsenault9cd90712016-04-14 01:42:16 +00003680// Constant fold canonicalize.
3681SDValue SITargetLowering::performFCanonicalizeCombine(
3682 SDNode *N,
3683 DAGCombinerInfo &DCI) const {
3684 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
3685 if (!CFP)
3686 return SDValue();
3687
3688 SelectionDAG &DAG = DCI.DAG;
3689 const APFloat &C = CFP->getValueAPF();
3690
3691 // Flush denormals to 0 if not enabled.
3692 if (C.isDenormal()) {
3693 EVT VT = N->getValueType(0);
3694 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
3695 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3696
3697 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
3698 return DAG.getConstantFP(0.0, SDLoc(N), VT);
Matt Arsenaultce841302016-12-22 03:05:37 +00003699
3700 if (VT == MVT::f16 && !Subtarget->hasFP16Denormals())
3701 return DAG.getConstantFP(0.0, SDLoc(N), VT);
Matt Arsenault9cd90712016-04-14 01:42:16 +00003702 }
3703
3704 if (C.isNaN()) {
3705 EVT VT = N->getValueType(0);
3706 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
3707 if (C.isSignaling()) {
3708 // Quiet a signaling NaN.
3709 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3710 }
3711
3712 // Make sure it is the canonical NaN bitpattern.
3713 //
3714 // TODO: Can we use -1 as the canonical NaN value since it's an inline
3715 // immediate?
3716 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
3717 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3718 }
3719
3720 return SDValue(CFP, 0);
3721}
3722
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003723static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
3724 switch (Opc) {
3725 case ISD::FMAXNUM:
3726 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003727 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003728 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003729 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003730 return AMDGPUISD::UMAX3;
3731 case ISD::FMINNUM:
3732 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003733 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003734 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003735 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003736 return AMDGPUISD::UMIN3;
3737 default:
3738 llvm_unreachable("Not a min/max opcode");
3739 }
3740}
3741
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003742static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3743 SDValue Op0, SDValue Op1, bool Signed) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003744 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
3745 if (!K1)
3746 return SDValue();
3747
3748 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
3749 if (!K0)
3750 return SDValue();
3751
Matt Arsenaultf639c322016-01-28 20:53:42 +00003752 if (Signed) {
3753 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
3754 return SDValue();
3755 } else {
3756 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
3757 return SDValue();
3758 }
3759
3760 EVT VT = K0->getValueType(0);
Tom Stellard115a6152016-11-10 16:02:37 +00003761
3762 MVT NVT = MVT::i32;
3763 unsigned ExtOp = Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3764
3765 SDValue Tmp1, Tmp2, Tmp3;
3766 Tmp1 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(0));
3767 Tmp2 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(1));
3768 Tmp3 = DAG.getNode(ExtOp, SL, NVT, Op1);
3769
3770 if (VT == MVT::i16) {
3771 Tmp1 = DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, NVT,
3772 Tmp1, Tmp2, Tmp3);
3773
3774 return DAG.getNode(ISD::TRUNCATE, SL, VT, Tmp1);
3775 } else
3776 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
3777 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
Matt Arsenaultf639c322016-01-28 20:53:42 +00003778}
3779
3780static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
3781 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
3782 return true;
3783
3784 return DAG.isKnownNeverNaN(Op);
3785}
3786
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003787static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3788 SDValue Op0, SDValue Op1) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003789 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
3790 if (!K1)
3791 return SDValue();
3792
3793 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
3794 if (!K0)
3795 return SDValue();
3796
3797 // Ordered >= (although NaN inputs should have folded away by now).
3798 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
3799 if (Cmp == APFloat::cmpGreaterThan)
3800 return SDValue();
3801
3802 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
3803 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
3804 // give the other result, which is different from med3 with a NaN input.
3805 SDValue Var = Op0.getOperand(0);
3806 if (!isKnownNeverSNan(DAG, Var))
3807 return SDValue();
3808
3809 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
3810 Var, SDValue(K0, 0), SDValue(K1, 0));
3811}
3812
3813SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
3814 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003815 SelectionDAG &DAG = DCI.DAG;
3816
3817 unsigned Opc = N->getOpcode();
3818 SDValue Op0 = N->getOperand(0);
3819 SDValue Op1 = N->getOperand(1);
3820
3821 // Only do this if the inner op has one use since this will just increases
3822 // register pressure for no benefit.
3823
Matt Arsenault5b39b342016-01-28 20:53:48 +00003824 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
3825 // max(max(a, b), c) -> max3(a, b, c)
3826 // min(min(a, b), c) -> min3(a, b, c)
3827 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
3828 SDLoc DL(N);
3829 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3830 DL,
3831 N->getValueType(0),
3832 Op0.getOperand(0),
3833 Op0.getOperand(1),
3834 Op1);
3835 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003836
Matt Arsenault5b39b342016-01-28 20:53:48 +00003837 // Try commuted.
3838 // max(a, max(b, c)) -> max3(a, b, c)
3839 // min(a, min(b, c)) -> min3(a, b, c)
3840 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
3841 SDLoc DL(N);
3842 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3843 DL,
3844 N->getValueType(0),
3845 Op0,
3846 Op1.getOperand(0),
3847 Op1.getOperand(1));
3848 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003849 }
3850
Matt Arsenaultf639c322016-01-28 20:53:42 +00003851 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
3852 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
3853 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
3854 return Med3;
3855 }
3856
3857 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
3858 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
3859 return Med3;
3860 }
3861
3862 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00003863 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
3864 (Opc == AMDGPUISD::FMIN_LEGACY &&
3865 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00003866 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
3867 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
3868 return Res;
3869 }
3870
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003871 return SDValue();
3872}
3873
Matt Arsenault46e6b7a2016-12-22 04:03:35 +00003874unsigned SITargetLowering::getFusedOpcode(const SelectionDAG &DAG,
3875 const SDNode *N0,
3876 const SDNode *N1) const {
3877 EVT VT = N0->getValueType(0);
3878
Matt Arsenault770ec862016-12-22 03:55:35 +00003879 // Only do this if we are not trying to support denormals. v_mad_f32 does not
3880 // support denormals ever.
3881 if ((VT == MVT::f32 && !Subtarget->hasFP32Denormals()) ||
3882 (VT == MVT::f16 && !Subtarget->hasFP16Denormals()))
3883 return ISD::FMAD;
3884
3885 const TargetOptions &Options = DAG.getTarget().Options;
Matt Arsenault46e6b7a2016-12-22 04:03:35 +00003886 if ((Options.AllowFPOpFusion == FPOpFusion::Fast ||
3887 Options.UnsafeFPMath ||
3888 (cast<BinaryWithFlagsSDNode>(N0)->Flags.hasUnsafeAlgebra() &&
3889 cast<BinaryWithFlagsSDNode>(N1)->Flags.hasUnsafeAlgebra())) &&
Matt Arsenault770ec862016-12-22 03:55:35 +00003890 isFMAFasterThanFMulAndFAdd(VT)) {
3891 return ISD::FMA;
3892 }
3893
3894 return 0;
3895}
3896
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003897SDValue SITargetLowering::performFAddCombine(SDNode *N,
3898 DAGCombinerInfo &DCI) const {
3899 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3900 return SDValue();
3901
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003902 SelectionDAG &DAG = DCI.DAG;
Matt Arsenault770ec862016-12-22 03:55:35 +00003903 EVT VT = N->getValueType(0);
3904 assert(!VT.isVector());
3905
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003906 SDLoc SL(N);
3907 SDValue LHS = N->getOperand(0);
3908 SDValue RHS = N->getOperand(1);
3909
3910 // These should really be instruction patterns, but writing patterns with
3911 // source modiifiers is a pain.
3912
3913 // fadd (fadd (a, a), b) -> mad 2.0, a, b
3914 if (LHS.getOpcode() == ISD::FADD) {
3915 SDValue A = LHS.getOperand(0);
3916 if (A == LHS.getOperand(1)) {
Matt Arsenault46e6b7a2016-12-22 04:03:35 +00003917 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode());
Matt Arsenault770ec862016-12-22 03:55:35 +00003918 if (FusedOp != 0) {
3919 const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
Matt Arsenaulte7d8ed32016-12-22 04:03:40 +00003920 return DAG.getNode(FusedOp, SL, VT, A, Two, RHS);
Matt Arsenault770ec862016-12-22 03:55:35 +00003921 }
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003922 }
3923 }
3924
3925 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
3926 if (RHS.getOpcode() == ISD::FADD) {
3927 SDValue A = RHS.getOperand(0);
3928 if (A == RHS.getOperand(1)) {
Matt Arsenault46e6b7a2016-12-22 04:03:35 +00003929 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode());
Matt Arsenault770ec862016-12-22 03:55:35 +00003930 if (FusedOp != 0) {
3931 const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
Matt Arsenaulte7d8ed32016-12-22 04:03:40 +00003932 return DAG.getNode(FusedOp, SL, VT, A, Two, LHS);
Matt Arsenault770ec862016-12-22 03:55:35 +00003933 }
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003934 }
3935 }
3936
3937 return SDValue();
3938}
3939
3940SDValue SITargetLowering::performFSubCombine(SDNode *N,
3941 DAGCombinerInfo &DCI) const {
3942 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3943 return SDValue();
3944
3945 SelectionDAG &DAG = DCI.DAG;
3946 SDLoc SL(N);
3947 EVT VT = N->getValueType(0);
3948 assert(!VT.isVector());
3949
3950 // Try to get the fneg to fold into the source modifier. This undoes generic
3951 // DAG combines and folds them into the mad.
3952 //
3953 // Only do this if we are not trying to support denormals. v_mad_f32 does
3954 // not support denormals ever.
Matt Arsenault770ec862016-12-22 03:55:35 +00003955 SDValue LHS = N->getOperand(0);
3956 SDValue RHS = N->getOperand(1);
3957 if (LHS.getOpcode() == ISD::FADD) {
3958 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
3959 SDValue A = LHS.getOperand(0);
3960 if (A == LHS.getOperand(1)) {
Matt Arsenault46e6b7a2016-12-22 04:03:35 +00003961 unsigned FusedOp = getFusedOpcode(DAG, N, LHS.getNode());
Matt Arsenault770ec862016-12-22 03:55:35 +00003962 if (FusedOp != 0){
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003963 const SDValue Two = DAG.getConstantFP(2.0, SL, VT);
3964 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3965
Matt Arsenaulte7d8ed32016-12-22 04:03:40 +00003966 return DAG.getNode(FusedOp, SL, VT, A, Two, NegRHS);
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003967 }
3968 }
Matt Arsenault770ec862016-12-22 03:55:35 +00003969 }
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003970
Matt Arsenault770ec862016-12-22 03:55:35 +00003971 if (RHS.getOpcode() == ISD::FADD) {
3972 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003973
Matt Arsenault770ec862016-12-22 03:55:35 +00003974 SDValue A = RHS.getOperand(0);
3975 if (A == RHS.getOperand(1)) {
Matt Arsenault46e6b7a2016-12-22 04:03:35 +00003976 unsigned FusedOp = getFusedOpcode(DAG, N, RHS.getNode());
Matt Arsenault770ec862016-12-22 03:55:35 +00003977 if (FusedOp != 0){
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003978 const SDValue NegTwo = DAG.getConstantFP(-2.0, SL, VT);
Matt Arsenaulte7d8ed32016-12-22 04:03:40 +00003979 return DAG.getNode(FusedOp, SL, VT, A, NegTwo, LHS);
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00003980 }
3981 }
3982 }
3983
3984 return SDValue();
3985}
3986
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003987SDValue SITargetLowering::performSetCCCombine(SDNode *N,
3988 DAGCombinerInfo &DCI) const {
3989 SelectionDAG &DAG = DCI.DAG;
3990 SDLoc SL(N);
3991
3992 SDValue LHS = N->getOperand(0);
3993 SDValue RHS = N->getOperand(1);
3994 EVT VT = LHS.getValueType();
3995
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00003996 if (VT != MVT::f32 && VT != MVT::f64 && (Subtarget->has16BitInsts() &&
3997 VT != MVT::f16))
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003998 return SDValue();
3999
4000 // Match isinf pattern
4001 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
4002 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
4003 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
4004 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
4005 if (!CRHS)
4006 return SDValue();
4007
4008 const APFloat &APF = CRHS->getValueAPF();
4009 if (APF.isInfinity() && !APF.isNegative()) {
4010 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004011 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
4012 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00004013 }
4014 }
4015
4016 return SDValue();
4017}
4018
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00004019SDValue SITargetLowering::performCvtF32UByteNCombine(SDNode *N,
4020 DAGCombinerInfo &DCI) const {
4021 SelectionDAG &DAG = DCI.DAG;
4022 SDLoc SL(N);
4023 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
4024
4025 SDValue Src = N->getOperand(0);
4026 SDValue Srl = N->getOperand(0);
4027 if (Srl.getOpcode() == ISD::ZERO_EXTEND)
4028 Srl = Srl.getOperand(0);
4029
4030 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero.
4031 if (Srl.getOpcode() == ISD::SRL) {
4032 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
4033 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
4034 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
4035
4036 if (const ConstantSDNode *C =
4037 dyn_cast<ConstantSDNode>(Srl.getOperand(1))) {
4038 Srl = DAG.getZExtOrTrunc(Srl.getOperand(0), SDLoc(Srl.getOperand(0)),
4039 EVT(MVT::i32));
4040
4041 unsigned SrcOffset = C->getZExtValue() + 8 * Offset;
4042 if (SrcOffset < 32 && SrcOffset % 8 == 0) {
4043 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, SL,
4044 MVT::f32, Srl);
4045 }
4046 }
4047 }
4048
4049 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
4050
4051 APInt KnownZero, KnownOne;
4052 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
4053 !DCI.isBeforeLegalizeOps());
4054 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
4055 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
4056 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
4057 DCI.CommitTargetLoweringOpt(TLO);
4058 }
4059
4060 return SDValue();
4061}
4062
Tom Stellard75aadc22012-12-11 21:25:42 +00004063SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
4064 DAGCombinerInfo &DCI) const {
Tom Stellard75aadc22012-12-11 21:25:42 +00004065 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00004066 default:
4067 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00004068 case ISD::FADD:
4069 return performFAddCombine(N, DCI);
4070 case ISD::FSUB:
4071 return performFSubCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00004072 case ISD::SETCC:
4073 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00004074 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00004075 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00004076 case ISD::SMAX:
4077 case ISD::SMIN:
4078 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00004079 case ISD::UMIN:
4080 case AMDGPUISD::FMIN_LEGACY:
4081 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00004082 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00004083 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00004084 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00004085 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00004086 break;
4087 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00004088 case ISD::LOAD:
4089 case ISD::STORE:
4090 case ISD::ATOMIC_LOAD:
4091 case ISD::ATOMIC_STORE:
4092 case ISD::ATOMIC_CMP_SWAP:
4093 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
4094 case ISD::ATOMIC_SWAP:
4095 case ISD::ATOMIC_LOAD_ADD:
4096 case ISD::ATOMIC_LOAD_SUB:
4097 case ISD::ATOMIC_LOAD_AND:
4098 case ISD::ATOMIC_LOAD_OR:
4099 case ISD::ATOMIC_LOAD_XOR:
4100 case ISD::ATOMIC_LOAD_NAND:
4101 case ISD::ATOMIC_LOAD_MIN:
4102 case ISD::ATOMIC_LOAD_MAX:
4103 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00004104 case ISD::ATOMIC_LOAD_UMAX:
4105 case AMDGPUISD::ATOMIC_INC:
4106 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00004107 if (DCI.isBeforeLegalize())
4108 break;
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00004109 return performMemSDNodeCombine(cast<MemSDNode>(N), DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00004110 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00004111 case ISD::AND:
4112 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00004113 case ISD::OR:
4114 return performOrCombine(N, DCI);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00004115 case ISD::XOR:
4116 return performXorCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00004117 case AMDGPUISD::FP_CLASS:
4118 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00004119 case ISD::FCANONICALIZE:
4120 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00004121 case AMDGPUISD::FRACT:
4122 case AMDGPUISD::RCP:
4123 case AMDGPUISD::RSQ:
Matt Arsenault32fc5272016-07-26 16:45:45 +00004124 case AMDGPUISD::RCP_LEGACY:
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00004125 case AMDGPUISD::RSQ_LEGACY:
4126 case AMDGPUISD::RSQ_CLAMP:
4127 case AMDGPUISD::LDEXP: {
4128 SDValue Src = N->getOperand(0);
4129 if (Src.isUndef())
4130 return Src;
4131 break;
4132 }
Matt Arsenaultd8b73d52016-12-22 03:44:42 +00004133 case ISD::SINT_TO_FP:
4134 case ISD::UINT_TO_FP:
4135 return performUCharToFloatCombine(N, DCI);
4136 case AMDGPUISD::CVT_F32_UBYTE0:
4137 case AMDGPUISD::CVT_F32_UBYTE1:
4138 case AMDGPUISD::CVT_F32_UBYTE2:
4139 case AMDGPUISD::CVT_F32_UBYTE3:
4140 return performCvtF32UByteNCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00004141 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00004142 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00004143}
Christian Konigd910b7d2013-02-26 17:52:16 +00004144
Christian Konig8e06e2a2013-04-10 08:39:08 +00004145/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00004146static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00004147 switch (Idx) {
4148 default: return 0;
4149 case AMDGPU::sub0: return 0;
4150 case AMDGPU::sub1: return 1;
4151 case AMDGPU::sub2: return 2;
4152 case AMDGPU::sub3: return 3;
4153 }
4154}
4155
4156/// \brief Adjust the writemask of MIMG instructions
4157void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
4158 SelectionDAG &DAG) const {
4159 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00004160 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00004161 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
4162 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00004163 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00004164
4165 // Try to figure out the used register components
4166 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
4167 I != E; ++I) {
4168
4169 // Abort if we can't understand the usage
4170 if (!I->isMachineOpcode() ||
4171 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
4172 return;
4173
Tom Stellard54774e52013-10-23 02:53:47 +00004174 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
4175 // Note that subregs are packed, i.e. Lane==0 is the first bit set
4176 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
4177 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00004178 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00004179
Tom Stellard54774e52013-10-23 02:53:47 +00004180 // Set which texture component corresponds to the lane.
4181 unsigned Comp;
4182 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
4183 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00004184 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00004185 Dmask &= ~(1 << Comp);
4186 }
4187
Christian Konig8e06e2a2013-04-10 08:39:08 +00004188 // Abort if we have more than one user per component
4189 if (Users[Lane])
4190 return;
4191
4192 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00004193 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00004194 }
4195
Tom Stellard54774e52013-10-23 02:53:47 +00004196 // Abort if there's no change
4197 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00004198 return;
4199
4200 // Adjust the writemask in the node
4201 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00004202 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004203 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00004204 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00004205 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00004206
Christian Konig8b1ed282013-04-10 08:39:16 +00004207 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00004208 // (if NewDmask has only one bit set...)
4209 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004210 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
4211 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00004212 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00004213 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00004214 SDValue(Node, 0), RC);
4215 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
4216 return;
4217 }
4218
Christian Konig8e06e2a2013-04-10 08:39:08 +00004219 // Update the users of the node with the new indices
4220 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
4221
4222 SDNode *User = Users[i];
4223 if (!User)
4224 continue;
4225
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004226 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00004227 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
4228
4229 switch (Idx) {
4230 default: break;
4231 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
4232 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
4233 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
4234 }
4235 }
4236}
4237
Tom Stellardc98ee202015-07-16 19:40:07 +00004238static bool isFrameIndexOp(SDValue Op) {
4239 if (Op.getOpcode() == ISD::AssertZext)
4240 Op = Op.getOperand(0);
4241
4242 return isa<FrameIndexSDNode>(Op);
4243}
4244
Tom Stellard3457a842014-10-09 19:06:00 +00004245/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
4246/// with frame index operands.
4247/// LLVM assumes that inputs are to these instructions are registers.
4248void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
4249 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00004250
4251 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00004252 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00004253 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00004254 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00004255 continue;
4256 }
4257
Tom Stellard3457a842014-10-09 19:06:00 +00004258 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00004259 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00004260 Node->getOperand(i).getValueType(),
4261 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00004262 }
4263
Tom Stellard3457a842014-10-09 19:06:00 +00004264 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00004265}
4266
Matt Arsenault08d84942014-06-03 23:06:13 +00004267/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00004268SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
4269 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00004270 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00004271 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00004272
Nicolai Haehnlec06bfa12016-07-11 21:59:43 +00004273 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() &&
4274 !TII->isGather4(Opcode))
Christian Konig8e06e2a2013-04-10 08:39:08 +00004275 adjustWritemask(Node, DAG);
4276
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00004277 if (Opcode == AMDGPU::INSERT_SUBREG ||
4278 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00004279 legalizeTargetIndependentNode(Node, DAG);
4280 return Node;
4281 }
Tom Stellard654d6692015-01-08 15:08:17 +00004282 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00004283}
Christian Konig8b1ed282013-04-10 08:39:16 +00004284
4285/// \brief Assign the register class depending on the number of
4286/// bits set in the writemask
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004287void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Christian Konig8b1ed282013-04-10 08:39:16 +00004288 SDNode *Node) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00004289 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004290
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004291 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00004292
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004293 if (TII->isVOP3(MI.getOpcode())) {
Matt Arsenault6005fcb2015-10-21 21:51:02 +00004294 // Make sure constant bus requirements are respected.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004295 TII->legalizeOperandsVOP3(MRI, MI);
Matt Arsenault6005fcb2015-10-21 21:51:02 +00004296 return;
4297 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00004298
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004299 if (TII->isMIMG(MI)) {
4300 unsigned VReg = MI.getOperand(0).getReg();
Changpeng Fang8236fe12016-11-14 18:33:18 +00004301 const TargetRegisterClass *RC = MRI.getRegClass(VReg);
4302 // TODO: Need mapping tables to handle other cases (register classes).
4303 if (RC != &AMDGPU::VReg_128RegClass)
4304 return;
4305
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004306 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4;
4307 unsigned Writemask = MI.getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004308 unsigned BitsSet = 0;
4309 for (unsigned i = 0; i < 4; ++i)
4310 BitsSet += Writemask & (1 << i) ? 1 : 0;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004311 switch (BitsSet) {
4312 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00004313 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004314 case 2: RC = &AMDGPU::VReg_64RegClass; break;
4315 case 3: RC = &AMDGPU::VReg_96RegClass; break;
4316 }
4317
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004318 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet);
4319 MI.setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004320 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00004321 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00004322 }
4323
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004324 // Replace unused atomics with the no return version.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004325 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004326 if (NoRetAtomicOp != -1) {
4327 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004328 MI.setDesc(TII->get(NoRetAtomicOp));
4329 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00004330 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004331 }
4332
Tom Stellard354a43c2016-04-01 18:27:37 +00004333 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
4334 // instruction, because the return type of these instructions is a vec2 of
4335 // the memory type, so it can be tied to the input operand.
4336 // This means these instructions always have a use, so we need to add a
4337 // special case to check if the atomic has only one extract_subreg use,
4338 // which itself has no uses.
4339 if ((Node->hasNUsesOfValue(1, 0) &&
Nicolai Haehnle750082d2016-04-15 14:42:36 +00004340 Node->use_begin()->isMachineOpcode() &&
Tom Stellard354a43c2016-04-01 18:27:37 +00004341 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
4342 !Node->use_begin()->hasAnyUseOfValue(0))) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004343 unsigned Def = MI.getOperand(0).getReg();
Tom Stellard354a43c2016-04-01 18:27:37 +00004344
4345 // Change this into a noret atomic.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004346 MI.setDesc(TII->get(NoRetAtomicOp));
4347 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00004348
4349 // If we only remove the def operand from the atomic instruction, the
4350 // extract_subreg will be left with a use of a vreg without a def.
4351 // So we need to insert an implicit_def to avoid machine verifier
4352 // errors.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004353 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
Tom Stellard354a43c2016-04-01 18:27:37 +00004354 TII->get(AMDGPU::IMPLICIT_DEF), Def);
4355 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004356 return;
4357 }
Christian Konig8b1ed282013-04-10 08:39:16 +00004358}
Tom Stellard0518ff82013-06-03 17:39:58 +00004359
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004360static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL,
4361 uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004362 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00004363 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
4364}
4365
4366MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004367 const SDLoc &DL,
Matt Arsenault485defe2014-11-05 19:01:17 +00004368 SDValue Ptr) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00004369 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault485defe2014-11-05 19:01:17 +00004370
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004371 // Build the half of the subregister with the constants before building the
4372 // full 128-bit register. If we are building multiple resource descriptors,
4373 // this will allow CSEing of the 2-component register.
4374 const SDValue Ops0[] = {
4375 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
4376 buildSMovImm32(DAG, DL, 0),
4377 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
4378 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
4379 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
4380 };
Matt Arsenault485defe2014-11-05 19:01:17 +00004381
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004382 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
4383 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00004384
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004385 // Combine the constants and the pointer.
4386 const SDValue Ops1[] = {
4387 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
4388 Ptr,
4389 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
4390 SubRegHi,
4391 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
4392 };
Matt Arsenault485defe2014-11-05 19:01:17 +00004393
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004394 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00004395}
4396
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004397/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00004398/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
4399/// of the resource descriptor) to create an offset, which is added to
4400/// the resource pointer.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004401MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL,
4402 SDValue Ptr, uint32_t RsrcDword1,
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004403 uint64_t RsrcDword2And3) const {
4404 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
4405 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
4406 if (RsrcDword1) {
4407 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004408 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
4409 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004410 }
4411
4412 SDValue DataLo = buildSMovImm32(DAG, DL,
4413 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
4414 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
4415
4416 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004417 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004418 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004419 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004420 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004421 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004422 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004423 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004424 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004425 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004426 };
4427
4428 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
4429}
4430
Tom Stellard94593ee2013-06-03 17:40:18 +00004431SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
4432 const TargetRegisterClass *RC,
4433 unsigned Reg, EVT VT) const {
4434 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
4435
4436 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
4437 cast<RegisterSDNode>(VReg)->getReg(), VT);
4438}
Tom Stellardd7e6f132015-04-08 01:09:26 +00004439
4440//===----------------------------------------------------------------------===//
4441// SI Inline Assembly Support
4442//===----------------------------------------------------------------------===//
4443
4444std::pair<unsigned, const TargetRegisterClass *>
4445SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00004446 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00004447 MVT VT) const {
Matt Arsenault742deb22016-11-18 04:42:57 +00004448 if (!isTypeLegal(VT))
4449 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004450
4451 if (Constraint.size() == 1) {
4452 switch (Constraint[0]) {
4453 case 's':
4454 case 'r':
4455 switch (VT.getSizeInBits()) {
4456 default:
4457 return std::make_pair(0U, nullptr);
4458 case 32:
Matt Arsenault9e910142016-12-20 19:06:12 +00004459 case 16:
Marek Olsak79c05872016-11-25 17:37:09 +00004460 return std::make_pair(0U, &AMDGPU::SReg_32_XM0RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004461 case 64:
4462 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
4463 case 128:
4464 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
4465 case 256:
4466 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
4467 }
4468
4469 case 'v':
4470 switch (VT.getSizeInBits()) {
4471 default:
4472 return std::make_pair(0U, nullptr);
4473 case 32:
Matt Arsenault9e910142016-12-20 19:06:12 +00004474 case 16:
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004475 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
4476 case 64:
4477 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
4478 case 96:
4479 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
4480 case 128:
4481 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
4482 case 256:
4483 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
4484 case 512:
4485 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
4486 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00004487 }
4488 }
4489
4490 if (Constraint.size() > 1) {
4491 const TargetRegisterClass *RC = nullptr;
4492 if (Constraint[1] == 'v') {
4493 RC = &AMDGPU::VGPR_32RegClass;
4494 } else if (Constraint[1] == 's') {
4495 RC = &AMDGPU::SGPR_32RegClass;
4496 }
4497
4498 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00004499 uint32_t Idx;
4500 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
4501 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00004502 return std::make_pair(RC->getRegister(Idx), RC);
4503 }
4504 }
4505 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
4506}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004507
4508SITargetLowering::ConstraintType
4509SITargetLowering::getConstraintType(StringRef Constraint) const {
4510 if (Constraint.size() == 1) {
4511 switch (Constraint[0]) {
4512 default: break;
4513 case 's':
4514 case 'v':
4515 return C_RegisterClass;
4516 }
4517 }
4518 return TargetLowering::getConstraintType(Constraint);
4519}