blob: ac5fddfd42d5826cdfac22df6543c6fed1948c9d [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief Custom DAG lowering for SI
12//
13//===----------------------------------------------------------------------===//
14
NAKAMURA Takumi45e0a832014-07-20 11:15:07 +000015#ifdef _MSC_VER
16// Provide M_PI.
17#define _USE_MATH_DEFINES
18#include <cmath>
19#endif
20
Christian Konig99ee0f42013-03-07 09:04:14 +000021#include "AMDGPU.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000022#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000023#include "AMDGPUSubtarget.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000024#include "SIISelLowering.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025#include "SIInstrInfo.h"
26#include "SIMachineFunctionInfo.h"
27#include "SIRegisterInfo.h"
Alexey Samsonova253bf92014-08-27 19:36:53 +000028#include "llvm/ADT/BitVector.h"
Matt Arsenault9a10cea2016-01-26 04:29:24 +000029#include "llvm/ADT/StringSwitch.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000030#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
32#include "llvm/CodeGen/MachineRegisterInfo.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Wei Ding07e03712016-07-28 16:42:13 +000034#include "llvm/CodeGen/Analysis.h"
Oliver Stannard7e7d9832016-02-02 13:52:43 +000035#include "llvm/IR/DiagnosticInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000036#include "llvm/IR/Function.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000037
38using namespace llvm;
39
Matt Arsenaultd486d3f2016-10-12 18:49:05 +000040static cl::opt<bool> EnableVGPRIndexMode(
41 "amdgpu-vgpr-index-mode",
42 cl::desc("Use GPR indexing mode instead of movrel for vector indexing"),
43 cl::init(false));
44
45
Tom Stellardf110f8f2016-04-14 16:27:03 +000046static unsigned findFirstFreeSGPR(CCState &CCInfo) {
47 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
48 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) {
49 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) {
50 return AMDGPU::SGPR0 + Reg;
51 }
52 }
53 llvm_unreachable("Cannot allocate sgpr");
54}
55
Matt Arsenault43e92fe2016-06-24 06:30:11 +000056SITargetLowering::SITargetLowering(const TargetMachine &TM,
57 const SISubtarget &STI)
Eric Christopher7792e322015-01-30 23:24:40 +000058 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000059 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000060 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000061
Marek Olsak79c05872016-11-25 17:37:09 +000062 addRegisterClass(MVT::i32, &AMDGPU::SReg_32_XM0RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000063 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000064
Tom Stellard436780b2014-05-15 14:41:57 +000065 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
66 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
67 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000068
Matt Arsenault61001bb2015-11-25 19:58:34 +000069 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
70 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
71
Tom Stellard436780b2014-05-15 14:41:57 +000072 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
73 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000074
Tom Stellardf0a21072014-11-18 20:39:39 +000075 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000076 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
77
Tom Stellardf0a21072014-11-18 20:39:39 +000078 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000079 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000080
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +000081 if (Subtarget->has16BitInsts()) {
Marek Olsak79c05872016-11-25 17:37:09 +000082 addRegisterClass(MVT::i16, &AMDGPU::SReg_32_XM0RegClass);
83 addRegisterClass(MVT::f16, &AMDGPU::SReg_32_XM0RegClass);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +000084 }
Tom Stellard115a6152016-11-10 16:02:37 +000085
Eric Christopher23a3a7c2015-02-26 00:00:24 +000086 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000087
Tom Stellard35bb18c2013-08-26 15:06:04 +000088 // We need to custom lower vector stores from local memory
Matt Arsenault71e66762016-05-21 02:27:49 +000089 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000090 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000091 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
92 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000093 setOperationAction(ISD::LOAD, MVT::i1, Custom);
Matt Arsenault2b957b52016-05-02 20:07:26 +000094
Matt Arsenaultbcdfee72016-05-02 20:13:51 +000095 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +000096 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
97 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
98 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
99 setOperationAction(ISD::STORE, MVT::i1, Custom);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +0000100
Matt Arsenault71e66762016-05-21 02:27:49 +0000101 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
102 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000103 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
104
105 setOperationAction(ISD::SELECT, MVT::i1, Promote);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000106 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +0000107 setOperationAction(ISD::SELECT, MVT::f64, Promote);
108 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000109
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000110 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
111 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
112 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
113 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Matt Arsenault71e66762016-05-21 02:27:49 +0000114 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000115
Tom Stellardd1efda82016-01-20 21:48:24 +0000116 setOperationAction(ISD::SETCC, MVT::i1, Promote);
Tom Stellard83747202013-07-18 21:43:53 +0000117 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
118 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
119
Matt Arsenault71e66762016-05-21 02:27:49 +0000120 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
121 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000122
Matt Arsenault4e466652014-04-16 01:41:30 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000125 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000127 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
128 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
Matt Arsenault4e466652014-04-16 01:41:30 +0000129 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
130
Tom Stellard9fa17912013-08-14 23:24:45 +0000131 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000132 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000133 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
134
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000135 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000136 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000137 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
138 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
139 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
140 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000141
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000142 // We only support LOAD/STORE and vector manipulation ops for vectors
143 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000144 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000145 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000146 switch (Op) {
Tom Stellard967bf582014-02-13 23:34:15 +0000147 case ISD::LOAD:
148 case ISD::STORE:
149 case ISD::BUILD_VECTOR:
150 case ISD::BITCAST:
151 case ISD::EXTRACT_VECTOR_ELT:
152 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000153 case ISD::INSERT_SUBVECTOR:
154 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000155 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000156 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000157 case ISD::CONCAT_VECTORS:
158 setOperationAction(Op, VT, Custom);
159 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000160 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000161 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000162 break;
163 }
164 }
165 }
166
Matt Arsenaultcb540bc2016-07-19 00:35:03 +0000167 // TODO: For dynamic 64-bit vector inserts/extracts, should emit a pseudo that
168 // is expanded to avoid having two separate loops in case the index is a VGPR.
169
Matt Arsenault61001bb2015-11-25 19:58:34 +0000170 // Most operations are naturally 32-bit vector operations. We only support
171 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
172 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
173 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
174 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
175
176 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
177 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
178
179 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
180 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
181
182 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
183 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
184 }
185
Matt Arsenault71e66762016-05-21 02:27:49 +0000186 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
187 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
188 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
189 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000190
Tom Stellard354a43c2016-04-01 18:27:37 +0000191 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
192 // and output demarshalling
193 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
194 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
195
196 // We can't return success/failure, only the old value,
197 // let LLVM add the comparison
198 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand);
199 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand);
200
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000201 if (getSubtarget()->hasFlatAddressSpace()) {
Matt Arsenault99c14522016-04-25 19:27:24 +0000202 setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom);
203 setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom);
204 }
205
Matt Arsenault71e66762016-05-21 02:27:49 +0000206 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
207 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
208
209 // On SI this is s_memtime and s_memrealtime on VI.
210 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
Matt Arsenault0bb294b2016-06-17 22:27:03 +0000211 setOperationAction(ISD::TRAP, MVT::Other, Custom);
Matt Arsenault71e66762016-05-21 02:27:49 +0000212
213 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
214 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
215
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000216 if (Subtarget->getGeneration() >= SISubtarget::SEA_ISLANDS) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000217 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
218 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
219 setOperationAction(ISD::FRINT, MVT::f64, Legal);
220 }
221
222 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
223
224 setOperationAction(ISD::FSIN, MVT::f32, Custom);
225 setOperationAction(ISD::FCOS, MVT::f32, Custom);
226 setOperationAction(ISD::FDIV, MVT::f32, Custom);
227 setOperationAction(ISD::FDIV, MVT::f64, Custom);
228
Tom Stellard115a6152016-11-10 16:02:37 +0000229 if (Subtarget->has16BitInsts()) {
230 setOperationAction(ISD::Constant, MVT::i16, Legal);
231
232 setOperationAction(ISD::SMIN, MVT::i16, Legal);
233 setOperationAction(ISD::SMAX, MVT::i16, Legal);
234
235 setOperationAction(ISD::UMIN, MVT::i16, Legal);
236 setOperationAction(ISD::UMAX, MVT::i16, Legal);
237
238 setOperationAction(ISD::SETCC, MVT::i16, Promote);
239 AddPromotedToType(ISD::SETCC, MVT::i16, MVT::i32);
240
241 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Promote);
242 AddPromotedToType(ISD::SIGN_EXTEND, MVT::i16, MVT::i32);
243
244 setOperationAction(ISD::ROTR, MVT::i16, Promote);
245 setOperationAction(ISD::ROTL, MVT::i16, Promote);
246
247 setOperationAction(ISD::SDIV, MVT::i16, Promote);
248 setOperationAction(ISD::UDIV, MVT::i16, Promote);
249 setOperationAction(ISD::SREM, MVT::i16, Promote);
250 setOperationAction(ISD::UREM, MVT::i16, Promote);
251
252 setOperationAction(ISD::BSWAP, MVT::i16, Promote);
253 setOperationAction(ISD::BITREVERSE, MVT::i16, Promote);
254
255 setOperationAction(ISD::CTTZ, MVT::i16, Promote);
256 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16, Promote);
257 setOperationAction(ISD::CTLZ, MVT::i16, Promote);
258 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16, Promote);
259
260 setOperationAction(ISD::SELECT_CC, MVT::i16, Expand);
261
262 setOperationAction(ISD::BR_CC, MVT::i16, Expand);
263
264 setOperationAction(ISD::LOAD, MVT::i16, Custom);
265
266 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
267
Tom Stellard115a6152016-11-10 16:02:37 +0000268 setOperationAction(ISD::FP16_TO_FP, MVT::i16, Promote);
269 AddPromotedToType(ISD::FP16_TO_FP, MVT::i16, MVT::i32);
270 setOperationAction(ISD::FP_TO_FP16, MVT::i16, Promote);
271 AddPromotedToType(ISD::FP_TO_FP16, MVT::i16, MVT::i32);
Tom Stellardb4c8e8e2016-11-12 00:19:11 +0000272
Konstantin Zhuravlyov3f0cdc72016-11-17 04:00:46 +0000273 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
274 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote);
275 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
276 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote);
Tom Stellardb4c8e8e2016-11-12 00:19:11 +0000277
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000278 // F16 - Constant Actions.
279 setOperationAction(ISD::ConstantFP, MVT::f16, Custom);
280
281 // F16 - Load/Store Actions.
282 setOperationAction(ISD::LOAD, MVT::f16, Promote);
283 AddPromotedToType(ISD::LOAD, MVT::f16, MVT::i16);
284 setOperationAction(ISD::STORE, MVT::f16, Promote);
285 AddPromotedToType(ISD::STORE, MVT::f16, MVT::i16);
286
287 // F16 - VOP1 Actions.
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +0000288 setOperationAction(ISD::FP_ROUND, MVT::f16, Custom);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000289 setOperationAction(ISD::FCOS, MVT::f16, Promote);
290 setOperationAction(ISD::FSIN, MVT::f16, Promote);
Konstantin Zhuravlyov3f0cdc72016-11-17 04:00:46 +0000291 setOperationAction(ISD::FP_TO_SINT, MVT::f16, Promote);
292 setOperationAction(ISD::FP_TO_UINT, MVT::f16, Promote);
293 setOperationAction(ISD::SINT_TO_FP, MVT::f16, Promote);
294 setOperationAction(ISD::UINT_TO_FP, MVT::f16, Promote);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000295
296 // F16 - VOP2 Actions.
Konstantin Zhuravlyov662e01d2016-11-17 03:49:01 +0000297 setOperationAction(ISD::BR_CC, MVT::f16, Expand);
Konstantin Zhuravlyov2a87a422016-11-16 03:16:26 +0000298 setOperationAction(ISD::SELECT_CC, MVT::f16, Expand);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000299 setOperationAction(ISD::FMAXNUM, MVT::f16, Legal);
300 setOperationAction(ISD::FMINNUM, MVT::f16, Legal);
301 setOperationAction(ISD::FDIV, MVT::f16, Promote);
302
303 // F16 - VOP3 Actions.
304 setOperationAction(ISD::FMA, MVT::f16, Legal);
305 if (!Subtarget->hasFP16Denormals())
306 setOperationAction(ISD::FMAD, MVT::f16, Legal);
Tom Stellard115a6152016-11-10 16:02:37 +0000307 }
308
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000309 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000310 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000311 setTargetDAGCombine(ISD::FMINNUM);
312 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000313 setTargetDAGCombine(ISD::SMIN);
314 setTargetDAGCombine(ISD::SMAX);
315 setTargetDAGCombine(ISD::UMIN);
316 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000317 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000318 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000319 setTargetDAGCombine(ISD::OR);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000320 setTargetDAGCombine(ISD::XOR);
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +0000321 setTargetDAGCombine(ISD::SINT_TO_FP);
Matt Arsenault364a6742014-06-11 17:50:44 +0000322 setTargetDAGCombine(ISD::UINT_TO_FP);
Matt Arsenault9cd90712016-04-14 01:42:16 +0000323 setTargetDAGCombine(ISD::FCANONICALIZE);
Matt Arsenault364a6742014-06-11 17:50:44 +0000324
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000325 // All memory operations. Some folding on the pointer operand is done to help
326 // matching the constant offsets in the addressing modes.
327 setTargetDAGCombine(ISD::LOAD);
328 setTargetDAGCombine(ISD::STORE);
329 setTargetDAGCombine(ISD::ATOMIC_LOAD);
330 setTargetDAGCombine(ISD::ATOMIC_STORE);
331 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
332 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
333 setTargetDAGCombine(ISD::ATOMIC_SWAP);
334 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
335 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
336 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
337 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
338 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
339 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
340 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
341 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
342 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
343 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
344
Christian Konigeecebd02013-03-26 14:04:02 +0000345 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000346}
347
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000348const SISubtarget *SITargetLowering::getSubtarget() const {
349 return static_cast<const SISubtarget *>(Subtarget);
350}
351
Tom Stellard0125f2a2013-06-25 02:39:35 +0000352//===----------------------------------------------------------------------===//
353// TargetLowering queries
354//===----------------------------------------------------------------------===//
355
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000356bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
357 const CallInst &CI,
358 unsigned IntrID) const {
359 switch (IntrID) {
360 case Intrinsic::amdgcn_atomic_inc:
361 case Intrinsic::amdgcn_atomic_dec:
362 Info.opc = ISD::INTRINSIC_W_CHAIN;
363 Info.memVT = MVT::getVT(CI.getType());
364 Info.ptrVal = CI.getOperand(0);
365 Info.align = 0;
366 Info.vol = false;
367 Info.readMem = true;
368 Info.writeMem = true;
369 return true;
370 default:
371 return false;
372 }
373}
374
Matt Arsenaulte306a322014-10-21 16:25:08 +0000375bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
376 EVT) const {
377 // SI has some legal vector types, but no legal vector operations. Say no
378 // shuffles are legal in order to prefer scalarizing some vector operations.
379 return false;
380}
381
Tom Stellard70580f82015-07-20 14:28:41 +0000382bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
383 // Flat instructions do not have offsets, and only have the register
384 // address.
385 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
386}
387
Matt Arsenault711b3902015-08-07 20:18:34 +0000388bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
389 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
390 // additionally can do r + r + i with addr64. 32-bit has more addressing
391 // mode options. Depending on the resource constant, it can also do
392 // (i64 r0) + (i32 r1) * (i14 i).
393 //
394 // Private arrays end up using a scratch buffer most of the time, so also
395 // assume those use MUBUF instructions. Scratch loads / stores are currently
396 // implemented as mubuf instructions with offen bit set, so slightly
397 // different than the normal addr64.
398 if (!isUInt<12>(AM.BaseOffs))
399 return false;
400
401 // FIXME: Since we can split immediate into soffset and immediate offset,
402 // would it make sense to allow any immediate?
403
404 switch (AM.Scale) {
405 case 0: // r + i or just i, depending on HasBaseReg.
406 return true;
407 case 1:
408 return true; // We have r + r or r + i.
409 case 2:
410 if (AM.HasBaseReg) {
411 // Reject 2 * r + r.
412 return false;
413 }
414
415 // Allow 2 * r as r + r
416 // Or 2 * r + i is allowed as r + r + i.
417 return true;
418 default: // Don't allow n * r
419 return false;
420 }
421}
422
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000423bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
424 const AddrMode &AM, Type *Ty,
425 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000426 // No global is ever allowed as a base.
427 if (AM.BaseGV)
428 return false;
429
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000430 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000431 case AMDGPUAS::GLOBAL_ADDRESS: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000432 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) {
Tom Stellard70580f82015-07-20 14:28:41 +0000433 // Assume the we will use FLAT for all global memory accesses
434 // on VI.
435 // FIXME: This assumption is currently wrong. On VI we still use
436 // MUBUF instructions for the r + i addressing mode. As currently
437 // implemented, the MUBUF instructions only work on buffer < 4GB.
438 // It may be possible to support > 4GB buffers with MUBUF instructions,
439 // by setting the stride value in the resource descriptor which would
440 // increase the size limit to (stride * 4GB). However, this is risky,
441 // because it has never been validated.
442 return isLegalFlatAddressingMode(AM);
443 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000444
Matt Arsenault711b3902015-08-07 20:18:34 +0000445 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000446 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000447 case AMDGPUAS::CONSTANT_ADDRESS: {
448 // If the offset isn't a multiple of 4, it probably isn't going to be
449 // correctly aligned.
Matt Arsenault3cc1e002016-08-13 01:43:51 +0000450 // FIXME: Can we get the real alignment here?
Matt Arsenault711b3902015-08-07 20:18:34 +0000451 if (AM.BaseOffs % 4 != 0)
452 return isLegalMUBUFAddressingMode(AM);
453
454 // There are no SMRD extloads, so if we have to do a small type access we
455 // will use a MUBUF load.
456 // FIXME?: We also need to do this if unaligned, but we don't know the
457 // alignment here.
458 if (DL.getTypeStoreSize(Ty) < 4)
459 return isLegalMUBUFAddressingMode(AM);
460
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000461 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000462 // SMRD instructions have an 8-bit, dword offset on SI.
463 if (!isUInt<8>(AM.BaseOffs / 4))
464 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000465 } else if (Subtarget->getGeneration() == SISubtarget::SEA_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000466 // On CI+, this can also be a 32-bit literal constant offset. If it fits
467 // in 8-bits, it can use a smaller encoding.
468 if (!isUInt<32>(AM.BaseOffs / 4))
469 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000470 } else if (Subtarget->getGeneration() == SISubtarget::VOLCANIC_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000471 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
472 if (!isUInt<20>(AM.BaseOffs))
473 return false;
474 } else
475 llvm_unreachable("unhandled generation");
476
477 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
478 return true;
479
480 if (AM.Scale == 1 && AM.HasBaseReg)
481 return true;
482
483 return false;
484 }
485
486 case AMDGPUAS::PRIVATE_ADDRESS:
Matt Arsenault711b3902015-08-07 20:18:34 +0000487 return isLegalMUBUFAddressingMode(AM);
488
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000489 case AMDGPUAS::LOCAL_ADDRESS:
490 case AMDGPUAS::REGION_ADDRESS: {
491 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
492 // field.
493 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
494 // an 8-bit dword offset but we don't know the alignment here.
495 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000496 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000497
498 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
499 return true;
500
501 if (AM.Scale == 1 && AM.HasBaseReg)
502 return true;
503
Matt Arsenault5015a892014-08-15 17:17:07 +0000504 return false;
505 }
Tom Stellard70580f82015-07-20 14:28:41 +0000506 case AMDGPUAS::FLAT_ADDRESS:
Matt Arsenault7d1b6c82016-04-29 06:25:10 +0000507 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
508 // For an unknown address space, this usually means that this is for some
509 // reason being used for pure arithmetic, and not based on some addressing
510 // computation. We don't have instructions that compute pointers with any
511 // addressing modes, so treat them as having no offset like flat
512 // instructions.
Tom Stellard70580f82015-07-20 14:28:41 +0000513 return isLegalFlatAddressingMode(AM);
514
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000515 default:
516 llvm_unreachable("unhandled address space");
517 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000518}
519
Matt Arsenaulte6986632015-01-14 01:35:22 +0000520bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000521 unsigned AddrSpace,
522 unsigned Align,
523 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000524 if (IsFast)
525 *IsFast = false;
526
Matt Arsenault1018c892014-04-24 17:08:26 +0000527 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
528 // which isn't a simple VT.
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000529 // Until MVT is extended to handle this, simply check for the size and
530 // rely on the condition below: allow accesses if the size is a multiple of 4.
531 if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 &&
532 VT.getStoreSize() > 16)) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000533 return false;
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000534 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000535
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000536 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
537 AddrSpace == AMDGPUAS::REGION_ADDRESS) {
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000538 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
539 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
540 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000541 bool AlignedBy4 = (Align % 4 == 0);
542 if (IsFast)
543 *IsFast = AlignedBy4;
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000544
Sanjay Patelce74db92015-09-03 15:03:19 +0000545 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000546 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000547
Tom Stellard64a9d082016-10-14 18:10:39 +0000548 // FIXME: We have to be conservative here and assume that flat operations
549 // will access scratch. If we had access to the IR function, then we
550 // could determine if any private memory was used in the function.
551 if (!Subtarget->hasUnalignedScratchAccess() &&
552 (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS ||
553 AddrSpace == AMDGPUAS::FLAT_ADDRESS)) {
554 return false;
555 }
556
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000557 if (Subtarget->hasUnalignedBufferAccess()) {
558 // If we have an uniform constant load, it still requires using a slow
559 // buffer instruction if unaligned.
560 if (IsFast) {
561 *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) ?
562 (Align % 4 == 0) : true;
563 }
564
565 return true;
566 }
567
Tom Stellard33e64c62015-02-04 20:49:52 +0000568 // Smaller than dword value must be aligned.
Tom Stellard33e64c62015-02-04 20:49:52 +0000569 if (VT.bitsLT(MVT::i32))
570 return false;
571
Matt Arsenault1018c892014-04-24 17:08:26 +0000572 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
573 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000574 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000575 if (IsFast)
576 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000577
578 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000579}
580
Matt Arsenault46645fa2014-07-28 17:49:26 +0000581EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
582 unsigned SrcAlign, bool IsMemset,
583 bool ZeroMemset,
584 bool MemcpyStrSrc,
585 MachineFunction &MF) const {
586 // FIXME: Should account for address space here.
587
588 // The default fallback uses the private pointer size as a guess for a type to
589 // use. Make sure we switch these to 64-bit accesses.
590
591 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
592 return MVT::v4i32;
593
594 if (Size >= 8 && DstAlign >= 4)
595 return MVT::v2i32;
596
597 // Use the default.
598 return MVT::Other;
599}
600
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000601static bool isFlatGlobalAddrSpace(unsigned AS) {
602 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
603 AS == AMDGPUAS::FLAT_ADDRESS ||
604 AS == AMDGPUAS::CONSTANT_ADDRESS;
605}
606
607bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
608 unsigned DestAS) const {
Matt Arsenault37fefd62016-06-10 02:18:02 +0000609 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000610}
611
Tom Stellarda6f24c62015-12-15 20:55:55 +0000612bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
613 const MemSDNode *MemNode = cast<MemSDNode>(N);
614 const Value *Ptr = MemNode->getMemOperand()->getValue();
615
616 // UndefValue means this is a load of a kernel input. These are uniform.
Tom Stellard418beb72016-07-13 14:23:33 +0000617 // Sometimes LDS instructions have constant pointers.
618 // If Ptr is null, then that means this mem operand contains a
619 // PseudoSourceValue like GOT.
620 if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) ||
621 isa<Constant>(Ptr) || isa<GlobalValue>(Ptr))
Tom Stellarda6f24c62015-12-15 20:55:55 +0000622 return true;
623
Tom Stellard418beb72016-07-13 14:23:33 +0000624 const Instruction *I = dyn_cast<Instruction>(Ptr);
Tom Stellarda6f24c62015-12-15 20:55:55 +0000625 return I && I->getMetadata("amdgpu.uniform");
626}
627
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000628TargetLoweringBase::LegalizeTypeAction
629SITargetLowering::getPreferredVectorAction(EVT VT) const {
630 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
631 return TypeSplitVector;
632
633 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000634}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000635
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000636bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
637 Type *Ty) const {
Matt Arsenault749035b2016-07-30 01:40:36 +0000638 // FIXME: Could be smarter if called for vector constants.
639 return true;
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000640}
641
Tom Stellard2e045bb2016-01-20 00:13:22 +0000642bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
643
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000644 // i16 is not desirable unless it is a load or a store.
645 if (VT == MVT::i16 && Op != ISD::LOAD && Op != ISD::STORE)
646 return false;
647
Tom Stellard2e045bb2016-01-20 00:13:22 +0000648 // SimplifySetCC uses this function to determine whether or not it should
649 // create setcc with i1 operands. We don't have instructions for i1 setcc.
650 if (VT == MVT::i1 && Op == ISD::SETCC)
651 return false;
652
653 return TargetLowering::isTypeDesirableForOp(Op, VT);
654}
655
Jan Veselyfea814d2016-06-21 20:46:20 +0000656SDValue SITargetLowering::LowerParameterPtr(SelectionDAG &DAG,
657 const SDLoc &SL, SDValue Chain,
658 unsigned Offset) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000659 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000660 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000661 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaultac234b62015-11-30 21:15:57 +0000662 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000663
Matt Arsenault86033ca2014-07-28 17:31:39 +0000664 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000665 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000666 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
667 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
Jan Veselyfea814d2016-06-21 20:46:20 +0000668 return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
669 DAG.getConstant(Offset, SL, PtrVT));
670}
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000671
Jan Veselyfea814d2016-06-21 20:46:20 +0000672SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
673 const SDLoc &SL, SDValue Chain,
674 unsigned Offset, bool Signed) const {
675 const DataLayout &DL = DAG.getDataLayout();
Tom Stellard083f1622016-10-17 16:56:19 +0000676 Type *Ty = MemVT.getTypeForEVT(*DAG.getContext());
Jan Veselyfea814d2016-06-21 20:46:20 +0000677 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000678 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
679
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000680 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000681
Jan Veselyfea814d2016-06-21 20:46:20 +0000682 SDValue Ptr = LowerParameterPtr(DAG, SL, Chain, Offset);
Tom Stellardbc6c5232016-10-17 16:21:45 +0000683 SDValue Load = DAG.getLoad(MemVT, SL, Chain, Ptr, PtrInfo, Align,
684 MachineMemOperand::MONonTemporal |
685 MachineMemOperand::MODereferenceable |
686 MachineMemOperand::MOInvariant);
687
688 SDValue Val;
689 if (MemVT.isFloatingPoint())
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000690 Val = getFPExtOrFPTrunc(DAG, Load, SL, VT);
Tom Stellardbc6c5232016-10-17 16:21:45 +0000691 else if (Signed)
692 Val = DAG.getSExtOrTrunc(Load, SL, VT);
693 else
694 Val = DAG.getZExtOrTrunc(Load, SL, VT);
695
696 SDValue Ops[] = {
697 Val,
698 Load.getValue(1)
699 };
700
701 return DAG.getMergeValues(Ops, SL);
Tom Stellard94593ee2013-06-03 17:40:18 +0000702}
703
Christian Konig2c8f6d52013-03-07 09:03:52 +0000704SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000705 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000706 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
707 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000708 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000709
710 MachineFunction &MF = DAG.getMachineFunction();
711 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000712 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000713 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000714
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000715 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) {
Matt Arsenaultd48da142015-11-02 23:23:02 +0000716 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000717 DiagnosticInfoUnsupported NoGraphicsHSA(
718 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000719 DAG.getContext()->diagnose(NoGraphicsHSA);
Diana Picus81bc3172016-05-26 15:24:55 +0000720 return DAG.getEntryNode();
Matt Arsenaultd48da142015-11-02 23:23:02 +0000721 }
722
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000723 // Create stack objects that are used for emitting debugger prologue if
724 // "amdgpu-debugger-emit-prologue" attribute was specified.
725 if (ST.debuggerEmitPrologue())
726 createDebuggerPrologueStackObjects(MF);
727
Christian Konig2c8f6d52013-03-07 09:03:52 +0000728 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000729 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000730
731 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000732 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000733
734 // First check if it's a PS input addr
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000735 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000736 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000737
Marek Olsakfccabaf2016-01-13 11:45:36 +0000738 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000739 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000740 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000741 ++PSInputNum;
742 continue;
743 }
744
Marek Olsakfccabaf2016-01-13 11:45:36 +0000745 Info->markPSInputAllocated(PSInputNum);
746 if (Arg.Used)
747 Info->PSInputEna |= 1 << PSInputNum;
748
749 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000750 }
751
Matt Arsenault539ca882016-05-05 20:27:02 +0000752 if (AMDGPU::isShader(CallConv)) {
753 // Second split vertices into their elements
754 if (Arg.VT.isVector()) {
755 ISD::InputArg NewArg = Arg;
756 NewArg.Flags.setSplit();
757 NewArg.VT = Arg.VT.getVectorElementType();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000758
Matt Arsenault539ca882016-05-05 20:27:02 +0000759 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
760 // three or five element vertex only needs three or five registers,
761 // NOT four or eight.
762 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
763 unsigned NumElements = ParamType->getVectorNumElements();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000764
Matt Arsenault539ca882016-05-05 20:27:02 +0000765 for (unsigned j = 0; j != NumElements; ++j) {
766 Splits.push_back(NewArg);
767 NewArg.PartOffset += NewArg.VT.getStoreSize();
768 }
769 } else {
770 Splits.push_back(Arg);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000771 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000772 }
773 }
774
775 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000776 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
777 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000778
Christian Konig99ee0f42013-03-07 09:04:14 +0000779 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000780 //
781 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
782 // PSInputAddr, the user wants to enable some bits after the compilation
783 // based on run-time states. Since we can't know what the final PSInputEna
784 // will look like, so we shouldn't do anything here and the user should take
785 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000786 //
787 // Otherwise, the following restrictions apply:
788 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
789 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
790 // enabled too.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000791 if (CallConv == CallingConv::AMDGPU_PS &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000792 ((Info->getPSInputAddr() & 0x7F) == 0 ||
NAKAMURA Takumife1202c2016-06-20 00:37:41 +0000793 ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000794 CCInfo.AllocateReg(AMDGPU::VGPR0);
795 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000796 Info->markPSInputAllocated(0);
797 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000798 }
799
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000800 if (!AMDGPU::isShader(CallConv)) {
Tom Stellardf110f8f2016-04-14 16:27:03 +0000801 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
802 } else {
803 assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() &&
804 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() &&
805 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
806 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
807 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
808 !Info->hasWorkItemIDZ());
Tom Stellardaf775432013-10-23 00:44:32 +0000809 }
810
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000811 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
812 if (Info->hasPrivateSegmentBuffer()) {
813 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
814 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
815 CCInfo.AllocateReg(PrivateSegmentBufferReg);
816 }
817
818 if (Info->hasDispatchPtr()) {
819 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000820 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000821 CCInfo.AllocateReg(DispatchPtrReg);
822 }
823
Matt Arsenault48ab5262016-04-25 19:27:18 +0000824 if (Info->hasQueuePtr()) {
825 unsigned QueuePtrReg = Info->addQueuePtr(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000826 MF.addLiveIn(QueuePtrReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault48ab5262016-04-25 19:27:18 +0000827 CCInfo.AllocateReg(QueuePtrReg);
828 }
829
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000830 if (Info->hasKernargSegmentPtr()) {
831 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000832 MF.addLiveIn(InputPtrReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000833 CCInfo.AllocateReg(InputPtrReg);
834 }
835
Matt Arsenault8d718dc2016-07-22 17:01:30 +0000836 if (Info->hasDispatchID()) {
837 unsigned DispatchIDReg = Info->addDispatchID(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000838 MF.addLiveIn(DispatchIDReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault8d718dc2016-07-22 17:01:30 +0000839 CCInfo.AllocateReg(DispatchIDReg);
840 }
841
Matt Arsenault296b8492016-02-12 06:31:30 +0000842 if (Info->hasFlatScratchInit()) {
843 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000844 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SGPR_64RegClass);
Matt Arsenault296b8492016-02-12 06:31:30 +0000845 CCInfo.AllocateReg(FlatScratchInitReg);
846 }
847
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000848 if (!AMDGPU::isShader(CallConv))
849 analyzeFormalArgumentsCompute(CCInfo, Ins);
850 else
851 AnalyzeFormalArguments(CCInfo, Splits);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000852
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000853 SmallVector<SDValue, 16> Chains;
854
Christian Konig2c8f6d52013-03-07 09:03:52 +0000855 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
856
Christian Konigb7be72d2013-05-17 09:46:48 +0000857 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000858 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000859 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000860 continue;
861 }
862
Christian Konig2c8f6d52013-03-07 09:03:52 +0000863 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000864 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000865
866 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000867 VT = Ins[i].VT;
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000868 EVT MemVT = VA.getLocVT();
Tom Stellardb5798b02015-06-26 21:15:03 +0000869 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
870 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000871 // The first 36 bytes of the input buffer contains information about
872 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000873 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000874 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000875 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000876
Craig Toppere3dcce92015-08-01 22:20:21 +0000877 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000878 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000879 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Tom Stellardca7ecf32014-08-22 18:49:31 +0000880 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
881 // On SI local pointers are just offsets into LDS, so they are always
882 // less than 16-bits. On CI and newer they could potentially be
883 // real pointers, so we can't guarantee their size.
884 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
885 DAG.getValueType(MVT::i16));
886 }
887
Tom Stellarded882c22013-06-03 17:40:11 +0000888 InVals.push_back(Arg);
Matt Arsenault52ef4012016-07-26 16:45:58 +0000889 Info->setABIArgOffset(Offset + MemVT.getStoreSize());
Tom Stellarded882c22013-06-03 17:40:11 +0000890 continue;
891 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000892 assert(VA.isRegLoc() && "Parameter must be in a register!");
893
894 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000895
896 if (VT == MVT::i64) {
897 // For now assume it is a pointer
898 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
Matt Arsenaultcdad3162016-11-29 19:39:48 +0000899 &AMDGPU::SGPR_64RegClass);
900 Reg = MF.addLiveIn(Reg, &AMDGPU::SGPR_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000901 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
902 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000903 continue;
904 }
905
906 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
907
908 Reg = MF.addLiveIn(Reg, RC);
909 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
910
Christian Konig2c8f6d52013-03-07 09:03:52 +0000911 if (Arg.VT.isVector()) {
912
913 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000914 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000915 unsigned NumElements = ParamType->getVectorNumElements();
916
917 SmallVector<SDValue, 4> Regs;
918 Regs.push_back(Val);
919 for (unsigned j = 1; j != NumElements; ++j) {
920 Reg = ArgLocs[ArgIdx++].getLocReg();
921 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000922
923 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
924 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000925 }
926
927 // Fill up the missing vector elements
928 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000929 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000930
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000931 InVals.push_back(DAG.getBuildVector(Arg.VT, DL, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000932 continue;
933 }
934
935 InVals.push_back(Val);
936 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000937
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000938 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
939 // these from the dispatch pointer.
940
941 // Start adding system SGPRs.
942 if (Info->hasWorkGroupIDX()) {
943 unsigned Reg = Info->addWorkGroupIDX();
Marek Olsak79c05872016-11-25 17:37:09 +0000944 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000945 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000946 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000947
948 if (Info->hasWorkGroupIDY()) {
949 unsigned Reg = Info->addWorkGroupIDY();
Marek Olsak79c05872016-11-25 17:37:09 +0000950 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000951 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000952 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000953
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000954 if (Info->hasWorkGroupIDZ()) {
955 unsigned Reg = Info->addWorkGroupIDZ();
Marek Olsak79c05872016-11-25 17:37:09 +0000956 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000957 CCInfo.AllocateReg(Reg);
958 }
959
960 if (Info->hasWorkGroupInfo()) {
961 unsigned Reg = Info->addWorkGroupInfo();
Marek Olsak79c05872016-11-25 17:37:09 +0000962 MF.addLiveIn(Reg, &AMDGPU::SReg_32_XM0RegClass);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000963 CCInfo.AllocateReg(Reg);
964 }
965
966 if (Info->hasPrivateSegmentWaveByteOffset()) {
967 // Scratch wave offset passed in system SGPR.
Tom Stellardf110f8f2016-04-14 16:27:03 +0000968 unsigned PrivateSegmentWaveByteOffsetReg;
969
970 if (AMDGPU::isShader(CallConv)) {
971 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
972 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
973 } else
974 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset();
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000975
976 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
977 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
978 }
979
980 // Now that we've figured out where the scratch register inputs are, see if
981 // should reserve the arguments and use them directly.
Matthias Braun941a7052016-07-28 18:40:00 +0000982 bool HasStackObjects = MF.getFrameInfo().hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +0000983 // Record that we know we have non-spill stack objects so we don't need to
984 // check all stack objects later.
985 if (HasStackObjects)
986 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000987
Matt Arsenault253640e2016-10-13 13:10:00 +0000988 // Everything live out of a block is spilled with fast regalloc, so it's
989 // almost certain that spilling will be required.
990 if (getTargetMachine().getOptLevel() == CodeGenOpt::None)
991 HasStackObjects = true;
992
Tom Stellard0b76fc4c2016-09-16 21:34:26 +0000993 if (ST.isAmdCodeObjectV2()) {
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000994 if (HasStackObjects) {
995 // If we have stack objects, we unquestionably need the private buffer
Tom Stellard0b76fc4c2016-09-16 21:34:26 +0000996 // resource. For the Code Object V2 ABI, this will be the first 4 user
997 // SGPR inputs. We can reserve those and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000998
999 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
1000 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
1001 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
1002
1003 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
1004 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
1005 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
1006 } else {
1007 unsigned ReservedBufferReg
1008 = TRI->reservedPrivateSegmentBufferReg(MF);
1009 unsigned ReservedOffsetReg
1010 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
1011
1012 // We tentatively reserve the last registers (skipping the last two
1013 // which may contain VCC). After register allocation, we'll replace
1014 // these with the ones immediately after those which were really
1015 // allocated. In the prologue copies will be inserted from the argument
1016 // to these reserved registers.
1017 Info->setScratchRSrcReg(ReservedBufferReg);
1018 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
1019 }
1020 } else {
1021 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
1022
1023 // Without HSA, relocations are used for the scratch pointer and the
1024 // buffer resource setup is always inserted in the prologue. Scratch wave
1025 // offset is still in an input SGPR.
1026 Info->setScratchRSrcReg(ReservedBufferReg);
1027
1028 if (HasStackObjects) {
1029 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
1030 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
1031 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
1032 } else {
1033 unsigned ReservedOffsetReg
1034 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
1035 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
1036 }
1037 }
1038
1039 if (Info->hasWorkItemIDX()) {
1040 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
1041 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1042 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +00001043 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001044
1045 if (Info->hasWorkItemIDY()) {
1046 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
1047 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1048 CCInfo.AllocateReg(Reg);
1049 }
1050
1051 if (Info->hasWorkItemIDZ()) {
1052 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
1053 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1054 CCInfo.AllocateReg(Reg);
1055 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +00001056
Matt Arsenaultcf13d182015-07-10 22:51:36 +00001057 if (Chains.empty())
1058 return Chain;
1059
1060 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +00001061}
1062
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001063SDValue
1064SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1065 bool isVarArg,
1066 const SmallVectorImpl<ISD::OutputArg> &Outs,
1067 const SmallVectorImpl<SDValue> &OutVals,
1068 const SDLoc &DL, SelectionDAG &DAG) const {
Marek Olsak8a0f3352016-01-13 17:23:04 +00001069 MachineFunction &MF = DAG.getMachineFunction();
1070 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1071
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001072 if (!AMDGPU::isShader(CallConv))
Marek Olsak8a0f3352016-01-13 17:23:04 +00001073 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
1074 OutVals, DL, DAG);
1075
Marek Olsak8e9cc632016-01-13 17:23:09 +00001076 Info->setIfReturnsVoid(Outs.size() == 0);
1077
Marek Olsak8a0f3352016-01-13 17:23:04 +00001078 SmallVector<ISD::OutputArg, 48> Splits;
1079 SmallVector<SDValue, 48> SplitVals;
1080
1081 // Split vectors into their elements.
1082 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1083 const ISD::OutputArg &Out = Outs[i];
1084
1085 if (Out.VT.isVector()) {
1086 MVT VT = Out.VT.getVectorElementType();
1087 ISD::OutputArg NewOut = Out;
1088 NewOut.Flags.setSplit();
1089 NewOut.VT = VT;
1090
1091 // We want the original number of vector elements here, e.g.
1092 // three or five, not four or eight.
1093 unsigned NumElements = Out.ArgVT.getVectorNumElements();
1094
1095 for (unsigned j = 0; j != NumElements; ++j) {
1096 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
1097 DAG.getConstant(j, DL, MVT::i32));
1098 SplitVals.push_back(Elem);
1099 Splits.push_back(NewOut);
1100 NewOut.PartOffset += NewOut.VT.getStoreSize();
1101 }
1102 } else {
1103 SplitVals.push_back(OutVals[i]);
1104 Splits.push_back(Out);
1105 }
1106 }
1107
1108 // CCValAssign - represent the assignment of the return value to a location.
1109 SmallVector<CCValAssign, 48> RVLocs;
1110
1111 // CCState - Info about the registers and stack slots.
1112 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1113 *DAG.getContext());
1114
1115 // Analyze outgoing return values.
1116 AnalyzeReturn(CCInfo, Splits);
1117
1118 SDValue Flag;
1119 SmallVector<SDValue, 48> RetOps;
1120 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1121
1122 // Copy the result values into the output registers.
1123 for (unsigned i = 0, realRVLocIdx = 0;
1124 i != RVLocs.size();
1125 ++i, ++realRVLocIdx) {
1126 CCValAssign &VA = RVLocs[i];
1127 assert(VA.isRegLoc() && "Can only return in registers!");
1128
1129 SDValue Arg = SplitVals[realRVLocIdx];
1130
1131 // Copied from other backends.
1132 switch (VA.getLocInfo()) {
1133 default: llvm_unreachable("Unknown loc info!");
1134 case CCValAssign::Full:
1135 break;
1136 case CCValAssign::BCvt:
1137 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1138 break;
1139 }
1140
1141 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
1142 Flag = Chain.getValue(1);
1143 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1144 }
1145
1146 // Update chain and glue.
1147 RetOps[0] = Chain;
1148 if (Flag.getNode())
1149 RetOps.push_back(Flag);
1150
Matt Arsenault9babdf42016-06-22 20:15:28 +00001151 unsigned Opc = Info->returnsVoid() ? AMDGPUISD::ENDPGM : AMDGPUISD::RETURN;
1152 return DAG.getNode(Opc, DL, MVT::Other, RetOps);
Marek Olsak8a0f3352016-01-13 17:23:04 +00001153}
1154
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001155unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1156 SelectionDAG &DAG) const {
1157 unsigned Reg = StringSwitch<unsigned>(RegName)
1158 .Case("m0", AMDGPU::M0)
1159 .Case("exec", AMDGPU::EXEC)
1160 .Case("exec_lo", AMDGPU::EXEC_LO)
1161 .Case("exec_hi", AMDGPU::EXEC_HI)
1162 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1163 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1164 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1165 .Default(AMDGPU::NoRegister);
1166
1167 if (Reg == AMDGPU::NoRegister) {
1168 report_fatal_error(Twine("invalid register name \""
1169 + StringRef(RegName) + "\"."));
1170
1171 }
1172
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001173 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001174 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1175 report_fatal_error(Twine("invalid register \""
1176 + StringRef(RegName) + "\" for subtarget."));
1177 }
1178
1179 switch (Reg) {
1180 case AMDGPU::M0:
1181 case AMDGPU::EXEC_LO:
1182 case AMDGPU::EXEC_HI:
1183 case AMDGPU::FLAT_SCR_LO:
1184 case AMDGPU::FLAT_SCR_HI:
1185 if (VT.getSizeInBits() == 32)
1186 return Reg;
1187 break;
1188 case AMDGPU::EXEC:
1189 case AMDGPU::FLAT_SCR:
1190 if (VT.getSizeInBits() == 64)
1191 return Reg;
1192 break;
1193 default:
1194 llvm_unreachable("missing register type checking");
1195 }
1196
1197 report_fatal_error(Twine("invalid type for register \""
1198 + StringRef(RegName) + "\"."));
1199}
1200
Matt Arsenault786724a2016-07-12 21:41:32 +00001201// If kill is not the last instruction, split the block so kill is always a
1202// proper terminator.
1203MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI,
1204 MachineBasicBlock *BB) const {
1205 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1206
1207 MachineBasicBlock::iterator SplitPoint(&MI);
1208 ++SplitPoint;
1209
1210 if (SplitPoint == BB->end()) {
1211 // Don't bother with a new block.
1212 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1213 return BB;
1214 }
1215
1216 MachineFunction *MF = BB->getParent();
1217 MachineBasicBlock *SplitBB
1218 = MF->CreateMachineBasicBlock(BB->getBasicBlock());
1219
Matt Arsenault786724a2016-07-12 21:41:32 +00001220 MF->insert(++MachineFunction::iterator(BB), SplitBB);
1221 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end());
1222
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001223 SplitBB->transferSuccessorsAndUpdatePHIs(BB);
Matt Arsenault786724a2016-07-12 21:41:32 +00001224 BB->addSuccessor(SplitBB);
1225
1226 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1227 return SplitBB;
1228}
1229
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001230// Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the
1231// wavefront. If the value is uniform and just happens to be in a VGPR, this
1232// will only do one iteration. In the worst case, this will loop 64 times.
1233//
1234// TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001235static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop(
1236 const SIInstrInfo *TII,
1237 MachineRegisterInfo &MRI,
1238 MachineBasicBlock &OrigBB,
1239 MachineBasicBlock &LoopBB,
1240 const DebugLoc &DL,
1241 const MachineOperand &IdxReg,
1242 unsigned InitReg,
1243 unsigned ResultReg,
1244 unsigned PhiReg,
1245 unsigned InitSaveExecReg,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001246 int Offset,
1247 bool UseGPRIdxMode) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001248 MachineBasicBlock::iterator I = LoopBB.begin();
1249
1250 unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1251 unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1252 unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1253 unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1254
1255 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg)
1256 .addReg(InitReg)
1257 .addMBB(&OrigBB)
1258 .addReg(ResultReg)
1259 .addMBB(&LoopBB);
1260
1261 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec)
1262 .addReg(InitSaveExecReg)
1263 .addMBB(&OrigBB)
1264 .addReg(NewExec)
1265 .addMBB(&LoopBB);
1266
1267 // Read the next variant <- also loop target.
1268 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg)
1269 .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef()));
1270
1271 // Compare the just read M0 value to all possible Idx values.
1272 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg)
1273 .addReg(CurrentIdxReg)
Matt Arsenaultf0ba86a2016-07-21 09:40:57 +00001274 .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg());
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001275
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001276 if (UseGPRIdxMode) {
1277 unsigned IdxReg;
1278 if (Offset == 0) {
1279 IdxReg = CurrentIdxReg;
1280 } else {
1281 IdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1282 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), IdxReg)
1283 .addReg(CurrentIdxReg, RegState::Kill)
1284 .addImm(Offset);
1285 }
1286
1287 MachineInstr *SetIdx =
1288 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_IDX))
1289 .addReg(IdxReg, RegState::Kill);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001290 SetIdx->getOperand(2).setIsUndef();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001291 } else {
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001292 // Move index from VCC into M0
1293 if (Offset == 0) {
1294 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1295 .addReg(CurrentIdxReg, RegState::Kill);
1296 } else {
1297 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1298 .addReg(CurrentIdxReg, RegState::Kill)
1299 .addImm(Offset);
1300 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001301 }
1302
1303 // Update EXEC, save the original EXEC value to VCC.
1304 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec)
1305 .addReg(CondReg, RegState::Kill);
1306
1307 MRI.setSimpleHint(NewExec, CondReg);
1308
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001309 // Update EXEC, switch all done bits to 0 and all todo bits to 1.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001310 MachineInstr *InsertPt =
1311 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001312 .addReg(AMDGPU::EXEC)
1313 .addReg(NewExec);
1314
1315 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use
1316 // s_cbranch_scc0?
1317
1318 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover.
1319 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
1320 .addMBB(&LoopBB);
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001321
1322 return InsertPt->getIterator();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001323}
1324
1325// This has slightly sub-optimal regalloc when the source vector is killed by
1326// the read. The register allocator does not understand that the kill is
1327// per-workitem, so is kept alive for the whole loop so we end up not re-using a
1328// subregister from it, using 1 more VGPR than necessary. This was saved when
1329// this was expanded after register allocation.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001330static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII,
1331 MachineBasicBlock &MBB,
1332 MachineInstr &MI,
1333 unsigned InitResultReg,
1334 unsigned PhiReg,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001335 int Offset,
1336 bool UseGPRIdxMode) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001337 MachineFunction *MF = MBB.getParent();
1338 MachineRegisterInfo &MRI = MF->getRegInfo();
1339 const DebugLoc &DL = MI.getDebugLoc();
1340 MachineBasicBlock::iterator I(&MI);
1341
1342 unsigned DstReg = MI.getOperand(0).getReg();
1343 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1344 unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1345
1346 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec);
1347
1348 // Save the EXEC mask
1349 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec)
1350 .addReg(AMDGPU::EXEC);
1351
1352 // To insert the loop we need to split the block. Move everything after this
1353 // point to a new block, and insert a new empty block between the two.
1354 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock();
1355 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
1356 MachineFunction::iterator MBBI(MBB);
1357 ++MBBI;
1358
1359 MF->insert(MBBI, LoopBB);
1360 MF->insert(MBBI, RemainderBB);
1361
1362 LoopBB->addSuccessor(LoopBB);
1363 LoopBB->addSuccessor(RemainderBB);
1364
1365 // Move the rest of the block into a new block.
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001366 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001367 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
1368
1369 MBB.addSuccessor(LoopBB);
1370
1371 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1372
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001373 auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx,
1374 InitResultReg, DstReg, PhiReg, TmpExec,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001375 Offset, UseGPRIdxMode);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001376
1377 MachineBasicBlock::iterator First = RemainderBB->begin();
1378 BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
1379 .addReg(SaveExec);
1380
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001381 return InsPt;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001382}
1383
1384// Returns subreg index, offset
1385static std::pair<unsigned, int>
1386computeIndirectRegAndOffset(const SIRegisterInfo &TRI,
1387 const TargetRegisterClass *SuperRC,
1388 unsigned VecReg,
1389 int Offset) {
1390 int NumElts = SuperRC->getSize() / 4;
1391
1392 // Skip out of bounds offsets, or else we would end up using an undefined
1393 // register.
1394 if (Offset >= NumElts || Offset < 0)
1395 return std::make_pair(AMDGPU::sub0, Offset);
1396
1397 return std::make_pair(AMDGPU::sub0 + Offset, 0);
1398}
1399
1400// Return true if the index is an SGPR and was set.
1401static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII,
1402 MachineRegisterInfo &MRI,
1403 MachineInstr &MI,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001404 int Offset,
1405 bool UseGPRIdxMode,
1406 bool IsIndirectSrc) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001407 MachineBasicBlock *MBB = MI.getParent();
1408 const DebugLoc &DL = MI.getDebugLoc();
1409 MachineBasicBlock::iterator I(&MI);
1410
1411 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1412 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
1413
1414 assert(Idx->getReg() != AMDGPU::NoRegister);
1415
1416 if (!TII->getRegisterInfo().isSGPRClass(IdxRC))
1417 return false;
1418
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001419 if (UseGPRIdxMode) {
1420 unsigned IdxMode = IsIndirectSrc ?
1421 VGPRIndexMode::SRC0_ENABLE : VGPRIndexMode::DST_ENABLE;
1422 if (Offset == 0) {
1423 MachineInstr *SetOn =
1424 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1425 .addOperand(*Idx)
1426 .addImm(IdxMode);
1427
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001428 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001429 } else {
1430 unsigned Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
1431 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp)
1432 .addOperand(*Idx)
1433 .addImm(Offset);
1434 MachineInstr *SetOn =
1435 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1436 .addReg(Tmp, RegState::Kill)
1437 .addImm(IdxMode);
1438
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001439 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001440 }
1441
1442 return true;
1443 }
1444
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001445 if (Offset == 0) {
1446 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1447 .addOperand(*Idx);
1448 } else {
1449 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1450 .addOperand(*Idx)
1451 .addImm(Offset);
1452 }
1453
1454 return true;
1455}
1456
1457// Control flow needs to be inserted if indexing with a VGPR.
1458static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI,
1459 MachineBasicBlock &MBB,
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001460 const SISubtarget &ST) {
1461 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001462 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1463 MachineFunction *MF = MBB.getParent();
1464 MachineRegisterInfo &MRI = MF->getRegInfo();
1465
1466 unsigned Dst = MI.getOperand(0).getReg();
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001467 unsigned SrcReg = TII->getNamedOperand(MI, AMDGPU::OpName::src)->getReg();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001468 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1469
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001470 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcReg);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001471
1472 unsigned SubReg;
1473 std::tie(SubReg, Offset)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001474 = computeIndirectRegAndOffset(TRI, VecRC, SrcReg, Offset);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001475
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001476 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode;
1477
1478 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, true)) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001479 MachineBasicBlock::iterator I(&MI);
1480 const DebugLoc &DL = MI.getDebugLoc();
1481
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001482 if (UseGPRIdxMode) {
1483 // TODO: Look at the uses to avoid the copy. This may require rescheduling
1484 // to avoid interfering with other uses, so probably requires a new
1485 // optimization pass.
1486 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001487 .addReg(SrcReg, RegState::Undef, SubReg)
1488 .addReg(SrcReg, RegState::Implicit)
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001489 .addReg(AMDGPU::M0, RegState::Implicit);
1490 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1491 } else {
1492 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001493 .addReg(SrcReg, RegState::Undef, SubReg)
1494 .addReg(SrcReg, RegState::Implicit);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001495 }
1496
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001497 MI.eraseFromParent();
1498
1499 return &MBB;
1500 }
1501
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001502
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001503 const DebugLoc &DL = MI.getDebugLoc();
1504 MachineBasicBlock::iterator I(&MI);
1505
1506 unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1507 unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1508
1509 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg);
1510
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001511 if (UseGPRIdxMode) {
1512 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1513 .addImm(0) // Reset inside loop.
1514 .addImm(VGPRIndexMode::SRC0_ENABLE);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001515 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001516
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001517 // Disable again after the loop.
1518 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1519 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001520
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001521 auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset, UseGPRIdxMode);
1522 MachineBasicBlock *LoopBB = InsPt->getParent();
1523
1524 if (UseGPRIdxMode) {
1525 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001526 .addReg(SrcReg, RegState::Undef, SubReg)
1527 .addReg(SrcReg, RegState::Implicit)
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001528 .addReg(AMDGPU::M0, RegState::Implicit);
1529 } else {
1530 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001531 .addReg(SrcReg, RegState::Undef, SubReg)
1532 .addReg(SrcReg, RegState::Implicit);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001533 }
1534
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001535 MI.eraseFromParent();
1536
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001537 return LoopBB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001538}
1539
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001540static unsigned getMOVRELDPseudo(const TargetRegisterClass *VecRC) {
1541 switch (VecRC->getSize()) {
1542 case 4:
1543 return AMDGPU::V_MOVRELD_B32_V1;
1544 case 8:
1545 return AMDGPU::V_MOVRELD_B32_V2;
1546 case 16:
1547 return AMDGPU::V_MOVRELD_B32_V4;
1548 case 32:
1549 return AMDGPU::V_MOVRELD_B32_V8;
1550 case 64:
1551 return AMDGPU::V_MOVRELD_B32_V16;
1552 default:
1553 llvm_unreachable("unsupported size for MOVRELD pseudos");
1554 }
1555}
1556
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001557static MachineBasicBlock *emitIndirectDst(MachineInstr &MI,
1558 MachineBasicBlock &MBB,
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001559 const SISubtarget &ST) {
1560 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001561 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1562 MachineFunction *MF = MBB.getParent();
1563 MachineRegisterInfo &MRI = MF->getRegInfo();
1564
1565 unsigned Dst = MI.getOperand(0).getReg();
1566 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
1567 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1568 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val);
1569 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1570 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
1571
1572 // This can be an immediate, but will be folded later.
1573 assert(Val->getReg());
1574
1575 unsigned SubReg;
1576 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC,
1577 SrcVec->getReg(),
1578 Offset);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001579 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode;
1580
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001581 if (Idx->getReg() == AMDGPU::NoRegister) {
1582 MachineBasicBlock::iterator I(&MI);
1583 const DebugLoc &DL = MI.getDebugLoc();
1584
1585 assert(Offset == 0);
1586
1587 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst)
1588 .addOperand(*SrcVec)
1589 .addOperand(*Val)
1590 .addImm(SubReg);
1591
1592 MI.eraseFromParent();
1593 return &MBB;
1594 }
1595
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001596 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, false)) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001597 MachineBasicBlock::iterator I(&MI);
1598 const DebugLoc &DL = MI.getDebugLoc();
1599
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001600 if (UseGPRIdxMode) {
1601 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_indirect))
1602 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst
1603 .addOperand(*Val)
1604 .addReg(Dst, RegState::ImplicitDefine)
1605 .addReg(SrcVec->getReg(), RegState::Implicit)
1606 .addReg(AMDGPU::M0, RegState::Implicit);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001607
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001608 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1609 } else {
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001610 const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(VecRC));
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001611
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001612 BuildMI(MBB, I, DL, MovRelDesc)
1613 .addReg(Dst, RegState::Define)
1614 .addReg(SrcVec->getReg())
1615 .addOperand(*Val)
1616 .addImm(SubReg - AMDGPU::sub0);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001617 }
1618
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001619 MI.eraseFromParent();
1620 return &MBB;
1621 }
1622
1623 if (Val->isReg())
1624 MRI.clearKillFlags(Val->getReg());
1625
1626 const DebugLoc &DL = MI.getDebugLoc();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001627
1628 if (UseGPRIdxMode) {
1629 MachineBasicBlock::iterator I(&MI);
1630
1631 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1632 .addImm(0) // Reset inside loop.
1633 .addImm(VGPRIndexMode::DST_ENABLE);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001634 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001635
1636 // Disable again after the loop.
1637 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1638 }
1639
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001640 unsigned PhiReg = MRI.createVirtualRegister(VecRC);
1641
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001642 auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg,
1643 Offset, UseGPRIdxMode);
1644 MachineBasicBlock *LoopBB = InsPt->getParent();
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001645
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001646 if (UseGPRIdxMode) {
1647 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_indirect))
1648 .addReg(PhiReg, RegState::Undef, SubReg) // vdst
1649 .addOperand(*Val) // src0
1650 .addReg(Dst, RegState::ImplicitDefine)
1651 .addReg(PhiReg, RegState::Implicit)
1652 .addReg(AMDGPU::M0, RegState::Implicit);
1653 } else {
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001654 const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(VecRC));
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001655
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001656 BuildMI(*LoopBB, InsPt, DL, MovRelDesc)
1657 .addReg(Dst, RegState::Define)
1658 .addReg(PhiReg)
1659 .addOperand(*Val)
1660 .addImm(SubReg - AMDGPU::sub0);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001661 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001662
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001663 MI.eraseFromParent();
1664
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001665 return LoopBB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001666}
1667
Matt Arsenault786724a2016-07-12 21:41:32 +00001668MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
1669 MachineInstr &MI, MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001670 switch (MI.getOpcode()) {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001671 case AMDGPU::SI_INIT_M0: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001672 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001673 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001674 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001675 .addOperand(MI.getOperand(0));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001676 MI.eraseFromParent();
Matt Arsenault20711b72015-02-20 22:10:45 +00001677 return BB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001678 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001679 case AMDGPU::GET_GROUPSTATICSIZE: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001680 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1681
Changpeng Fang01f60622016-03-15 17:28:44 +00001682 MachineFunction *MF = BB->getParent();
1683 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001684 DebugLoc DL = MI.getDebugLoc();
Matt Arsenault3c07c812016-07-22 17:01:33 +00001685 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32))
1686 .addOperand(MI.getOperand(0))
Matt Arsenault52ef4012016-07-26 16:45:58 +00001687 .addImm(MFI->getLDSSize());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001688 MI.eraseFromParent();
Changpeng Fang01f60622016-03-15 17:28:44 +00001689 return BB;
1690 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001691 case AMDGPU::SI_INDIRECT_SRC_V1:
1692 case AMDGPU::SI_INDIRECT_SRC_V2:
1693 case AMDGPU::SI_INDIRECT_SRC_V4:
1694 case AMDGPU::SI_INDIRECT_SRC_V8:
1695 case AMDGPU::SI_INDIRECT_SRC_V16:
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001696 return emitIndirectSrc(MI, *BB, *getSubtarget());
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001697 case AMDGPU::SI_INDIRECT_DST_V1:
1698 case AMDGPU::SI_INDIRECT_DST_V2:
1699 case AMDGPU::SI_INDIRECT_DST_V4:
1700 case AMDGPU::SI_INDIRECT_DST_V8:
1701 case AMDGPU::SI_INDIRECT_DST_V16:
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001702 return emitIndirectDst(MI, *BB, *getSubtarget());
Matt Arsenault786724a2016-07-12 21:41:32 +00001703 case AMDGPU::SI_KILL:
1704 return splitKillBlock(MI, BB);
Matt Arsenault22e41792016-08-27 01:00:37 +00001705 case AMDGPU::V_CNDMASK_B64_PSEUDO: {
1706 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
1707 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1708
1709 unsigned Dst = MI.getOperand(0).getReg();
1710 unsigned Src0 = MI.getOperand(1).getReg();
1711 unsigned Src1 = MI.getOperand(2).getReg();
1712 const DebugLoc &DL = MI.getDebugLoc();
1713 unsigned SrcCond = MI.getOperand(3).getReg();
1714
1715 unsigned DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1716 unsigned DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1717
1718 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo)
1719 .addReg(Src0, 0, AMDGPU::sub0)
1720 .addReg(Src1, 0, AMDGPU::sub0)
1721 .addReg(SrcCond);
1722 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi)
1723 .addReg(Src0, 0, AMDGPU::sub1)
1724 .addReg(Src1, 0, AMDGPU::sub1)
1725 .addReg(SrcCond);
1726
1727 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst)
1728 .addReg(DstLo)
1729 .addImm(AMDGPU::sub0)
1730 .addReg(DstHi)
1731 .addImm(AMDGPU::sub1);
1732 MI.eraseFromParent();
1733 return BB;
1734 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001735 default:
1736 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001737 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001738}
1739
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001740bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1741 // This currently forces unfolding various combinations of fsub into fma with
1742 // free fneg'd operands. As long as we have fast FMA (controlled by
1743 // isFMAFasterThanFMulAndFAdd), we should perform these.
1744
1745 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1746 // most of these combines appear to be cycle neutral but save on instruction
1747 // count / code size.
1748 return true;
1749}
1750
Mehdi Amini44ede332015-07-09 02:09:04 +00001751EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1752 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001753 if (!VT.isVector()) {
1754 return MVT::i1;
1755 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001756 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001757}
1758
Mehdi Aminieaabc512015-07-09 15:12:23 +00001759MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001760 return MVT::i32;
1761}
1762
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001763// Answering this is somewhat tricky and depends on the specific device which
1764// have different rates for fma or all f64 operations.
1765//
1766// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1767// regardless of which device (although the number of cycles differs between
1768// devices), so it is always profitable for f64.
1769//
1770// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1771// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1772// which we can always do even without fused FP ops since it returns the same
1773// result as the separate operations and since it is always full
1774// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1775// however does not support denormals, so we do report fma as faster if we have
1776// a fast fma device and require denormals.
1777//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001778bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1779 VT = VT.getScalarType();
1780
1781 if (!VT.isSimple())
1782 return false;
1783
1784 switch (VT.getSimpleVT().SimpleTy) {
1785 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001786 // This is as fast on some subtargets. However, we always have full rate f32
1787 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001788 // which we should prefer over fma. We can't use this if we want to support
1789 // denormals, so only report this in these cases.
1790 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001791 case MVT::f64:
1792 return true;
1793 default:
1794 break;
1795 }
1796
1797 return false;
1798}
1799
Tom Stellard75aadc22012-12-11 21:25:42 +00001800//===----------------------------------------------------------------------===//
1801// Custom DAG Lowering Operations
1802//===----------------------------------------------------------------------===//
1803
1804SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1805 switch (Op.getOpcode()) {
1806 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001807 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001808 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001809 SDValue Result = LowerLOAD(Op, DAG);
1810 assert((!Result.getNode() ||
1811 Result.getNode()->getNumValues() == 2) &&
1812 "Load should return a value and a chain");
1813 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001814 }
Tom Stellardaf775432013-10-23 00:44:32 +00001815
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001816 case ISD::FSIN:
1817 case ISD::FCOS:
1818 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001819 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001820 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001821 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001822 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001823 case ISD::GlobalAddress: {
1824 MachineFunction &MF = DAG.getMachineFunction();
1825 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1826 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001827 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001828 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001829 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001830 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Matt Arsenault99c14522016-04-25 19:27:24 +00001831 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG);
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001832 case ISD::TRAP: return lowerTRAP(Op, DAG);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001833
1834 case ISD::ConstantFP:
1835 return lowerConstantFP(Op, DAG);
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00001836 case ISD::FP_ROUND:
1837 return lowerFP_ROUND(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001838 }
1839 return SDValue();
1840}
1841
Tom Stellardf8794352012-12-19 22:10:31 +00001842/// \brief Helper function for LowerBRCOND
1843static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001844
Tom Stellardf8794352012-12-19 22:10:31 +00001845 SDNode *Parent = Value.getNode();
1846 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1847 I != E; ++I) {
1848
1849 if (I.getUse().get() != Value)
1850 continue;
1851
1852 if (I->getOpcode() == Opcode)
1853 return *I;
1854 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001855 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001856}
1857
Tom Stellardbc4497b2016-02-12 23:45:29 +00001858bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault6408c912016-09-16 22:11:18 +00001859 if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
1860 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1861 case AMDGPUIntrinsic::amdgcn_if:
1862 case AMDGPUIntrinsic::amdgcn_else:
1863 case AMDGPUIntrinsic::amdgcn_end_cf:
1864 case AMDGPUIntrinsic::amdgcn_loop:
1865 return true;
1866 default:
1867 return false;
1868 }
Tom Stellardbc4497b2016-02-12 23:45:29 +00001869 }
Matt Arsenault6408c912016-09-16 22:11:18 +00001870
1871 if (Intr->getOpcode() == ISD::INTRINSIC_WO_CHAIN) {
1872 switch (cast<ConstantSDNode>(Intr->getOperand(0))->getZExtValue()) {
1873 case AMDGPUIntrinsic::amdgcn_break:
1874 case AMDGPUIntrinsic::amdgcn_if_break:
1875 case AMDGPUIntrinsic::amdgcn_else_break:
1876 return true;
1877 default:
1878 return false;
1879 }
1880 }
1881
1882 return false;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001883}
1884
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001885void SITargetLowering::createDebuggerPrologueStackObjects(
1886 MachineFunction &MF) const {
1887 // Create stack objects that are used for emitting debugger prologue.
1888 //
1889 // Debugger prologue writes work group IDs and work item IDs to scratch memory
1890 // at fixed location in the following format:
1891 // offset 0: work group ID x
1892 // offset 4: work group ID y
1893 // offset 8: work group ID z
1894 // offset 16: work item ID x
1895 // offset 20: work item ID y
1896 // offset 24: work item ID z
1897 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1898 int ObjectIdx = 0;
1899
1900 // For each dimension:
1901 for (unsigned i = 0; i < 3; ++i) {
1902 // Create fixed stack object for work group ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001903 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001904 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx);
1905 // Create fixed stack object for work item ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001906 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001907 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx);
1908 }
1909}
1910
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00001911bool SITargetLowering::shouldEmitFixup(const GlobalValue *GV) const {
1912 const Triple &TT = getTargetMachine().getTargetTriple();
1913 return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS &&
1914 AMDGPU::shouldEmitConstantsToTextSection(TT);
1915}
1916
1917bool SITargetLowering::shouldEmitGOTReloc(const GlobalValue *GV) const {
1918 return (GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS ||
1919 GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) &&
1920 !shouldEmitFixup(GV) &&
1921 !getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
1922}
1923
1924bool SITargetLowering::shouldEmitPCReloc(const GlobalValue *GV) const {
1925 return !shouldEmitFixup(GV) && !shouldEmitGOTReloc(GV);
1926}
1927
Tom Stellardf8794352012-12-19 22:10:31 +00001928/// This transforms the control flow intrinsics to get the branch destination as
1929/// last parameter, also switches branch target with BR if the need arise
1930SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1931 SelectionDAG &DAG) const {
1932
Andrew Trickef9de2a2013-05-25 02:42:55 +00001933 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001934
1935 SDNode *Intr = BRCOND.getOperand(1).getNode();
1936 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001937 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001938 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001939
1940 if (Intr->getOpcode() == ISD::SETCC) {
1941 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001942 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001943 Intr = SetCC->getOperand(0).getNode();
1944
1945 } else {
1946 // Get the target from BR if we don't negate the condition
1947 BR = findUser(BRCOND, ISD::BR);
1948 Target = BR->getOperand(1);
1949 }
1950
Matt Arsenault6408c912016-09-16 22:11:18 +00001951 // FIXME: This changes the types of the intrinsics instead of introducing new
1952 // nodes with the correct types.
1953 // e.g. llvm.amdgcn.loop
1954
1955 // eg: i1,ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3
1956 // => t9: ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3, BasicBlock:ch<bb1 0x7fee5286d088>
1957
Nicolai Haehnleffbd56a2016-05-05 17:36:36 +00001958 if (!isCFIntrinsic(Intr)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00001959 // This is a uniform branch so we don't need to legalize.
1960 return BRCOND;
1961 }
1962
Matt Arsenault6408c912016-09-16 22:11:18 +00001963 bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID ||
1964 Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN;
1965
Tom Stellardbc4497b2016-02-12 23:45:29 +00001966 assert(!SetCC ||
1967 (SetCC->getConstantOperandVal(1) == 1 &&
Tom Stellardbc4497b2016-02-12 23:45:29 +00001968 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1969 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001970
Tom Stellardf8794352012-12-19 22:10:31 +00001971 // operands of the new intrinsic call
1972 SmallVector<SDValue, 4> Ops;
Matt Arsenault6408c912016-09-16 22:11:18 +00001973 if (HaveChain)
1974 Ops.push_back(BRCOND.getOperand(0));
1975
1976 Ops.append(Intr->op_begin() + (HaveChain ? 1 : 0), Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001977 Ops.push_back(Target);
1978
Matt Arsenault6408c912016-09-16 22:11:18 +00001979 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
1980
Tom Stellardf8794352012-12-19 22:10:31 +00001981 // build the new intrinsic call
1982 SDNode *Result = DAG.getNode(
1983 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001984 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001985
Matt Arsenault6408c912016-09-16 22:11:18 +00001986 if (!HaveChain) {
1987 SDValue Ops[] = {
1988 SDValue(Result, 0),
1989 BRCOND.getOperand(0)
1990 };
1991
1992 Result = DAG.getMergeValues(Ops, DL).getNode();
1993 }
1994
Tom Stellardf8794352012-12-19 22:10:31 +00001995 if (BR) {
1996 // Give the branch instruction our target
1997 SDValue Ops[] = {
1998 BR->getOperand(0),
1999 BRCOND.getOperand(2)
2000 };
Chandler Carruth356665a2014-08-01 22:09:43 +00002001 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
2002 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
2003 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00002004 }
2005
2006 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
2007
2008 // Copy the intrinsic results to registers
2009 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
2010 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
2011 if (!CopyToReg)
2012 continue;
2013
2014 Chain = DAG.getCopyToReg(
2015 Chain, DL,
2016 CopyToReg->getOperand(1),
2017 SDValue(Result, i - 1),
2018 SDValue());
2019
2020 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
2021 }
2022
2023 // Remove the old intrinsic from the chain
2024 DAG.ReplaceAllUsesOfValueWith(
2025 SDValue(Intr, Intr->getNumValues() - 1),
2026 Intr->getOperand(0));
2027
2028 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00002029}
2030
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002031SDValue SITargetLowering::getFPExtOrFPTrunc(SelectionDAG &DAG,
2032 SDValue Op,
2033 const SDLoc &DL,
2034 EVT VT) const {
2035 return Op.getValueType().bitsLE(VT) ?
2036 DAG.getNode(ISD::FP_EXTEND, DL, VT, Op) :
2037 DAG.getNode(ISD::FTRUNC, DL, VT, Op);
2038}
2039
2040SDValue SITargetLowering::lowerConstantFP(SDValue Op, SelectionDAG &DAG) const {
2041 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(Op)) {
2042 return DAG.getConstant(FP->getValueAPF().bitcastToAPInt().getZExtValue(),
2043 SDLoc(Op), MVT::i32);
2044 }
2045
2046 return SDValue();
2047}
2048
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00002049SDValue SITargetLowering::lowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaultafe614c2016-11-18 18:33:36 +00002050 assert(Op.getValueType() == MVT::f16 &&
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00002051 "Do not know how to custom lower FP_ROUND for non-f16 type");
2052
Matt Arsenaultafe614c2016-11-18 18:33:36 +00002053 SDValue Src = Op.getOperand(0);
2054 EVT SrcVT = Src.getValueType();
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00002055 if (SrcVT != MVT::f64)
2056 return Op;
2057
2058 SDLoc DL(Op);
Matt Arsenaultafe614c2016-11-18 18:33:36 +00002059
Konstantin Zhuravlyovd709efb2016-11-17 04:28:37 +00002060 SDValue FpToFp16 = DAG.getNode(ISD::FP_TO_FP16, DL, MVT::i32, Src);
2061 SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::i16, FpToFp16);
2062 return DAG.getNode(ISD::BITCAST, DL, MVT::f16, Trunc);;
2063}
2064
Matt Arsenault99c14522016-04-25 19:27:24 +00002065SDValue SITargetLowering::getSegmentAperture(unsigned AS,
2066 SelectionDAG &DAG) const {
2067 SDLoc SL;
2068 MachineFunction &MF = DAG.getMachineFunction();
2069 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00002070 unsigned UserSGPR = Info->getQueuePtrUserSGPR();
2071 assert(UserSGPR != AMDGPU::NoRegister);
2072
Matt Arsenault99c14522016-04-25 19:27:24 +00002073 SDValue QueuePtr = CreateLiveInRegister(
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00002074 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
Matt Arsenault99c14522016-04-25 19:27:24 +00002075
2076 // Offset into amd_queue_t for group_segment_aperture_base_hi /
2077 // private_segment_aperture_base_hi.
2078 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
2079
2080 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr,
2081 DAG.getConstant(StructOffset, SL, MVT::i64));
2082
2083 // TODO: Use custom target PseudoSourceValue.
2084 // TODO: We should use the value from the IR intrinsic call, but it might not
2085 // be available and how do we get it?
2086 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()),
2087 AMDGPUAS::CONSTANT_ADDRESS));
2088
2089 MachinePointerInfo PtrInfo(V, StructOffset);
Justin Lebar9c375812016-07-15 18:27:10 +00002090 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo,
2091 MinAlign(64, StructOffset),
Justin Lebaradbf09e2016-09-11 01:38:58 +00002092 MachineMemOperand::MODereferenceable |
2093 MachineMemOperand::MOInvariant);
Matt Arsenault99c14522016-04-25 19:27:24 +00002094}
2095
2096SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
2097 SelectionDAG &DAG) const {
2098 SDLoc SL(Op);
2099 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op);
2100
2101 SDValue Src = ASC->getOperand(0);
2102
2103 // FIXME: Really support non-0 null pointers.
2104 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32);
2105 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
2106
2107 // flat -> local/private
2108 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
2109 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
2110 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
2111 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
2112 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
2113
2114 return DAG.getNode(ISD::SELECT, SL, MVT::i32,
2115 NonNull, Ptr, SegmentNullPtr);
2116 }
2117 }
2118
2119 // local/private -> flat
2120 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
2121 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
2122 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
2123 SDValue NonNull
2124 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
2125
2126 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG);
2127 SDValue CvtPtr
2128 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
2129
2130 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull,
2131 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr),
2132 FlatNullPtr);
2133 }
2134 }
2135
2136 // global <-> flat are no-ops and never emitted.
2137
2138 const MachineFunction &MF = DAG.getMachineFunction();
2139 DiagnosticInfoUnsupported InvalidAddrSpaceCast(
2140 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc());
2141 DAG.getContext()->diagnose(InvalidAddrSpaceCast);
2142
2143 return DAG.getUNDEF(ASC->getValueType(0));
2144}
2145
Tom Stellard418beb72016-07-13 14:23:33 +00002146bool
2147SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2148 // We can fold offsets for anything that doesn't require a GOT relocation.
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00002149 return (GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS ||
2150 GA->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) &&
2151 !shouldEmitGOTReloc(GA->getGlobal());
Tom Stellard418beb72016-07-13 14:23:33 +00002152}
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002153
Tom Stellard418beb72016-07-13 14:23:33 +00002154static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV,
2155 SDLoc DL, unsigned Offset, EVT PtrVT,
2156 unsigned GAFlags = SIInstrInfo::MO_NONE) {
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002157 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
2158 // lowered to the following code sequence:
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002159 //
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002160 // For constant address space:
2161 // s_getpc_b64 s[0:1]
2162 // s_add_u32 s0, s0, $symbol
2163 // s_addc_u32 s1, s1, 0
2164 //
2165 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
2166 // a fixup or relocation is emitted to replace $symbol with a literal
2167 // constant, which is a pc-relative offset from the encoding of the $symbol
2168 // operand to the global variable.
2169 //
2170 // For global address space:
2171 // s_getpc_b64 s[0:1]
2172 // s_add_u32 s0, s0, $symbol@{gotpc}rel32@lo
2173 // s_addc_u32 s1, s1, $symbol@{gotpc}rel32@hi
2174 //
2175 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
2176 // fixups or relocations are emitted to replace $symbol@*@lo and
2177 // $symbol@*@hi with lower 32 bits and higher 32 bits of a literal constant,
2178 // which is a 64-bit pc-relative offset from the encoding of the $symbol
2179 // operand to the global variable.
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002180 //
2181 // What we want here is an offset from the value returned by s_getpc
2182 // (which is the address of the s_add_u32 instruction) to the global
2183 // variable, but since the encoding of $symbol starts 4 bytes after the start
2184 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too
2185 // small. This requires us to add 4 to the global variable offset in order to
2186 // compute the correct address.
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002187 SDValue PtrLo = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
2188 GAFlags);
2189 SDValue PtrHi = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
2190 GAFlags == SIInstrInfo::MO_NONE ?
2191 GAFlags : GAFlags + 1);
2192 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, PtrLo, PtrHi);
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002193}
2194
Tom Stellard418beb72016-07-13 14:23:33 +00002195SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
2196 SDValue Op,
2197 SelectionDAG &DAG) const {
2198 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
2199
2200 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS &&
2201 GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS)
2202 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
2203
2204 SDLoc DL(GSD);
2205 const GlobalValue *GV = GSD->getGlobal();
2206 EVT PtrVT = Op.getValueType();
2207
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00002208 if (shouldEmitFixup(GV))
Tom Stellard418beb72016-07-13 14:23:33 +00002209 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00002210 else if (shouldEmitPCReloc(GV))
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002211 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT,
2212 SIInstrInfo::MO_REL32);
Tom Stellard418beb72016-07-13 14:23:33 +00002213
2214 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002215 SIInstrInfo::MO_GOTPCREL32);
Tom Stellard418beb72016-07-13 14:23:33 +00002216
2217 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext());
2218 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
2219 const DataLayout &DataLayout = DAG.getDataLayout();
2220 unsigned Align = DataLayout.getABITypeAlignment(PtrTy);
2221 // FIXME: Use a PseudoSourceValue once those can be assigned an address space.
2222 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
2223
Justin Lebar9c375812016-07-15 18:27:10 +00002224 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align,
Justin Lebaradbf09e2016-09-11 01:38:58 +00002225 MachineMemOperand::MODereferenceable |
2226 MachineMemOperand::MOInvariant);
Tom Stellard418beb72016-07-13 14:23:33 +00002227}
2228
Matt Arsenault0bb294b2016-06-17 22:27:03 +00002229SDValue SITargetLowering::lowerTRAP(SDValue Op,
2230 SelectionDAG &DAG) const {
2231 const MachineFunction &MF = DAG.getMachineFunction();
2232 DiagnosticInfoUnsupported NoTrap(*MF.getFunction(),
2233 "trap handler not supported",
2234 Op.getDebugLoc(),
2235 DS_Warning);
2236 DAG.getContext()->diagnose(NoTrap);
2237
2238 // Emit s_endpgm.
2239
2240 // FIXME: This should really be selected to s_trap, but that requires
2241 // setting up the trap handler for it o do anything.
Matt Arsenault9babdf42016-06-22 20:15:28 +00002242 return DAG.getNode(AMDGPUISD::ENDPGM, SDLoc(Op), MVT::Other,
2243 Op.getOperand(0));
Matt Arsenault0bb294b2016-06-17 22:27:03 +00002244}
2245
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002246SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain,
2247 const SDLoc &DL, SDValue V) const {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00002248 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
2249 // the destination register.
2250 //
Tom Stellardfc92e772015-05-12 14:18:14 +00002251 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
2252 // so we will end up with redundant moves to m0.
2253 //
Matt Arsenault4ac341c2016-04-14 21:58:15 +00002254 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
2255
2256 // A Null SDValue creates a glue result.
2257 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
2258 V, Chain);
2259 return SDValue(M0, 0);
Tom Stellardfc92e772015-05-12 14:18:14 +00002260}
2261
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002262SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
2263 SDValue Op,
2264 MVT VT,
2265 unsigned Offset) const {
2266 SDLoc SL(Op);
2267 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
2268 DAG.getEntryNode(), Offset, false);
2269 // The local size values will have the hi 16-bits as zero.
2270 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
2271 DAG.getValueType(VT));
2272}
2273
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002274static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
Matt Arsenaulte0132462016-01-30 05:19:45 +00002275 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002276 "non-hsa intrinsic with hsa target",
2277 DL.getDebugLoc());
2278 DAG.getContext()->diagnose(BadIntrin);
2279 return DAG.getUNDEF(VT);
2280}
2281
2282static SDValue emitRemovedIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
2283 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
2284 "intrinsic not supported on subtarget",
2285 DL.getDebugLoc());
Matt Arsenaulte0132462016-01-30 05:19:45 +00002286 DAG.getContext()->diagnose(BadIntrin);
2287 return DAG.getUNDEF(VT);
2288}
2289
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002290SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2291 SelectionDAG &DAG) const {
2292 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00002293 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002294 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002295
2296 EVT VT = Op.getValueType();
2297 SDLoc DL(Op);
2298 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2299
Sanjay Patela2607012015-09-16 16:31:21 +00002300 // TODO: Should this propagate fast-math-flags?
2301
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002302 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00002303 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault48ab5262016-04-25 19:27:18 +00002304 case Intrinsic::amdgcn_queue_ptr: {
Tom Stellard0b76fc4c2016-09-16 21:34:26 +00002305 if (!Subtarget->isAmdCodeObjectV2()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00002306 DiagnosticInfoUnsupported BadIntrin(
2307 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
2308 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00002309 DAG.getContext()->diagnose(BadIntrin);
2310 return DAG.getUNDEF(VT);
2311 }
2312
Matt Arsenault48ab5262016-04-25 19:27:18 +00002313 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
2314 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00002315 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
Matt Arsenault48ab5262016-04-25 19:27:18 +00002316 TRI->getPreloadedValue(MF, Reg), VT);
2317 }
Jan Veselyfea814d2016-06-21 20:46:20 +00002318 case Intrinsic::amdgcn_implicitarg_ptr: {
2319 unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
2320 return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset);
2321 }
Matt Arsenaultdc4ebad2016-04-29 21:16:52 +00002322 case Intrinsic::amdgcn_kernarg_segment_ptr: {
2323 unsigned Reg
2324 = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
2325 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2326 }
Matt Arsenault8d718dc2016-07-22 17:01:30 +00002327 case Intrinsic::amdgcn_dispatch_id: {
2328 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_ID);
2329 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2330 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002331 case Intrinsic::amdgcn_rcp:
2332 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
2333 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00002334 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002335 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002336 case Intrinsic::amdgcn_rsq_legacy: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002337 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002338 return emitRemovedIntrinsicError(DAG, DL, VT);
2339
2340 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
2341 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002342 case Intrinsic::amdgcn_rcp_legacy: {
2343 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
2344 return emitRemovedIntrinsicError(DAG, DL, VT);
2345 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1));
2346 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +00002347 case Intrinsic::amdgcn_rsq_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002348 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00002349 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00002350
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002351 Type *Type = VT.getTypeForEVT(*DAG.getContext());
2352 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
2353 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
2354
2355 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
2356 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
2357 DAG.getConstantFP(Max, DL, VT));
2358 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
2359 DAG.getConstantFP(Min, DL, VT));
2360 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002361 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002362 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002363 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002364
Tom Stellardec2e43c2014-09-22 15:35:29 +00002365 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2366 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002367 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002368 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002369 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002370
Tom Stellardec2e43c2014-09-22 15:35:29 +00002371 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2372 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002373 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002374 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002375 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002376
Tom Stellardec2e43c2014-09-22 15:35:29 +00002377 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2378 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002379 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002380 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002381 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002382
Tom Stellardec2e43c2014-09-22 15:35:29 +00002383 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2384 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002385 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002386 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002387 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002388
Tom Stellardec2e43c2014-09-22 15:35:29 +00002389 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2390 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002391 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002392 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002393 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002394
Tom Stellardec2e43c2014-09-22 15:35:29 +00002395 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2396 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002397 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002398 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002399 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002400
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002401 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2402 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002403 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002404 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002405 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002406
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002407 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2408 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002409 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002410 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002411 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002412
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002413 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2414 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenault43976df2016-01-30 04:25:19 +00002415 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002416 case Intrinsic::r600_read_tgid_x:
Marek Olsak79c05872016-11-25 17:37:09 +00002417 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002418 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002419 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002420 case Intrinsic::r600_read_tgid_y:
Marek Olsak79c05872016-11-25 17:37:09 +00002421 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002422 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002423 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002424 case Intrinsic::r600_read_tgid_z:
Marek Olsak79c05872016-11-25 17:37:09 +00002425 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32_XM0RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002426 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002427 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002428 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002429 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002430 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002431 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002432 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002433 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002434 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002435 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002436 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002437 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002438 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002439 case AMDGPUIntrinsic::SI_load_const: {
2440 SDValue Ops[] = {
2441 Op.getOperand(1),
2442 Op.getOperand(2)
2443 };
2444
2445 MachineMemOperand *MMO = MF.getMachineMemOperand(
Justin Lebaradbf09e2016-09-11 01:38:58 +00002446 MachinePointerInfo(),
2447 MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable |
2448 MachineMemOperand::MOInvariant,
2449 VT.getStoreSize(), 4);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002450 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
2451 Op->getVTList(), Ops, VT, MMO);
2452 }
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002453 case AMDGPUIntrinsic::amdgcn_fdiv_fast: {
2454 return lowerFDIV_FAST(Op, DAG);
2455 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002456 case AMDGPUIntrinsic::SI_vs_load_input:
2457 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
2458 Op.getOperand(1),
2459 Op.getOperand(2),
2460 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00002461
Tom Stellard2a9d9472015-05-12 15:00:46 +00002462 case AMDGPUIntrinsic::SI_fs_constant: {
2463 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2464 SDValue Glue = M0.getValue(1);
2465 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
2466 DAG.getConstant(2, DL, MVT::i32), // P0
2467 Op.getOperand(1), Op.getOperand(2), Glue);
2468 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00002469 case AMDGPUIntrinsic::SI_packf16:
2470 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
2471 return DAG.getUNDEF(MVT::i32);
2472 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00002473 case AMDGPUIntrinsic::SI_fs_interp: {
2474 SDValue IJ = Op.getOperand(4);
2475 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2476 DAG.getConstant(0, DL, MVT::i32));
2477 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2478 DAG.getConstant(1, DL, MVT::i32));
Tom Stellard1473f072016-11-26 02:26:04 +00002479 I = DAG.getNode(ISD::BITCAST, DL, MVT::f32, I);
2480 J = DAG.getNode(ISD::BITCAST, DL, MVT::f32, J);
Tom Stellard2a9d9472015-05-12 15:00:46 +00002481 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2482 SDValue Glue = M0.getValue(1);
2483 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
2484 DAG.getVTList(MVT::f32, MVT::Glue),
2485 I, Op.getOperand(1), Op.getOperand(2), Glue);
2486 Glue = SDValue(P1.getNode(), 1);
2487 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
2488 Op.getOperand(1), Op.getOperand(2), Glue);
2489 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00002490 case Intrinsic::amdgcn_interp_p1: {
2491 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
2492 SDValue Glue = M0.getValue(1);
2493 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
2494 Op.getOperand(2), Op.getOperand(3), Glue);
2495 }
2496 case Intrinsic::amdgcn_interp_p2: {
2497 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
2498 SDValue Glue = SDValue(M0.getNode(), 1);
2499 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
2500 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
2501 Glue);
2502 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002503 case Intrinsic::amdgcn_sin:
2504 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
2505
2506 case Intrinsic::amdgcn_cos:
2507 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
2508
2509 case Intrinsic::amdgcn_log_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002510 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002511 return SDValue();
2512
2513 DiagnosticInfoUnsupported BadIntrin(
2514 *MF.getFunction(), "intrinsic not supported on subtarget",
2515 DL.getDebugLoc());
2516 DAG.getContext()->diagnose(BadIntrin);
2517 return DAG.getUNDEF(VT);
2518 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002519 case Intrinsic::amdgcn_ldexp:
2520 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
2521 Op.getOperand(1), Op.getOperand(2));
Matt Arsenault74015162016-05-28 00:19:52 +00002522
2523 case Intrinsic::amdgcn_fract:
2524 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
2525
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002526 case Intrinsic::amdgcn_class:
2527 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
2528 Op.getOperand(1), Op.getOperand(2));
2529 case Intrinsic::amdgcn_div_fmas:
2530 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
2531 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
2532 Op.getOperand(4));
2533
2534 case Intrinsic::amdgcn_div_fixup:
2535 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
2536 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
2537
2538 case Intrinsic::amdgcn_trig_preop:
2539 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
2540 Op.getOperand(1), Op.getOperand(2));
2541 case Intrinsic::amdgcn_div_scale: {
2542 // 3rd parameter required to be a constant.
2543 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2544 if (!Param)
2545 return DAG.getUNDEF(VT);
2546
2547 // Translate to the operands expected by the machine instruction. The
2548 // first parameter must be the same as the first instruction.
2549 SDValue Numerator = Op.getOperand(1);
2550 SDValue Denominator = Op.getOperand(2);
2551
2552 // Note this order is opposite of the machine instruction's operations,
2553 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
2554 // intrinsic has the numerator as the first operand to match a normal
2555 // division operation.
2556
2557 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
2558
2559 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
2560 Denominator, Numerator);
2561 }
Wei Ding07e03712016-07-28 16:42:13 +00002562 case Intrinsic::amdgcn_icmp: {
2563 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2564 int CondCode = CD->getSExtValue();
2565
2566 if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002567 CondCode >= ICmpInst::Predicate::BAD_ICMP_PREDICATE)
Wei Ding07e03712016-07-28 16:42:13 +00002568 return DAG.getUNDEF(VT);
2569
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002570 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002571 ISD::CondCode CCOpcode = getICmpCondCode(IcInput);
2572 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2573 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2574 }
2575 case Intrinsic::amdgcn_fcmp: {
2576 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2577 int CondCode = CD->getSExtValue();
2578
2579 if (CondCode <= FCmpInst::Predicate::FCMP_FALSE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002580 CondCode >= FCmpInst::Predicate::FCMP_TRUE)
Wei Ding07e03712016-07-28 16:42:13 +00002581 return DAG.getUNDEF(VT);
2582
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002583 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002584 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput);
2585 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2586 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2587 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002588 case Intrinsic::amdgcn_fmul_legacy:
2589 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT,
2590 Op.getOperand(1), Op.getOperand(2));
Matt Arsenaultc96e1de2016-07-18 18:35:05 +00002591 case Intrinsic::amdgcn_sffbh:
2592 case AMDGPUIntrinsic::AMDGPU_flbit_i32: // Legacy name.
2593 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002594 default:
2595 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
2596 }
2597}
2598
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002599SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
2600 SelectionDAG &DAG) const {
2601 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2602 switch (IntrID) {
2603 case Intrinsic::amdgcn_atomic_inc:
2604 case Intrinsic::amdgcn_atomic_dec: {
2605 MemSDNode *M = cast<MemSDNode>(Op);
2606 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
2607 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
2608 SDValue Ops[] = {
2609 M->getOperand(0), // Chain
2610 M->getOperand(2), // Ptr
2611 M->getOperand(3) // Value
2612 };
2613
2614 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
2615 M->getMemoryVT(), M->getMemOperand());
2616 }
2617 default:
2618 return SDValue();
2619 }
2620}
2621
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002622SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
2623 SelectionDAG &DAG) const {
2624 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00002625 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002626 SDValue Chain = Op.getOperand(0);
2627 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2628
2629 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00002630 case AMDGPUIntrinsic::SI_sendmsg: {
2631 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
2632 SDValue Glue = Chain.getValue(1);
2633 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
2634 Op.getOperand(2), Glue);
2635 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002636 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002637 SDValue Ops[] = {
2638 Chain,
2639 Op.getOperand(2),
2640 Op.getOperand(3),
2641 Op.getOperand(4),
2642 Op.getOperand(5),
2643 Op.getOperand(6),
2644 Op.getOperand(7),
2645 Op.getOperand(8),
2646 Op.getOperand(9),
2647 Op.getOperand(10),
2648 Op.getOperand(11),
2649 Op.getOperand(12),
2650 Op.getOperand(13),
2651 Op.getOperand(14)
2652 };
2653
2654 EVT VT = Op.getOperand(3).getValueType();
2655
2656 MachineMemOperand *MMO = MF.getMachineMemOperand(
2657 MachinePointerInfo(),
2658 MachineMemOperand::MOStore,
2659 VT.getStoreSize(), 4);
2660 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
2661 Op->getVTList(), Ops, VT, MMO);
2662 }
Matt Arsenault00568682016-07-13 06:04:22 +00002663 case AMDGPUIntrinsic::AMDGPU_kill: {
Matt Arsenault03006fd2016-07-19 16:27:56 +00002664 SDValue Src = Op.getOperand(2);
2665 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) {
Matt Arsenault00568682016-07-13 06:04:22 +00002666 if (!K->isNegative())
2667 return Chain;
Matt Arsenault03006fd2016-07-19 16:27:56 +00002668
2669 SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32);
2670 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne);
Matt Arsenault00568682016-07-13 06:04:22 +00002671 }
2672
Matt Arsenault03006fd2016-07-19 16:27:56 +00002673 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src);
2674 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast);
Matt Arsenault00568682016-07-13 06:04:22 +00002675 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002676 default:
2677 return SDValue();
2678 }
2679}
2680
Tom Stellard81d871d2013-11-13 23:36:50 +00002681SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2682 SDLoc DL(Op);
2683 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00002684 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00002685 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00002686
Matt Arsenaulta1436412016-02-10 18:21:45 +00002687 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
Matt Arsenault6dfda962016-02-10 18:21:39 +00002688 // FIXME: Copied from PPC
2689 // First, load into 32 bits, then truncate to 1 bit.
2690
2691 SDValue Chain = Load->getChain();
2692 SDValue BasePtr = Load->getBasePtr();
2693 MachineMemOperand *MMO = Load->getMemOperand();
2694
Tom Stellard115a6152016-11-10 16:02:37 +00002695 EVT RealMemVT = (MemVT == MVT::i1) ? MVT::i8 : MVT::i16;
2696
Matt Arsenault6dfda962016-02-10 18:21:39 +00002697 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
Tom Stellard115a6152016-11-10 16:02:37 +00002698 BasePtr, RealMemVT, MMO);
Matt Arsenault6dfda962016-02-10 18:21:39 +00002699
2700 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002701 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00002702 NewLD.getValue(1)
2703 };
2704
2705 return DAG.getMergeValues(Ops, DL);
2706 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002707
Matt Arsenaulta1436412016-02-10 18:21:45 +00002708 if (!MemVT.isVector())
2709 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002710
Matt Arsenaulta1436412016-02-10 18:21:45 +00002711 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
2712 "Custom lowering for non-i32 vectors hasn't been implemented.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002713
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002714 unsigned AS = Load->getAddressSpace();
2715 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
2716 AS, Load->getAlignment())) {
2717 SDValue Ops[2];
2718 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
2719 return DAG.getMergeValues(Ops, DL);
2720 }
2721
Tom Stellardf8e6eaf2016-10-26 14:38:47 +00002722 MachineFunction &MF = DAG.getMachineFunction();
2723 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
2724 // If there is a possibilty that flat instruction access scratch memory
2725 // then we need to use the same legalization rules we use for private.
2726 if (AS == AMDGPUAS::FLAT_ADDRESS)
2727 AS = MFI->hasFlatScratchInit() ?
2728 AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS;
2729
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002730 unsigned NumElements = MemVT.getVectorNumElements();
2731 switch (AS) {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002732 case AMDGPUAS::CONSTANT_ADDRESS:
2733 if (isMemOpUniform(Load))
2734 return SDValue();
2735 // Non-uniform loads will be selected to MUBUF instructions, so they
2736 // have the same legalization requires ments as global and private
2737 // loads.
2738 //
Justin Bognerb03fd122016-08-17 05:10:15 +00002739 LLVM_FALLTHROUGH;
Matt Arsenaulta1436412016-02-10 18:21:45 +00002740 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002741 case AMDGPUAS::FLAT_ADDRESS:
2742 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00002743 return SplitVectorLoad(Op, DAG);
2744 // v4 loads are supported for private and global memory.
2745 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002746 case AMDGPUAS::PRIVATE_ADDRESS: {
2747 // Depending on the setting of the private_element_size field in the
2748 // resource descriptor, we can only make private accesses up to a certain
2749 // size.
2750 switch (Subtarget->getMaxPrivateElementSize()) {
2751 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002752 return scalarizeVectorLoad(Load, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002753 case 8:
2754 if (NumElements > 2)
2755 return SplitVectorLoad(Op, DAG);
2756 return SDValue();
2757 case 16:
2758 // Same as global/flat
2759 if (NumElements > 4)
2760 return SplitVectorLoad(Op, DAG);
2761 return SDValue();
2762 default:
2763 llvm_unreachable("unsupported private_element_size");
2764 }
2765 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002766 case AMDGPUAS::LOCAL_ADDRESS: {
2767 if (NumElements > 2)
2768 return SplitVectorLoad(Op, DAG);
2769
2770 if (NumElements == 2)
2771 return SDValue();
2772
Matt Arsenaulta1436412016-02-10 18:21:45 +00002773 // If properly aligned, if we split we might be able to use ds_read_b64.
2774 return SplitVectorLoad(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002775 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00002776 default:
2777 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00002778 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002779}
2780
Tom Stellard0ec134f2014-02-04 17:18:40 +00002781SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2782 if (Op.getValueType() != MVT::i64)
2783 return SDValue();
2784
2785 SDLoc DL(Op);
2786 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002787
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002788 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2789 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002790
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002791 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
2792 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
2793
2794 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
2795 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002796
2797 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
2798
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002799 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
2800 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002801
2802 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
2803
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002804 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002805 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002806}
2807
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002808// Catch division cases where we can use shortcuts with rcp and rsq
2809// instructions.
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002810SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op,
2811 SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002812 SDLoc SL(Op);
2813 SDValue LHS = Op.getOperand(0);
2814 SDValue RHS = Op.getOperand(1);
2815 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002816 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002817
2818 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault979902b2016-08-02 22:25:04 +00002819 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals()))) {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002820
Matt Arsenault979902b2016-08-02 22:25:04 +00002821 if (CLHS->isExactlyValue(1.0)) {
2822 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
2823 // the CI documentation has a worst case error of 1 ulp.
2824 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
2825 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002826
Matt Arsenault979902b2016-08-02 22:25:04 +00002827 // 1.0 / sqrt(x) -> rsq(x)
2828 //
2829 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
2830 // error seems really high at 2^29 ULP.
2831 if (RHS.getOpcode() == ISD::FSQRT)
2832 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
2833
2834 // 1.0 / x -> rcp(x)
2835 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
2836 }
2837
2838 // Same as for 1.0, but expand the sign out of the constant.
2839 if (CLHS->isExactlyValue(-1.0)) {
2840 // -1.0 / x -> rcp (fneg x)
2841 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2842 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS);
2843 }
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002844 }
2845 }
2846
Wei Dinged0f97f2016-06-09 19:17:15 +00002847 const SDNodeFlags *Flags = Op->getFlags();
2848
2849 if (Unsafe || Flags->hasAllowReciprocal()) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002850 // Turn into multiply by the reciprocal.
2851 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00002852 SDNodeFlags Flags;
2853 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002854 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00002855 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002856 }
2857
2858 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002859}
2860
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002861// Faster 2.5 ULP division that does not support denormals.
2862SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const {
2863 SDLoc SL(Op);
2864 SDValue LHS = Op.getOperand(1);
2865 SDValue RHS = Op.getOperand(2);
2866
2867 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
2868
2869 const APFloat K0Val(BitsToFloat(0x6f800000));
2870 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
2871
2872 const APFloat K1Val(BitsToFloat(0x2f800000));
2873 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
2874
2875 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
2876
2877 EVT SetCCVT =
2878 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
2879
2880 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
2881
2882 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
2883
2884 // TODO: Should this propagate fast-math-flags?
2885 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
2886
2887 // rcp does not support denormals.
2888 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
2889
2890 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
2891
2892 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
2893}
2894
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002895SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002896 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
Eric Christopher538d09d02016-06-07 20:27:12 +00002897 return FastLowered;
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002898
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002899 SDLoc SL(Op);
2900 SDValue LHS = Op.getOperand(0);
2901 SDValue RHS = Op.getOperand(1);
2902
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002903 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002904
Wei Dinged0f97f2016-06-09 19:17:15 +00002905 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002906
Wei Dinged0f97f2016-06-09 19:17:15 +00002907 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, RHS, RHS, LHS);
2908 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, LHS, RHS, LHS);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002909
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002910 // Denominator is scaled to not be denormal, so using rcp is ok.
Wei Dinged0f97f2016-06-09 19:17:15 +00002911 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002912
Wei Dinged0f97f2016-06-09 19:17:15 +00002913 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002914
Wei Dinged0f97f2016-06-09 19:17:15 +00002915 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, ApproxRcp, One);
2916 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, ApproxRcp);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002917
Wei Dinged0f97f2016-06-09 19:17:15 +00002918 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002919
Wei Dinged0f97f2016-06-09 19:17:15 +00002920 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, NumeratorScaled);
2921 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul);
2922 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, NumeratorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002923
Wei Dinged0f97f2016-06-09 19:17:15 +00002924 SDValue Scale = NumeratorScaled.getValue(1);
2925 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, Fma4, Fma1, Fma3, Scale);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002926
Wei Dinged0f97f2016-06-09 19:17:15 +00002927 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002928}
2929
2930SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002931 if (DAG.getTarget().Options.UnsafeFPMath)
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002932 return lowerFastUnsafeFDIV(Op, DAG);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002933
2934 SDLoc SL(Op);
2935 SDValue X = Op.getOperand(0);
2936 SDValue Y = Op.getOperand(1);
2937
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002938 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002939
2940 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
2941
2942 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
2943
2944 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
2945
2946 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
2947
2948 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
2949
2950 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
2951
2952 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
2953
2954 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
2955
2956 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
2957 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
2958
2959 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
2960 NegDivScale0, Mul, DivScale1);
2961
2962 SDValue Scale;
2963
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002964 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002965 // Workaround a hardware bug on SI where the condition output from div_scale
2966 // is not usable.
2967
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002968 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002969
2970 // Figure out if the scale to use for div_fmas.
2971 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
2972 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
2973 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
2974 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
2975
2976 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
2977 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
2978
2979 SDValue Scale0Hi
2980 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
2981 SDValue Scale1Hi
2982 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
2983
2984 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
2985 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
2986 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
2987 } else {
2988 Scale = DivScale1.getValue(1);
2989 }
2990
2991 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
2992 Fma4, Fma3, Mul, Scale);
2993
2994 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002995}
2996
2997SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
2998 EVT VT = Op.getValueType();
2999
3000 if (VT == MVT::f32)
3001 return LowerFDIV32(Op, DAG);
3002
3003 if (VT == MVT::f64)
3004 return LowerFDIV64(Op, DAG);
3005
3006 llvm_unreachable("Unexpected type for fdiv");
3007}
3008
Tom Stellard81d871d2013-11-13 23:36:50 +00003009SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
3010 SDLoc DL(Op);
3011 StoreSDNode *Store = cast<StoreSDNode>(Op);
3012 EVT VT = Store->getMemoryVT();
3013
Matt Arsenault95245662016-02-11 05:32:46 +00003014 if (VT == MVT::i1) {
3015 return DAG.getTruncStore(Store->getChain(), DL,
3016 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
3017 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00003018 }
3019
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003020 assert(VT.isVector() &&
3021 Store->getValue().getValueType().getScalarType() == MVT::i32);
3022
3023 unsigned AS = Store->getAddressSpace();
3024 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
3025 AS, Store->getAlignment())) {
3026 return expandUnalignedStore(Store, DAG);
3027 }
Tom Stellard81d871d2013-11-13 23:36:50 +00003028
Tom Stellardf8e6eaf2016-10-26 14:38:47 +00003029 MachineFunction &MF = DAG.getMachineFunction();
3030 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
3031 // If there is a possibilty that flat instruction access scratch memory
3032 // then we need to use the same legalization rules we use for private.
3033 if (AS == AMDGPUAS::FLAT_ADDRESS)
3034 AS = MFI->hasFlatScratchInit() ?
3035 AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS;
3036
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003037 unsigned NumElements = VT.getVectorNumElements();
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003038 switch (AS) {
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003039 case AMDGPUAS::GLOBAL_ADDRESS:
3040 case AMDGPUAS::FLAT_ADDRESS:
3041 if (NumElements > 4)
3042 return SplitVectorStore(Op, DAG);
3043 return SDValue();
3044 case AMDGPUAS::PRIVATE_ADDRESS: {
3045 switch (Subtarget->getMaxPrivateElementSize()) {
3046 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00003047 return scalarizeVectorStore(Store, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003048 case 8:
3049 if (NumElements > 2)
3050 return SplitVectorStore(Op, DAG);
3051 return SDValue();
3052 case 16:
3053 if (NumElements > 4)
3054 return SplitVectorStore(Op, DAG);
3055 return SDValue();
3056 default:
3057 llvm_unreachable("unsupported private_element_size");
3058 }
3059 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003060 case AMDGPUAS::LOCAL_ADDRESS: {
3061 if (NumElements > 2)
3062 return SplitVectorStore(Op, DAG);
3063
3064 if (NumElements == 2)
3065 return Op;
3066
Matt Arsenault95245662016-02-11 05:32:46 +00003067 // If properly aligned, if we split we might be able to use ds_write_b64.
3068 return SplitVectorStore(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003069 }
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003070 default:
3071 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00003072 }
Tom Stellard81d871d2013-11-13 23:36:50 +00003073}
3074
Matt Arsenaultad14ce82014-07-19 18:44:39 +00003075SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003076 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00003077 EVT VT = Op.getValueType();
3078 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00003079 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003080 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
3081 DAG.getNode(ISD::FMUL, DL, VT, Arg,
3082 DAG.getConstantFP(0.5/M_PI, DL,
3083 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00003084
3085 switch (Op.getOpcode()) {
3086 case ISD::FCOS:
3087 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
3088 case ISD::FSIN:
3089 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
3090 default:
3091 llvm_unreachable("Wrong trig opcode");
3092 }
3093}
3094
Tom Stellard354a43c2016-04-01 18:27:37 +00003095SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
3096 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
3097 assert(AtomicNode->isCompareAndSwap());
3098 unsigned AS = AtomicNode->getAddressSpace();
3099
3100 // No custom lowering required for local address space
3101 if (!isFlatGlobalAddrSpace(AS))
3102 return Op;
3103
3104 // Non-local address space requires custom lowering for atomic compare
3105 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
3106 SDLoc DL(Op);
3107 SDValue ChainIn = Op.getOperand(0);
3108 SDValue Addr = Op.getOperand(1);
3109 SDValue Old = Op.getOperand(2);
3110 SDValue New = Op.getOperand(3);
3111 EVT VT = Op.getValueType();
3112 MVT SimpleVT = VT.getSimpleVT();
3113 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
3114
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003115 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
Tom Stellard354a43c2016-04-01 18:27:37 +00003116 SDValue Ops[] = { ChainIn, Addr, NewOld };
Matt Arsenault88701812016-06-09 23:42:48 +00003117
3118 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(),
3119 Ops, VT, AtomicNode->getMemOperand());
Tom Stellard354a43c2016-04-01 18:27:37 +00003120}
3121
Tom Stellard75aadc22012-12-11 21:25:42 +00003122//===----------------------------------------------------------------------===//
3123// Custom DAG optimizations
3124//===----------------------------------------------------------------------===//
3125
Matt Arsenault364a6742014-06-11 17:50:44 +00003126SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00003127 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00003128 EVT VT = N->getValueType(0);
3129 EVT ScalarVT = VT.getScalarType();
3130 if (ScalarVT != MVT::f32)
3131 return SDValue();
3132
3133 SelectionDAG &DAG = DCI.DAG;
3134 SDLoc DL(N);
3135
3136 SDValue Src = N->getOperand(0);
3137 EVT SrcVT = Src.getValueType();
3138
3139 // TODO: We could try to match extracting the higher bytes, which would be
3140 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
3141 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
3142 // about in practice.
3143 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
3144 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
3145 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
3146 DCI.AddToWorklist(Cvt.getNode());
3147 return Cvt;
3148 }
3149 }
3150
Matt Arsenault364a6742014-06-11 17:50:44 +00003151 return SDValue();
3152}
3153
Eric Christopher6c5b5112015-03-11 18:43:21 +00003154/// \brief Return true if the given offset Size in bytes can be folded into
3155/// the immediate offsets of a memory instruction for the given address space.
3156static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003157 const SISubtarget &STI) {
Eric Christopher6c5b5112015-03-11 18:43:21 +00003158 switch (AS) {
3159 case AMDGPUAS::GLOBAL_ADDRESS: {
3160 // MUBUF instructions a 12-bit offset in bytes.
3161 return isUInt<12>(OffsetSize);
3162 }
3163 case AMDGPUAS::CONSTANT_ADDRESS: {
3164 // SMRD instructions have an 8-bit offset in dwords on SI and
3165 // a 20-bit offset in bytes on VI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003166 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Eric Christopher6c5b5112015-03-11 18:43:21 +00003167 return isUInt<20>(OffsetSize);
3168 else
3169 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
3170 }
3171 case AMDGPUAS::LOCAL_ADDRESS:
3172 case AMDGPUAS::REGION_ADDRESS: {
3173 // The single offset versions have a 16-bit offset in bytes.
3174 return isUInt<16>(OffsetSize);
3175 }
3176 case AMDGPUAS::PRIVATE_ADDRESS:
3177 // Indirect register addressing does not use any offsets.
3178 default:
3179 return 0;
3180 }
3181}
3182
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003183// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
3184
3185// This is a variant of
3186// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
3187//
3188// The normal DAG combiner will do this, but only if the add has one use since
3189// that would increase the number of instructions.
3190//
3191// This prevents us from seeing a constant offset that can be folded into a
3192// memory instruction's addressing mode. If we know the resulting add offset of
3193// a pointer can be folded into an addressing offset, we can replace the pointer
3194// operand with the add of new constant offset. This eliminates one of the uses,
3195// and may allow the remaining use to also be simplified.
3196//
3197SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
3198 unsigned AddrSpace,
3199 DAGCombinerInfo &DCI) const {
3200 SDValue N0 = N->getOperand(0);
3201 SDValue N1 = N->getOperand(1);
3202
3203 if (N0.getOpcode() != ISD::ADD)
3204 return SDValue();
3205
3206 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
3207 if (!CN1)
3208 return SDValue();
3209
3210 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3211 if (!CAdd)
3212 return SDValue();
3213
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003214 // If the resulting offset is too large, we can't fold it into the addressing
3215 // mode offset.
3216 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003217 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget()))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003218 return SDValue();
3219
3220 SelectionDAG &DAG = DCI.DAG;
3221 SDLoc SL(N);
3222 EVT VT = N->getValueType(0);
3223
3224 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003225 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003226
3227 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
3228}
3229
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003230static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) {
3231 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) ||
3232 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) ||
3233 (Opc == ISD::XOR && Val == 0);
3234}
3235
3236// Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This
3237// will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit
3238// integer combine opportunities since most 64-bit operations are decomposed
3239// this way. TODO: We won't want this for SALU especially if it is an inline
3240// immediate.
3241SDValue SITargetLowering::splitBinaryBitConstantOp(
3242 DAGCombinerInfo &DCI,
3243 const SDLoc &SL,
3244 unsigned Opc, SDValue LHS,
3245 const ConstantSDNode *CRHS) const {
3246 uint64_t Val = CRHS->getZExtValue();
3247 uint32_t ValLo = Lo_32(Val);
3248 uint32_t ValHi = Hi_32(Val);
3249 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
3250
3251 if ((bitOpWithConstantIsReducible(Opc, ValLo) ||
3252 bitOpWithConstantIsReducible(Opc, ValHi)) ||
3253 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) {
3254 // If we need to materialize a 64-bit immediate, it will be split up later
3255 // anyway. Avoid creating the harder to understand 64-bit immediate
3256 // materialization.
3257 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi);
3258 }
3259
3260 return SDValue();
3261}
3262
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003263SDValue SITargetLowering::performAndCombine(SDNode *N,
3264 DAGCombinerInfo &DCI) const {
3265 if (DCI.isBeforeLegalize())
3266 return SDValue();
3267
3268 SelectionDAG &DAG = DCI.DAG;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003269 EVT VT = N->getValueType(0);
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003270 SDValue LHS = N->getOperand(0);
3271 SDValue RHS = N->getOperand(1);
3272
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003273
3274 if (VT == MVT::i64) {
3275 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
3276 if (CRHS) {
3277 if (SDValue Split
3278 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS))
3279 return Split;
3280 }
3281 }
3282
3283 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
3284 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
3285 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) {
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003286 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
3287 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
3288
3289 SDValue X = LHS.getOperand(0);
3290 SDValue Y = RHS.getOperand(0);
3291 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
3292 return SDValue();
3293
3294 if (LCC == ISD::SETO) {
3295 if (X != LHS.getOperand(1))
3296 return SDValue();
3297
3298 if (RCC == ISD::SETUNE) {
3299 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
3300 if (!C1 || !C1->isInfinity() || C1->isNegative())
3301 return SDValue();
3302
3303 const uint32_t Mask = SIInstrFlags::N_NORMAL |
3304 SIInstrFlags::N_SUBNORMAL |
3305 SIInstrFlags::N_ZERO |
3306 SIInstrFlags::P_ZERO |
3307 SIInstrFlags::P_SUBNORMAL |
3308 SIInstrFlags::P_NORMAL;
3309
3310 static_assert(((~(SIInstrFlags::S_NAN |
3311 SIInstrFlags::Q_NAN |
3312 SIInstrFlags::N_INFINITY |
3313 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
3314 "mask not equal");
3315
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003316 SDLoc DL(N);
3317 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
3318 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003319 }
3320 }
3321 }
3322
3323 return SDValue();
3324}
3325
Matt Arsenaultf2290332015-01-06 23:00:39 +00003326SDValue SITargetLowering::performOrCombine(SDNode *N,
3327 DAGCombinerInfo &DCI) const {
3328 SelectionDAG &DAG = DCI.DAG;
3329 SDValue LHS = N->getOperand(0);
3330 SDValue RHS = N->getOperand(1);
3331
Matt Arsenault3b082382016-04-12 18:24:38 +00003332 EVT VT = N->getValueType(0);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003333 if (VT == MVT::i1) {
3334 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
3335 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
3336 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
3337 SDValue Src = LHS.getOperand(0);
3338 if (Src != RHS.getOperand(0))
3339 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003340
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003341 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
3342 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
3343 if (!CLHS || !CRHS)
3344 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003345
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003346 // Only 10 bits are used.
3347 static const uint32_t MaxMask = 0x3ff;
Matt Arsenault3b082382016-04-12 18:24:38 +00003348
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003349 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
3350 SDLoc DL(N);
3351 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
3352 Src, DAG.getConstant(NewMask, DL, MVT::i32));
3353 }
Matt Arsenault3b082382016-04-12 18:24:38 +00003354
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003355 return SDValue();
3356 }
3357
3358 if (VT != MVT::i64)
3359 return SDValue();
3360
3361 // TODO: This could be a generic combine with a predicate for extracting the
3362 // high half of an integer being free.
3363
3364 // (or i64:x, (zero_extend i32:y)) ->
3365 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
3366 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
3367 RHS.getOpcode() != ISD::ZERO_EXTEND)
3368 std::swap(LHS, RHS);
3369
3370 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
3371 SDValue ExtSrc = RHS.getOperand(0);
3372 EVT SrcVT = ExtSrc.getValueType();
3373 if (SrcVT == MVT::i32) {
3374 SDLoc SL(N);
3375 SDValue LowLHS, HiBits;
3376 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
3377 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
3378
3379 DCI.AddToWorklist(LowOr.getNode());
3380 DCI.AddToWorklist(HiBits.getNode());
3381
3382 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3383 LowOr, HiBits);
3384 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
Matt Arsenault3b082382016-04-12 18:24:38 +00003385 }
3386 }
3387
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003388 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
3389 if (CRHS) {
3390 if (SDValue Split
3391 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS))
3392 return Split;
3393 }
Matt Arsenaultf2290332015-01-06 23:00:39 +00003394
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003395 return SDValue();
3396}
Matt Arsenaultf2290332015-01-06 23:00:39 +00003397
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003398SDValue SITargetLowering::performXorCombine(SDNode *N,
3399 DAGCombinerInfo &DCI) const {
3400 EVT VT = N->getValueType(0);
3401 if (VT != MVT::i64)
3402 return SDValue();
Matt Arsenaultf2290332015-01-06 23:00:39 +00003403
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003404 SDValue LHS = N->getOperand(0);
3405 SDValue RHS = N->getOperand(1);
3406
3407 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
3408 if (CRHS) {
3409 if (SDValue Split
3410 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS))
3411 return Split;
Matt Arsenaultf2290332015-01-06 23:00:39 +00003412 }
3413
3414 return SDValue();
3415}
3416
3417SDValue SITargetLowering::performClassCombine(SDNode *N,
3418 DAGCombinerInfo &DCI) const {
3419 SelectionDAG &DAG = DCI.DAG;
3420 SDValue Mask = N->getOperand(1);
3421
3422 // fp_class x, 0 -> false
3423 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
3424 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003425 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003426 }
3427
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003428 if (N->getOperand(0).isUndef())
3429 return DAG.getUNDEF(MVT::i1);
3430
Matt Arsenaultf2290332015-01-06 23:00:39 +00003431 return SDValue();
3432}
3433
Matt Arsenault9cd90712016-04-14 01:42:16 +00003434// Constant fold canonicalize.
3435SDValue SITargetLowering::performFCanonicalizeCombine(
3436 SDNode *N,
3437 DAGCombinerInfo &DCI) const {
3438 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
3439 if (!CFP)
3440 return SDValue();
3441
3442 SelectionDAG &DAG = DCI.DAG;
3443 const APFloat &C = CFP->getValueAPF();
3444
3445 // Flush denormals to 0 if not enabled.
3446 if (C.isDenormal()) {
3447 EVT VT = N->getValueType(0);
3448 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
3449 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3450
3451 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
3452 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3453 }
3454
3455 if (C.isNaN()) {
3456 EVT VT = N->getValueType(0);
3457 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
3458 if (C.isSignaling()) {
3459 // Quiet a signaling NaN.
3460 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3461 }
3462
3463 // Make sure it is the canonical NaN bitpattern.
3464 //
3465 // TODO: Can we use -1 as the canonical NaN value since it's an inline
3466 // immediate?
3467 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
3468 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3469 }
3470
3471 return SDValue(CFP, 0);
3472}
3473
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003474static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
3475 switch (Opc) {
3476 case ISD::FMAXNUM:
3477 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003478 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003479 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003480 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003481 return AMDGPUISD::UMAX3;
3482 case ISD::FMINNUM:
3483 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003484 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003485 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003486 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003487 return AMDGPUISD::UMIN3;
3488 default:
3489 llvm_unreachable("Not a min/max opcode");
3490 }
3491}
3492
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003493static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3494 SDValue Op0, SDValue Op1, bool Signed) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003495 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
3496 if (!K1)
3497 return SDValue();
3498
3499 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
3500 if (!K0)
3501 return SDValue();
3502
Matt Arsenaultf639c322016-01-28 20:53:42 +00003503 if (Signed) {
3504 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
3505 return SDValue();
3506 } else {
3507 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
3508 return SDValue();
3509 }
3510
3511 EVT VT = K0->getValueType(0);
Tom Stellard115a6152016-11-10 16:02:37 +00003512
3513 MVT NVT = MVT::i32;
3514 unsigned ExtOp = Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3515
3516 SDValue Tmp1, Tmp2, Tmp3;
3517 Tmp1 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(0));
3518 Tmp2 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(1));
3519 Tmp3 = DAG.getNode(ExtOp, SL, NVT, Op1);
3520
3521 if (VT == MVT::i16) {
3522 Tmp1 = DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, NVT,
3523 Tmp1, Tmp2, Tmp3);
3524
3525 return DAG.getNode(ISD::TRUNCATE, SL, VT, Tmp1);
3526 } else
3527 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
3528 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
Matt Arsenaultf639c322016-01-28 20:53:42 +00003529}
3530
3531static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
3532 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
3533 return true;
3534
3535 return DAG.isKnownNeverNaN(Op);
3536}
3537
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003538static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3539 SDValue Op0, SDValue Op1) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003540 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
3541 if (!K1)
3542 return SDValue();
3543
3544 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
3545 if (!K0)
3546 return SDValue();
3547
3548 // Ordered >= (although NaN inputs should have folded away by now).
3549 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
3550 if (Cmp == APFloat::cmpGreaterThan)
3551 return SDValue();
3552
3553 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
3554 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
3555 // give the other result, which is different from med3 with a NaN input.
3556 SDValue Var = Op0.getOperand(0);
3557 if (!isKnownNeverSNan(DAG, Var))
3558 return SDValue();
3559
3560 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
3561 Var, SDValue(K0, 0), SDValue(K1, 0));
3562}
3563
3564SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
3565 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003566 SelectionDAG &DAG = DCI.DAG;
3567
3568 unsigned Opc = N->getOpcode();
3569 SDValue Op0 = N->getOperand(0);
3570 SDValue Op1 = N->getOperand(1);
3571
3572 // Only do this if the inner op has one use since this will just increases
3573 // register pressure for no benefit.
3574
Matt Arsenault5b39b342016-01-28 20:53:48 +00003575 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
3576 // max(max(a, b), c) -> max3(a, b, c)
3577 // min(min(a, b), c) -> min3(a, b, c)
3578 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
3579 SDLoc DL(N);
3580 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3581 DL,
3582 N->getValueType(0),
3583 Op0.getOperand(0),
3584 Op0.getOperand(1),
3585 Op1);
3586 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003587
Matt Arsenault5b39b342016-01-28 20:53:48 +00003588 // Try commuted.
3589 // max(a, max(b, c)) -> max3(a, b, c)
3590 // min(a, min(b, c)) -> min3(a, b, c)
3591 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
3592 SDLoc DL(N);
3593 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3594 DL,
3595 N->getValueType(0),
3596 Op0,
3597 Op1.getOperand(0),
3598 Op1.getOperand(1));
3599 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003600 }
3601
Matt Arsenaultf639c322016-01-28 20:53:42 +00003602 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
3603 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
3604 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
3605 return Med3;
3606 }
3607
3608 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
3609 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
3610 return Med3;
3611 }
3612
3613 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00003614 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
3615 (Opc == AMDGPUISD::FMIN_LEGACY &&
3616 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00003617 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
3618 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
3619 return Res;
3620 }
3621
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003622 return SDValue();
3623}
3624
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003625SDValue SITargetLowering::performSetCCCombine(SDNode *N,
3626 DAGCombinerInfo &DCI) const {
3627 SelectionDAG &DAG = DCI.DAG;
3628 SDLoc SL(N);
3629
3630 SDValue LHS = N->getOperand(0);
3631 SDValue RHS = N->getOperand(1);
3632 EVT VT = LHS.getValueType();
3633
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00003634 if (VT != MVT::f32 && VT != MVT::f64 && (Subtarget->has16BitInsts() &&
3635 VT != MVT::f16))
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003636 return SDValue();
3637
3638 // Match isinf pattern
3639 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
3640 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
3641 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
3642 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
3643 if (!CRHS)
3644 return SDValue();
3645
3646 const APFloat &APF = CRHS->getValueAPF();
3647 if (APF.isInfinity() && !APF.isNegative()) {
3648 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003649 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
3650 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003651 }
3652 }
3653
3654 return SDValue();
3655}
3656
Tom Stellard75aadc22012-12-11 21:25:42 +00003657SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
3658 DAGCombinerInfo &DCI) const {
3659 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003660 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00003661
3662 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00003663 default:
3664 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003665 case ISD::SETCC:
3666 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00003667 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003668 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003669 case ISD::SMAX:
3670 case ISD::SMIN:
3671 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00003672 case ISD::UMIN:
3673 case AMDGPUISD::FMIN_LEGACY:
3674 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003675 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00003676 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003677 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00003678 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003679 break;
3680 }
Matt Arsenault364a6742014-06-11 17:50:44 +00003681
3682 case AMDGPUISD::CVT_F32_UBYTE0:
3683 case AMDGPUISD::CVT_F32_UBYTE1:
3684 case AMDGPUISD::CVT_F32_UBYTE2:
3685 case AMDGPUISD::CVT_F32_UBYTE3: {
3686 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +00003687
Matt Arsenault364a6742014-06-11 17:50:44 +00003688 SDValue Src = N->getOperand(0);
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +00003689 SDValue Srl = N->getOperand(0);
3690 if (Srl.getOpcode() == ISD::ZERO_EXTEND)
3691 Srl = Srl.getOperand(0);
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003692
Matt Arsenault327bb5a2016-07-01 22:47:50 +00003693 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero.
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +00003694 if (Srl.getOpcode() == ISD::SRL) {
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003695 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
3696 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
3697 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
3698
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +00003699 if (const ConstantSDNode *C =
3700 dyn_cast<ConstantSDNode>(Srl.getOperand(1))) {
3701 Srl = DAG.getZExtOrTrunc(Srl.getOperand(0), SDLoc(Srl.getOperand(0)),
3702 EVT(MVT::i32));
3703
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003704 unsigned SrcOffset = C->getZExtValue() + 8 * Offset;
3705 if (SrcOffset < 32 && SrcOffset % 8 == 0) {
3706 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, DL,
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +00003707 MVT::f32, Srl);
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003708 }
3709 }
3710 }
3711
Matt Arsenault364a6742014-06-11 17:50:44 +00003712 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
3713
3714 APInt KnownZero, KnownOne;
3715 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3716 !DCI.isBeforeLegalizeOps());
3717 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3718 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
3719 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
3720 DCI.CommitTargetLoweringOpt(TLO);
3721 }
3722
3723 break;
3724 }
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +00003725 case ISD::SINT_TO_FP:
Matt Arsenault364a6742014-06-11 17:50:44 +00003726 case ISD::UINT_TO_FP: {
3727 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003728 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003729 case ISD::FADD: {
3730 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3731 break;
3732
3733 EVT VT = N->getValueType(0);
3734 if (VT != MVT::f32)
3735 break;
3736
Matt Arsenault8d630032015-02-20 22:10:41 +00003737 // Only do this if we are not trying to support denormals. v_mad_f32 does
3738 // not support denormals ever.
3739 if (Subtarget->hasFP32Denormals())
3740 break;
3741
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003742 SDValue LHS = N->getOperand(0);
3743 SDValue RHS = N->getOperand(1);
3744
3745 // These should really be instruction patterns, but writing patterns with
3746 // source modiifiers is a pain.
3747
3748 // fadd (fadd (a, a), b) -> mad 2.0, a, b
3749 if (LHS.getOpcode() == ISD::FADD) {
3750 SDValue A = LHS.getOperand(0);
3751 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003752 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003753 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003754 }
3755 }
3756
3757 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
3758 if (RHS.getOpcode() == ISD::FADD) {
3759 SDValue A = RHS.getOperand(0);
3760 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003761 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003762 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003763 }
3764 }
3765
Matt Arsenault8d630032015-02-20 22:10:41 +00003766 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003767 }
Matt Arsenault8675db12014-08-29 16:01:14 +00003768 case ISD::FSUB: {
3769 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3770 break;
3771
3772 EVT VT = N->getValueType(0);
3773
3774 // Try to get the fneg to fold into the source modifier. This undoes generic
3775 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00003776 //
3777 // Only do this if we are not trying to support denormals. v_mad_f32 does
3778 // not support denormals ever.
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00003779 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00003780 SDValue LHS = N->getOperand(0);
3781 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003782 if (LHS.getOpcode() == ISD::FADD) {
3783 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
3784
3785 SDValue A = LHS.getOperand(0);
3786 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003787 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003788 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
3789
Matt Arsenault8d630032015-02-20 22:10:41 +00003790 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003791 }
3792 }
3793
3794 if (RHS.getOpcode() == ISD::FADD) {
3795 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
3796
3797 SDValue A = RHS.getOperand(0);
3798 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003799 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003800 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003801 }
3802 }
Matt Arsenault8d630032015-02-20 22:10:41 +00003803
3804 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00003805 }
3806
3807 break;
3808 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003809 case ISD::LOAD:
3810 case ISD::STORE:
3811 case ISD::ATOMIC_LOAD:
3812 case ISD::ATOMIC_STORE:
3813 case ISD::ATOMIC_CMP_SWAP:
3814 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
3815 case ISD::ATOMIC_SWAP:
3816 case ISD::ATOMIC_LOAD_ADD:
3817 case ISD::ATOMIC_LOAD_SUB:
3818 case ISD::ATOMIC_LOAD_AND:
3819 case ISD::ATOMIC_LOAD_OR:
3820 case ISD::ATOMIC_LOAD_XOR:
3821 case ISD::ATOMIC_LOAD_NAND:
3822 case ISD::ATOMIC_LOAD_MIN:
3823 case ISD::ATOMIC_LOAD_MAX:
3824 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003825 case ISD::ATOMIC_LOAD_UMAX:
3826 case AMDGPUISD::ATOMIC_INC:
3827 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003828 if (DCI.isBeforeLegalize())
3829 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003830
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003831 MemSDNode *MemNode = cast<MemSDNode>(N);
3832 SDValue Ptr = MemNode->getBasePtr();
3833
3834 // TODO: We could also do this for multiplies.
3835 unsigned AS = MemNode->getAddressSpace();
3836 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
3837 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
3838 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00003839 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003840
3841 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
3842 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
3843 }
3844 }
3845 break;
3846 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003847 case ISD::AND:
3848 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003849 case ISD::OR:
3850 return performOrCombine(N, DCI);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003851 case ISD::XOR:
3852 return performXorCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003853 case AMDGPUISD::FP_CLASS:
3854 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00003855 case ISD::FCANONICALIZE:
3856 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003857 case AMDGPUISD::FRACT:
3858 case AMDGPUISD::RCP:
3859 case AMDGPUISD::RSQ:
Matt Arsenault32fc5272016-07-26 16:45:45 +00003860 case AMDGPUISD::RCP_LEGACY:
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003861 case AMDGPUISD::RSQ_LEGACY:
3862 case AMDGPUISD::RSQ_CLAMP:
3863 case AMDGPUISD::LDEXP: {
3864 SDValue Src = N->getOperand(0);
3865 if (Src.isUndef())
3866 return Src;
3867 break;
3868 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003869 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003870 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00003871}
Christian Konigd910b7d2013-02-26 17:52:16 +00003872
Christian Konig8e06e2a2013-04-10 08:39:08 +00003873/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00003874static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00003875 switch (Idx) {
3876 default: return 0;
3877 case AMDGPU::sub0: return 0;
3878 case AMDGPU::sub1: return 1;
3879 case AMDGPU::sub2: return 2;
3880 case AMDGPU::sub3: return 3;
3881 }
3882}
3883
3884/// \brief Adjust the writemask of MIMG instructions
3885void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
3886 SelectionDAG &DAG) const {
3887 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00003888 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003889 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
3890 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00003891 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003892
3893 // Try to figure out the used register components
3894 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
3895 I != E; ++I) {
3896
3897 // Abort if we can't understand the usage
3898 if (!I->isMachineOpcode() ||
3899 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
3900 return;
3901
Tom Stellard54774e52013-10-23 02:53:47 +00003902 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
3903 // Note that subregs are packed, i.e. Lane==0 is the first bit set
3904 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
3905 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00003906 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00003907
Tom Stellard54774e52013-10-23 02:53:47 +00003908 // Set which texture component corresponds to the lane.
3909 unsigned Comp;
3910 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
3911 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00003912 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00003913 Dmask &= ~(1 << Comp);
3914 }
3915
Christian Konig8e06e2a2013-04-10 08:39:08 +00003916 // Abort if we have more than one user per component
3917 if (Users[Lane])
3918 return;
3919
3920 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00003921 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003922 }
3923
Tom Stellard54774e52013-10-23 02:53:47 +00003924 // Abort if there's no change
3925 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00003926 return;
3927
3928 // Adjust the writemask in the node
3929 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003930 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003931 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003932 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00003933 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003934
Christian Konig8b1ed282013-04-10 08:39:16 +00003935 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00003936 // (if NewDmask has only one bit set...)
3937 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003938 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
3939 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00003940 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003941 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00003942 SDValue(Node, 0), RC);
3943 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
3944 return;
3945 }
3946
Christian Konig8e06e2a2013-04-10 08:39:08 +00003947 // Update the users of the node with the new indices
3948 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
3949
3950 SDNode *User = Users[i];
3951 if (!User)
3952 continue;
3953
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003954 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003955 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
3956
3957 switch (Idx) {
3958 default: break;
3959 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
3960 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
3961 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
3962 }
3963 }
3964}
3965
Tom Stellardc98ee202015-07-16 19:40:07 +00003966static bool isFrameIndexOp(SDValue Op) {
3967 if (Op.getOpcode() == ISD::AssertZext)
3968 Op = Op.getOperand(0);
3969
3970 return isa<FrameIndexSDNode>(Op);
3971}
3972
Tom Stellard3457a842014-10-09 19:06:00 +00003973/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
3974/// with frame index operands.
3975/// LLVM assumes that inputs are to these instructions are registers.
3976void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
3977 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00003978
3979 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00003980 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00003981 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00003982 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003983 continue;
3984 }
3985
Tom Stellard3457a842014-10-09 19:06:00 +00003986 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003987 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00003988 Node->getOperand(i).getValueType(),
3989 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00003990 }
3991
Tom Stellard3457a842014-10-09 19:06:00 +00003992 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00003993}
3994
Matt Arsenault08d84942014-06-03 23:06:13 +00003995/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00003996SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
3997 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003998 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00003999 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00004000
Nicolai Haehnlec06bfa12016-07-11 21:59:43 +00004001 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() &&
4002 !TII->isGather4(Opcode))
Christian Konig8e06e2a2013-04-10 08:39:08 +00004003 adjustWritemask(Node, DAG);
4004
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00004005 if (Opcode == AMDGPU::INSERT_SUBREG ||
4006 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00004007 legalizeTargetIndependentNode(Node, DAG);
4008 return Node;
4009 }
Tom Stellard654d6692015-01-08 15:08:17 +00004010 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00004011}
Christian Konig8b1ed282013-04-10 08:39:16 +00004012
4013/// \brief Assign the register class depending on the number of
4014/// bits set in the writemask
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004015void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Christian Konig8b1ed282013-04-10 08:39:16 +00004016 SDNode *Node) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00004017 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004018
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004019 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00004020
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004021 if (TII->isVOP3(MI.getOpcode())) {
Matt Arsenault6005fcb2015-10-21 21:51:02 +00004022 // Make sure constant bus requirements are respected.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004023 TII->legalizeOperandsVOP3(MRI, MI);
Matt Arsenault6005fcb2015-10-21 21:51:02 +00004024 return;
4025 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00004026
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004027 if (TII->isMIMG(MI)) {
4028 unsigned VReg = MI.getOperand(0).getReg();
Changpeng Fang8236fe12016-11-14 18:33:18 +00004029 const TargetRegisterClass *RC = MRI.getRegClass(VReg);
4030 // TODO: Need mapping tables to handle other cases (register classes).
4031 if (RC != &AMDGPU::VReg_128RegClass)
4032 return;
4033
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004034 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4;
4035 unsigned Writemask = MI.getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004036 unsigned BitsSet = 0;
4037 for (unsigned i = 0; i < 4; ++i)
4038 BitsSet += Writemask & (1 << i) ? 1 : 0;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004039 switch (BitsSet) {
4040 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00004041 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004042 case 2: RC = &AMDGPU::VReg_64RegClass; break;
4043 case 3: RC = &AMDGPU::VReg_96RegClass; break;
4044 }
4045
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004046 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet);
4047 MI.setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004048 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00004049 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00004050 }
4051
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004052 // Replace unused atomics with the no return version.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004053 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004054 if (NoRetAtomicOp != -1) {
4055 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004056 MI.setDesc(TII->get(NoRetAtomicOp));
4057 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00004058 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004059 }
4060
Tom Stellard354a43c2016-04-01 18:27:37 +00004061 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
4062 // instruction, because the return type of these instructions is a vec2 of
4063 // the memory type, so it can be tied to the input operand.
4064 // This means these instructions always have a use, so we need to add a
4065 // special case to check if the atomic has only one extract_subreg use,
4066 // which itself has no uses.
4067 if ((Node->hasNUsesOfValue(1, 0) &&
Nicolai Haehnle750082d2016-04-15 14:42:36 +00004068 Node->use_begin()->isMachineOpcode() &&
Tom Stellard354a43c2016-04-01 18:27:37 +00004069 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
4070 !Node->use_begin()->hasAnyUseOfValue(0))) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004071 unsigned Def = MI.getOperand(0).getReg();
Tom Stellard354a43c2016-04-01 18:27:37 +00004072
4073 // Change this into a noret atomic.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004074 MI.setDesc(TII->get(NoRetAtomicOp));
4075 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00004076
4077 // If we only remove the def operand from the atomic instruction, the
4078 // extract_subreg will be left with a use of a vreg without a def.
4079 // So we need to insert an implicit_def to avoid machine verifier
4080 // errors.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004081 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
Tom Stellard354a43c2016-04-01 18:27:37 +00004082 TII->get(AMDGPU::IMPLICIT_DEF), Def);
4083 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004084 return;
4085 }
Christian Konig8b1ed282013-04-10 08:39:16 +00004086}
Tom Stellard0518ff82013-06-03 17:39:58 +00004087
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004088static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL,
4089 uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004090 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00004091 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
4092}
4093
4094MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004095 const SDLoc &DL,
Matt Arsenault485defe2014-11-05 19:01:17 +00004096 SDValue Ptr) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00004097 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault485defe2014-11-05 19:01:17 +00004098
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004099 // Build the half of the subregister with the constants before building the
4100 // full 128-bit register. If we are building multiple resource descriptors,
4101 // this will allow CSEing of the 2-component register.
4102 const SDValue Ops0[] = {
4103 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
4104 buildSMovImm32(DAG, DL, 0),
4105 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
4106 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
4107 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
4108 };
Matt Arsenault485defe2014-11-05 19:01:17 +00004109
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004110 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
4111 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00004112
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004113 // Combine the constants and the pointer.
4114 const SDValue Ops1[] = {
4115 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
4116 Ptr,
4117 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
4118 SubRegHi,
4119 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
4120 };
Matt Arsenault485defe2014-11-05 19:01:17 +00004121
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004122 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00004123}
4124
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004125/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00004126/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
4127/// of the resource descriptor) to create an offset, which is added to
4128/// the resource pointer.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004129MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL,
4130 SDValue Ptr, uint32_t RsrcDword1,
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004131 uint64_t RsrcDword2And3) const {
4132 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
4133 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
4134 if (RsrcDword1) {
4135 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004136 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
4137 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004138 }
4139
4140 SDValue DataLo = buildSMovImm32(DAG, DL,
4141 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
4142 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
4143
4144 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004145 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004146 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004147 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004148 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004149 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004150 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004151 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004152 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004153 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004154 };
4155
4156 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
4157}
4158
Tom Stellard94593ee2013-06-03 17:40:18 +00004159SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
4160 const TargetRegisterClass *RC,
4161 unsigned Reg, EVT VT) const {
4162 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
4163
4164 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
4165 cast<RegisterSDNode>(VReg)->getReg(), VT);
4166}
Tom Stellardd7e6f132015-04-08 01:09:26 +00004167
4168//===----------------------------------------------------------------------===//
4169// SI Inline Assembly Support
4170//===----------------------------------------------------------------------===//
4171
4172std::pair<unsigned, const TargetRegisterClass *>
4173SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00004174 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00004175 MVT VT) const {
Matt Arsenault742deb22016-11-18 04:42:57 +00004176 if (!isTypeLegal(VT))
4177 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004178
4179 if (Constraint.size() == 1) {
4180 switch (Constraint[0]) {
4181 case 's':
4182 case 'r':
4183 switch (VT.getSizeInBits()) {
4184 default:
4185 return std::make_pair(0U, nullptr);
4186 case 32:
Marek Olsak79c05872016-11-25 17:37:09 +00004187 return std::make_pair(0U, &AMDGPU::SReg_32_XM0RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004188 case 64:
4189 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
4190 case 128:
4191 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
4192 case 256:
4193 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
4194 }
4195
4196 case 'v':
4197 switch (VT.getSizeInBits()) {
4198 default:
4199 return std::make_pair(0U, nullptr);
4200 case 32:
4201 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
4202 case 64:
4203 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
4204 case 96:
4205 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
4206 case 128:
4207 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
4208 case 256:
4209 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
4210 case 512:
4211 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
4212 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00004213 }
4214 }
4215
4216 if (Constraint.size() > 1) {
4217 const TargetRegisterClass *RC = nullptr;
4218 if (Constraint[1] == 'v') {
4219 RC = &AMDGPU::VGPR_32RegClass;
4220 } else if (Constraint[1] == 's') {
4221 RC = &AMDGPU::SGPR_32RegClass;
4222 }
4223
4224 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00004225 uint32_t Idx;
4226 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
4227 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00004228 return std::make_pair(RC->getRegister(Idx), RC);
4229 }
4230 }
4231 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
4232}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004233
4234SITargetLowering::ConstraintType
4235SITargetLowering::getConstraintType(StringRef Constraint) const {
4236 if (Constraint.size() == 1) {
4237 switch (Constraint[0]) {
4238 default: break;
4239 case 's':
4240 case 'v':
4241 return C_RegisterClass;
4242 }
4243 }
4244 return TargetLowering::getConstraintType(Constraint);
4245}