blob: 1f90505ca1a47d6626d64cea3fde3fe37d033ce3 [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
Tom Stellard334b29c2014-04-17 21:00:09 +000062 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
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()) {
Tom Stellard115a6152016-11-10 16:02:37 +000082 addRegisterClass(MVT::i16, &AMDGPU::SReg_32RegClass);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +000083 addRegisterClass(MVT::f16, &AMDGPU::SReg_32RegClass);
84 }
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 Zhuravlyovf86e4b72016-11-13 07:01:11 +0000273 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Custom);
274 setOperationAction(ISD::FP_TO_UINT, MVT::i16, Custom);
275 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Custom);
276 setOperationAction(ISD::UINT_TO_FP, MVT::i16, Custom);
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.
288 setOperationAction(ISD::FCOS, MVT::f16, Promote);
289 setOperationAction(ISD::FSIN, MVT::f16, Promote);
290
291 // F16 - VOP2 Actions.
Konstantin Zhuravlyov662e01d2016-11-17 03:49:01 +0000292 setOperationAction(ISD::BR_CC, MVT::f16, Expand);
Konstantin Zhuravlyov2a87a422016-11-16 03:16:26 +0000293 setOperationAction(ISD::SELECT_CC, MVT::f16, Expand);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000294 setOperationAction(ISD::FMAXNUM, MVT::f16, Legal);
295 setOperationAction(ISD::FMINNUM, MVT::f16, Legal);
296 setOperationAction(ISD::FDIV, MVT::f16, Promote);
297
298 // F16 - VOP3 Actions.
299 setOperationAction(ISD::FMA, MVT::f16, Legal);
300 if (!Subtarget->hasFP16Denormals())
301 setOperationAction(ISD::FMAD, MVT::f16, Legal);
Tom Stellard115a6152016-11-10 16:02:37 +0000302 }
303
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000304 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000305 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000306 setTargetDAGCombine(ISD::FMINNUM);
307 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000308 setTargetDAGCombine(ISD::SMIN);
309 setTargetDAGCombine(ISD::SMAX);
310 setTargetDAGCombine(ISD::UMIN);
311 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000312 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000313 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000314 setTargetDAGCombine(ISD::OR);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +0000315 setTargetDAGCombine(ISD::XOR);
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +0000316 setTargetDAGCombine(ISD::SINT_TO_FP);
Matt Arsenault364a6742014-06-11 17:50:44 +0000317 setTargetDAGCombine(ISD::UINT_TO_FP);
Matt Arsenault9cd90712016-04-14 01:42:16 +0000318 setTargetDAGCombine(ISD::FCANONICALIZE);
Matt Arsenault364a6742014-06-11 17:50:44 +0000319
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000320 // All memory operations. Some folding on the pointer operand is done to help
321 // matching the constant offsets in the addressing modes.
322 setTargetDAGCombine(ISD::LOAD);
323 setTargetDAGCombine(ISD::STORE);
324 setTargetDAGCombine(ISD::ATOMIC_LOAD);
325 setTargetDAGCombine(ISD::ATOMIC_STORE);
326 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
327 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
328 setTargetDAGCombine(ISD::ATOMIC_SWAP);
329 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
330 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
331 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
332 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
333 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
334 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
335 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
336 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
337 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
338 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
339
Christian Konigeecebd02013-03-26 14:04:02 +0000340 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000341}
342
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000343const SISubtarget *SITargetLowering::getSubtarget() const {
344 return static_cast<const SISubtarget *>(Subtarget);
345}
346
Tom Stellard0125f2a2013-06-25 02:39:35 +0000347//===----------------------------------------------------------------------===//
348// TargetLowering queries
349//===----------------------------------------------------------------------===//
350
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000351bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
352 const CallInst &CI,
353 unsigned IntrID) const {
354 switch (IntrID) {
355 case Intrinsic::amdgcn_atomic_inc:
356 case Intrinsic::amdgcn_atomic_dec:
357 Info.opc = ISD::INTRINSIC_W_CHAIN;
358 Info.memVT = MVT::getVT(CI.getType());
359 Info.ptrVal = CI.getOperand(0);
360 Info.align = 0;
361 Info.vol = false;
362 Info.readMem = true;
363 Info.writeMem = true;
364 return true;
365 default:
366 return false;
367 }
368}
369
Matt Arsenaulte306a322014-10-21 16:25:08 +0000370bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
371 EVT) const {
372 // SI has some legal vector types, but no legal vector operations. Say no
373 // shuffles are legal in order to prefer scalarizing some vector operations.
374 return false;
375}
376
Tom Stellard70580f82015-07-20 14:28:41 +0000377bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
378 // Flat instructions do not have offsets, and only have the register
379 // address.
380 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
381}
382
Matt Arsenault711b3902015-08-07 20:18:34 +0000383bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
384 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
385 // additionally can do r + r + i with addr64. 32-bit has more addressing
386 // mode options. Depending on the resource constant, it can also do
387 // (i64 r0) + (i32 r1) * (i14 i).
388 //
389 // Private arrays end up using a scratch buffer most of the time, so also
390 // assume those use MUBUF instructions. Scratch loads / stores are currently
391 // implemented as mubuf instructions with offen bit set, so slightly
392 // different than the normal addr64.
393 if (!isUInt<12>(AM.BaseOffs))
394 return false;
395
396 // FIXME: Since we can split immediate into soffset and immediate offset,
397 // would it make sense to allow any immediate?
398
399 switch (AM.Scale) {
400 case 0: // r + i or just i, depending on HasBaseReg.
401 return true;
402 case 1:
403 return true; // We have r + r or r + i.
404 case 2:
405 if (AM.HasBaseReg) {
406 // Reject 2 * r + r.
407 return false;
408 }
409
410 // Allow 2 * r as r + r
411 // Or 2 * r + i is allowed as r + r + i.
412 return true;
413 default: // Don't allow n * r
414 return false;
415 }
416}
417
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000418bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
419 const AddrMode &AM, Type *Ty,
420 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000421 // No global is ever allowed as a base.
422 if (AM.BaseGV)
423 return false;
424
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000425 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000426 case AMDGPUAS::GLOBAL_ADDRESS: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000427 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS) {
Tom Stellard70580f82015-07-20 14:28:41 +0000428 // Assume the we will use FLAT for all global memory accesses
429 // on VI.
430 // FIXME: This assumption is currently wrong. On VI we still use
431 // MUBUF instructions for the r + i addressing mode. As currently
432 // implemented, the MUBUF instructions only work on buffer < 4GB.
433 // It may be possible to support > 4GB buffers with MUBUF instructions,
434 // by setting the stride value in the resource descriptor which would
435 // increase the size limit to (stride * 4GB). However, this is risky,
436 // because it has never been validated.
437 return isLegalFlatAddressingMode(AM);
438 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000439
Matt Arsenault711b3902015-08-07 20:18:34 +0000440 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000441 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000442 case AMDGPUAS::CONSTANT_ADDRESS: {
443 // If the offset isn't a multiple of 4, it probably isn't going to be
444 // correctly aligned.
Matt Arsenault3cc1e002016-08-13 01:43:51 +0000445 // FIXME: Can we get the real alignment here?
Matt Arsenault711b3902015-08-07 20:18:34 +0000446 if (AM.BaseOffs % 4 != 0)
447 return isLegalMUBUFAddressingMode(AM);
448
449 // There are no SMRD extloads, so if we have to do a small type access we
450 // will use a MUBUF load.
451 // FIXME?: We also need to do this if unaligned, but we don't know the
452 // alignment here.
453 if (DL.getTypeStoreSize(Ty) < 4)
454 return isLegalMUBUFAddressingMode(AM);
455
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000456 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000457 // SMRD instructions have an 8-bit, dword offset on SI.
458 if (!isUInt<8>(AM.BaseOffs / 4))
459 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000460 } else if (Subtarget->getGeneration() == SISubtarget::SEA_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000461 // On CI+, this can also be a 32-bit literal constant offset. If it fits
462 // in 8-bits, it can use a smaller encoding.
463 if (!isUInt<32>(AM.BaseOffs / 4))
464 return false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000465 } else if (Subtarget->getGeneration() == SISubtarget::VOLCANIC_ISLANDS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000466 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
467 if (!isUInt<20>(AM.BaseOffs))
468 return false;
469 } else
470 llvm_unreachable("unhandled generation");
471
472 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
473 return true;
474
475 if (AM.Scale == 1 && AM.HasBaseReg)
476 return true;
477
478 return false;
479 }
480
481 case AMDGPUAS::PRIVATE_ADDRESS:
Matt Arsenault711b3902015-08-07 20:18:34 +0000482 return isLegalMUBUFAddressingMode(AM);
483
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000484 case AMDGPUAS::LOCAL_ADDRESS:
485 case AMDGPUAS::REGION_ADDRESS: {
486 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
487 // field.
488 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
489 // an 8-bit dword offset but we don't know the alignment here.
490 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000491 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000492
493 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
494 return true;
495
496 if (AM.Scale == 1 && AM.HasBaseReg)
497 return true;
498
Matt Arsenault5015a892014-08-15 17:17:07 +0000499 return false;
500 }
Tom Stellard70580f82015-07-20 14:28:41 +0000501 case AMDGPUAS::FLAT_ADDRESS:
Matt Arsenault7d1b6c82016-04-29 06:25:10 +0000502 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
503 // For an unknown address space, this usually means that this is for some
504 // reason being used for pure arithmetic, and not based on some addressing
505 // computation. We don't have instructions that compute pointers with any
506 // addressing modes, so treat them as having no offset like flat
507 // instructions.
Tom Stellard70580f82015-07-20 14:28:41 +0000508 return isLegalFlatAddressingMode(AM);
509
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000510 default:
511 llvm_unreachable("unhandled address space");
512 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000513}
514
Matt Arsenaulte6986632015-01-14 01:35:22 +0000515bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000516 unsigned AddrSpace,
517 unsigned Align,
518 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000519 if (IsFast)
520 *IsFast = false;
521
Matt Arsenault1018c892014-04-24 17:08:26 +0000522 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
523 // which isn't a simple VT.
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000524 // Until MVT is extended to handle this, simply check for the size and
525 // rely on the condition below: allow accesses if the size is a multiple of 4.
526 if (VT == MVT::Other || (VT != MVT::Other && VT.getSizeInBits() > 1024 &&
527 VT.getStoreSize() > 16)) {
Tom Stellard81d871d2013-11-13 23:36:50 +0000528 return false;
Alina Sbirlea6f937b12016-08-04 16:38:44 +0000529 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000530
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000531 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS ||
532 AddrSpace == AMDGPUAS::REGION_ADDRESS) {
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000533 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
534 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
535 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000536 bool AlignedBy4 = (Align % 4 == 0);
537 if (IsFast)
538 *IsFast = AlignedBy4;
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000539
Sanjay Patelce74db92015-09-03 15:03:19 +0000540 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000541 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000542
Tom Stellard64a9d082016-10-14 18:10:39 +0000543 // FIXME: We have to be conservative here and assume that flat operations
544 // will access scratch. If we had access to the IR function, then we
545 // could determine if any private memory was used in the function.
546 if (!Subtarget->hasUnalignedScratchAccess() &&
547 (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS ||
548 AddrSpace == AMDGPUAS::FLAT_ADDRESS)) {
549 return false;
550 }
551
Matt Arsenault7f681ac2016-07-01 23:03:44 +0000552 if (Subtarget->hasUnalignedBufferAccess()) {
553 // If we have an uniform constant load, it still requires using a slow
554 // buffer instruction if unaligned.
555 if (IsFast) {
556 *IsFast = (AddrSpace == AMDGPUAS::CONSTANT_ADDRESS) ?
557 (Align % 4 == 0) : true;
558 }
559
560 return true;
561 }
562
Tom Stellard33e64c62015-02-04 20:49:52 +0000563 // Smaller than dword value must be aligned.
Tom Stellard33e64c62015-02-04 20:49:52 +0000564 if (VT.bitsLT(MVT::i32))
565 return false;
566
Matt Arsenault1018c892014-04-24 17:08:26 +0000567 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
568 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000569 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000570 if (IsFast)
571 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000572
573 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000574}
575
Matt Arsenault46645fa2014-07-28 17:49:26 +0000576EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
577 unsigned SrcAlign, bool IsMemset,
578 bool ZeroMemset,
579 bool MemcpyStrSrc,
580 MachineFunction &MF) const {
581 // FIXME: Should account for address space here.
582
583 // The default fallback uses the private pointer size as a guess for a type to
584 // use. Make sure we switch these to 64-bit accesses.
585
586 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
587 return MVT::v4i32;
588
589 if (Size >= 8 && DstAlign >= 4)
590 return MVT::v2i32;
591
592 // Use the default.
593 return MVT::Other;
594}
595
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000596static bool isFlatGlobalAddrSpace(unsigned AS) {
597 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
598 AS == AMDGPUAS::FLAT_ADDRESS ||
599 AS == AMDGPUAS::CONSTANT_ADDRESS;
600}
601
602bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
603 unsigned DestAS) const {
Matt Arsenault37fefd62016-06-10 02:18:02 +0000604 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000605}
606
Tom Stellarda6f24c62015-12-15 20:55:55 +0000607bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
608 const MemSDNode *MemNode = cast<MemSDNode>(N);
609 const Value *Ptr = MemNode->getMemOperand()->getValue();
610
611 // UndefValue means this is a load of a kernel input. These are uniform.
Tom Stellard418beb72016-07-13 14:23:33 +0000612 // Sometimes LDS instructions have constant pointers.
613 // If Ptr is null, then that means this mem operand contains a
614 // PseudoSourceValue like GOT.
615 if (!Ptr || isa<UndefValue>(Ptr) || isa<Argument>(Ptr) ||
616 isa<Constant>(Ptr) || isa<GlobalValue>(Ptr))
Tom Stellarda6f24c62015-12-15 20:55:55 +0000617 return true;
618
Tom Stellard418beb72016-07-13 14:23:33 +0000619 const Instruction *I = dyn_cast<Instruction>(Ptr);
Tom Stellarda6f24c62015-12-15 20:55:55 +0000620 return I && I->getMetadata("amdgpu.uniform");
621}
622
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000623TargetLoweringBase::LegalizeTypeAction
624SITargetLowering::getPreferredVectorAction(EVT VT) const {
625 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
626 return TypeSplitVector;
627
628 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000629}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000630
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000631bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
632 Type *Ty) const {
Matt Arsenault749035b2016-07-30 01:40:36 +0000633 // FIXME: Could be smarter if called for vector constants.
634 return true;
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000635}
636
Tom Stellard2e045bb2016-01-20 00:13:22 +0000637bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
638
Konstantin Zhuravlyove14df4b2016-09-28 20:05:39 +0000639 // i16 is not desirable unless it is a load or a store.
640 if (VT == MVT::i16 && Op != ISD::LOAD && Op != ISD::STORE)
641 return false;
642
Tom Stellard2e045bb2016-01-20 00:13:22 +0000643 // SimplifySetCC uses this function to determine whether or not it should
644 // create setcc with i1 operands. We don't have instructions for i1 setcc.
645 if (VT == MVT::i1 && Op == ISD::SETCC)
646 return false;
647
648 return TargetLowering::isTypeDesirableForOp(Op, VT);
649}
650
Jan Veselyfea814d2016-06-21 20:46:20 +0000651SDValue SITargetLowering::LowerParameterPtr(SelectionDAG &DAG,
652 const SDLoc &SL, SDValue Chain,
653 unsigned Offset) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000654 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000655 MachineFunction &MF = DAG.getMachineFunction();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000656 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaultac234b62015-11-30 21:15:57 +0000657 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000658
Matt Arsenault86033ca2014-07-28 17:31:39 +0000659 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000660 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000661 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
662 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
Jan Veselyfea814d2016-06-21 20:46:20 +0000663 return DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
664 DAG.getConstant(Offset, SL, PtrVT));
665}
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000666
Jan Veselyfea814d2016-06-21 20:46:20 +0000667SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
668 const SDLoc &SL, SDValue Chain,
669 unsigned Offset, bool Signed) const {
670 const DataLayout &DL = DAG.getDataLayout();
Tom Stellard083f1622016-10-17 16:56:19 +0000671 Type *Ty = MemVT.getTypeForEVT(*DAG.getContext());
Jan Veselyfea814d2016-06-21 20:46:20 +0000672 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000673 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
674
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000675 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000676
Jan Veselyfea814d2016-06-21 20:46:20 +0000677 SDValue Ptr = LowerParameterPtr(DAG, SL, Chain, Offset);
Tom Stellardbc6c5232016-10-17 16:21:45 +0000678 SDValue Load = DAG.getLoad(MemVT, SL, Chain, Ptr, PtrInfo, Align,
679 MachineMemOperand::MONonTemporal |
680 MachineMemOperand::MODereferenceable |
681 MachineMemOperand::MOInvariant);
682
683 SDValue Val;
684 if (MemVT.isFloatingPoint())
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +0000685 Val = getFPExtOrFPTrunc(DAG, Load, SL, VT);
Tom Stellardbc6c5232016-10-17 16:21:45 +0000686 else if (Signed)
687 Val = DAG.getSExtOrTrunc(Load, SL, VT);
688 else
689 Val = DAG.getZExtOrTrunc(Load, SL, VT);
690
691 SDValue Ops[] = {
692 Val,
693 Load.getValue(1)
694 };
695
696 return DAG.getMergeValues(Ops, SL);
Tom Stellard94593ee2013-06-03 17:40:18 +0000697}
698
Christian Konig2c8f6d52013-03-07 09:03:52 +0000699SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000700 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000701 const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
702 SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000703 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000704
705 MachineFunction &MF = DAG.getMachineFunction();
706 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000707 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000708 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000709
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000710 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) {
Matt Arsenaultd48da142015-11-02 23:23:02 +0000711 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000712 DiagnosticInfoUnsupported NoGraphicsHSA(
713 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000714 DAG.getContext()->diagnose(NoGraphicsHSA);
Diana Picus81bc3172016-05-26 15:24:55 +0000715 return DAG.getEntryNode();
Matt Arsenaultd48da142015-11-02 23:23:02 +0000716 }
717
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000718 // Create stack objects that are used for emitting debugger prologue if
719 // "amdgpu-debugger-emit-prologue" attribute was specified.
720 if (ST.debuggerEmitPrologue())
721 createDebuggerPrologueStackObjects(MF);
722
Christian Konig2c8f6d52013-03-07 09:03:52 +0000723 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000724 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000725
726 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000727 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000728
729 // First check if it's a PS input addr
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000730 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000731 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000732
Marek Olsakfccabaf2016-01-13 11:45:36 +0000733 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000734 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000735 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000736 ++PSInputNum;
737 continue;
738 }
739
Marek Olsakfccabaf2016-01-13 11:45:36 +0000740 Info->markPSInputAllocated(PSInputNum);
741 if (Arg.Used)
742 Info->PSInputEna |= 1 << PSInputNum;
743
744 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000745 }
746
Matt Arsenault539ca882016-05-05 20:27:02 +0000747 if (AMDGPU::isShader(CallConv)) {
748 // Second split vertices into their elements
749 if (Arg.VT.isVector()) {
750 ISD::InputArg NewArg = Arg;
751 NewArg.Flags.setSplit();
752 NewArg.VT = Arg.VT.getVectorElementType();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000753
Matt Arsenault539ca882016-05-05 20:27:02 +0000754 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
755 // three or five element vertex only needs three or five registers,
756 // NOT four or eight.
757 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
758 unsigned NumElements = ParamType->getVectorNumElements();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000759
Matt Arsenault539ca882016-05-05 20:27:02 +0000760 for (unsigned j = 0; j != NumElements; ++j) {
761 Splits.push_back(NewArg);
762 NewArg.PartOffset += NewArg.VT.getStoreSize();
763 }
764 } else {
765 Splits.push_back(Arg);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000766 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000767 }
768 }
769
770 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000771 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
772 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000773
Christian Konig99ee0f42013-03-07 09:04:14 +0000774 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000775 //
776 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
777 // PSInputAddr, the user wants to enable some bits after the compilation
778 // based on run-time states. Since we can't know what the final PSInputEna
779 // will look like, so we shouldn't do anything here and the user should take
780 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000781 //
782 // Otherwise, the following restrictions apply:
783 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
784 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
785 // enabled too.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000786 if (CallConv == CallingConv::AMDGPU_PS &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000787 ((Info->getPSInputAddr() & 0x7F) == 0 ||
NAKAMURA Takumife1202c2016-06-20 00:37:41 +0000788 ((Info->getPSInputAddr() & 0xF) == 0 && Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000789 CCInfo.AllocateReg(AMDGPU::VGPR0);
790 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000791 Info->markPSInputAllocated(0);
792 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000793 }
794
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000795 if (!AMDGPU::isShader(CallConv)) {
Tom Stellardf110f8f2016-04-14 16:27:03 +0000796 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
797 } else {
798 assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() &&
799 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() &&
800 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
801 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
802 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
803 !Info->hasWorkItemIDZ());
Tom Stellardaf775432013-10-23 00:44:32 +0000804 }
805
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000806 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
807 if (Info->hasPrivateSegmentBuffer()) {
808 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
809 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
810 CCInfo.AllocateReg(PrivateSegmentBufferReg);
811 }
812
813 if (Info->hasDispatchPtr()) {
814 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
815 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
816 CCInfo.AllocateReg(DispatchPtrReg);
817 }
818
Matt Arsenault48ab5262016-04-25 19:27:18 +0000819 if (Info->hasQueuePtr()) {
820 unsigned QueuePtrReg = Info->addQueuePtr(*TRI);
821 MF.addLiveIn(QueuePtrReg, &AMDGPU::SReg_64RegClass);
822 CCInfo.AllocateReg(QueuePtrReg);
823 }
824
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000825 if (Info->hasKernargSegmentPtr()) {
826 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
827 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
828 CCInfo.AllocateReg(InputPtrReg);
829 }
830
Matt Arsenault8d718dc2016-07-22 17:01:30 +0000831 if (Info->hasDispatchID()) {
832 unsigned DispatchIDReg = Info->addDispatchID(*TRI);
833 MF.addLiveIn(DispatchIDReg, &AMDGPU::SReg_64RegClass);
834 CCInfo.AllocateReg(DispatchIDReg);
835 }
836
Matt Arsenault296b8492016-02-12 06:31:30 +0000837 if (Info->hasFlatScratchInit()) {
838 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
839 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass);
840 CCInfo.AllocateReg(FlatScratchInitReg);
841 }
842
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000843 if (!AMDGPU::isShader(CallConv))
844 analyzeFormalArgumentsCompute(CCInfo, Ins);
845 else
846 AnalyzeFormalArguments(CCInfo, Splits);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000847
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000848 SmallVector<SDValue, 16> Chains;
849
Christian Konig2c8f6d52013-03-07 09:03:52 +0000850 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
851
Christian Konigb7be72d2013-05-17 09:46:48 +0000852 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000853 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000854 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000855 continue;
856 }
857
Christian Konig2c8f6d52013-03-07 09:03:52 +0000858 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000859 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000860
861 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000862 VT = Ins[i].VT;
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000863 EVT MemVT = VA.getLocVT();
Tom Stellardb5798b02015-06-26 21:15:03 +0000864 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
865 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000866 // The first 36 bytes of the input buffer contains information about
867 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000868 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000869 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000870 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000871
Craig Toppere3dcce92015-08-01 22:20:21 +0000872 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000873 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000874 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Tom Stellardca7ecf32014-08-22 18:49:31 +0000875 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
876 // On SI local pointers are just offsets into LDS, so they are always
877 // less than 16-bits. On CI and newer they could potentially be
878 // real pointers, so we can't guarantee their size.
879 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
880 DAG.getValueType(MVT::i16));
881 }
882
Tom Stellarded882c22013-06-03 17:40:11 +0000883 InVals.push_back(Arg);
Matt Arsenault52ef4012016-07-26 16:45:58 +0000884 Info->setABIArgOffset(Offset + MemVT.getStoreSize());
Tom Stellarded882c22013-06-03 17:40:11 +0000885 continue;
886 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000887 assert(VA.isRegLoc() && "Parameter must be in a register!");
888
889 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000890
891 if (VT == MVT::i64) {
892 // For now assume it is a pointer
893 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
894 &AMDGPU::SReg_64RegClass);
895 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000896 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
897 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000898 continue;
899 }
900
901 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
902
903 Reg = MF.addLiveIn(Reg, RC);
904 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
905
Christian Konig2c8f6d52013-03-07 09:03:52 +0000906 if (Arg.VT.isVector()) {
907
908 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000909 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000910 unsigned NumElements = ParamType->getVectorNumElements();
911
912 SmallVector<SDValue, 4> Regs;
913 Regs.push_back(Val);
914 for (unsigned j = 1; j != NumElements; ++j) {
915 Reg = ArgLocs[ArgIdx++].getLocReg();
916 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000917
918 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
919 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000920 }
921
922 // Fill up the missing vector elements
923 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000924 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000925
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000926 InVals.push_back(DAG.getBuildVector(Arg.VT, DL, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000927 continue;
928 }
929
930 InVals.push_back(Val);
931 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000932
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000933 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
934 // these from the dispatch pointer.
935
936 // Start adding system SGPRs.
937 if (Info->hasWorkGroupIDX()) {
938 unsigned Reg = Info->addWorkGroupIDX();
939 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
940 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000941 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000942
943 if (Info->hasWorkGroupIDY()) {
944 unsigned Reg = Info->addWorkGroupIDY();
945 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
946 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000947 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000948
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000949 if (Info->hasWorkGroupIDZ()) {
950 unsigned Reg = Info->addWorkGroupIDZ();
951 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
952 CCInfo.AllocateReg(Reg);
953 }
954
955 if (Info->hasWorkGroupInfo()) {
956 unsigned Reg = Info->addWorkGroupInfo();
957 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
958 CCInfo.AllocateReg(Reg);
959 }
960
961 if (Info->hasPrivateSegmentWaveByteOffset()) {
962 // Scratch wave offset passed in system SGPR.
Tom Stellardf110f8f2016-04-14 16:27:03 +0000963 unsigned PrivateSegmentWaveByteOffsetReg;
964
965 if (AMDGPU::isShader(CallConv)) {
966 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
967 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
968 } else
969 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset();
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000970
971 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
972 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
973 }
974
975 // Now that we've figured out where the scratch register inputs are, see if
976 // should reserve the arguments and use them directly.
Matthias Braun941a7052016-07-28 18:40:00 +0000977 bool HasStackObjects = MF.getFrameInfo().hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +0000978 // Record that we know we have non-spill stack objects so we don't need to
979 // check all stack objects later.
980 if (HasStackObjects)
981 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000982
Matt Arsenault253640e2016-10-13 13:10:00 +0000983 // Everything live out of a block is spilled with fast regalloc, so it's
984 // almost certain that spilling will be required.
985 if (getTargetMachine().getOptLevel() == CodeGenOpt::None)
986 HasStackObjects = true;
987
Tom Stellard0b76fc4c2016-09-16 21:34:26 +0000988 if (ST.isAmdCodeObjectV2()) {
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000989 if (HasStackObjects) {
990 // If we have stack objects, we unquestionably need the private buffer
Tom Stellard0b76fc4c2016-09-16 21:34:26 +0000991 // resource. For the Code Object V2 ABI, this will be the first 4 user
992 // SGPR inputs. We can reserve those and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000993
994 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
995 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
996 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
997
998 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
999 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
1000 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
1001 } else {
1002 unsigned ReservedBufferReg
1003 = TRI->reservedPrivateSegmentBufferReg(MF);
1004 unsigned ReservedOffsetReg
1005 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
1006
1007 // We tentatively reserve the last registers (skipping the last two
1008 // which may contain VCC). After register allocation, we'll replace
1009 // these with the ones immediately after those which were really
1010 // allocated. In the prologue copies will be inserted from the argument
1011 // to these reserved registers.
1012 Info->setScratchRSrcReg(ReservedBufferReg);
1013 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
1014 }
1015 } else {
1016 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
1017
1018 // Without HSA, relocations are used for the scratch pointer and the
1019 // buffer resource setup is always inserted in the prologue. Scratch wave
1020 // offset is still in an input SGPR.
1021 Info->setScratchRSrcReg(ReservedBufferReg);
1022
1023 if (HasStackObjects) {
1024 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
1025 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
1026 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
1027 } else {
1028 unsigned ReservedOffsetReg
1029 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
1030 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
1031 }
1032 }
1033
1034 if (Info->hasWorkItemIDX()) {
1035 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
1036 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1037 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +00001038 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001039
1040 if (Info->hasWorkItemIDY()) {
1041 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
1042 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1043 CCInfo.AllocateReg(Reg);
1044 }
1045
1046 if (Info->hasWorkItemIDZ()) {
1047 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
1048 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
1049 CCInfo.AllocateReg(Reg);
1050 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +00001051
Matt Arsenaultcf13d182015-07-10 22:51:36 +00001052 if (Chains.empty())
1053 return Chain;
1054
1055 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +00001056}
1057
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001058SDValue
1059SITargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
1060 bool isVarArg,
1061 const SmallVectorImpl<ISD::OutputArg> &Outs,
1062 const SmallVectorImpl<SDValue> &OutVals,
1063 const SDLoc &DL, SelectionDAG &DAG) const {
Marek Olsak8a0f3352016-01-13 17:23:04 +00001064 MachineFunction &MF = DAG.getMachineFunction();
1065 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1066
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001067 if (!AMDGPU::isShader(CallConv))
Marek Olsak8a0f3352016-01-13 17:23:04 +00001068 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
1069 OutVals, DL, DAG);
1070
Marek Olsak8e9cc632016-01-13 17:23:09 +00001071 Info->setIfReturnsVoid(Outs.size() == 0);
1072
Marek Olsak8a0f3352016-01-13 17:23:04 +00001073 SmallVector<ISD::OutputArg, 48> Splits;
1074 SmallVector<SDValue, 48> SplitVals;
1075
1076 // Split vectors into their elements.
1077 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
1078 const ISD::OutputArg &Out = Outs[i];
1079
1080 if (Out.VT.isVector()) {
1081 MVT VT = Out.VT.getVectorElementType();
1082 ISD::OutputArg NewOut = Out;
1083 NewOut.Flags.setSplit();
1084 NewOut.VT = VT;
1085
1086 // We want the original number of vector elements here, e.g.
1087 // three or five, not four or eight.
1088 unsigned NumElements = Out.ArgVT.getVectorNumElements();
1089
1090 for (unsigned j = 0; j != NumElements; ++j) {
1091 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
1092 DAG.getConstant(j, DL, MVT::i32));
1093 SplitVals.push_back(Elem);
1094 Splits.push_back(NewOut);
1095 NewOut.PartOffset += NewOut.VT.getStoreSize();
1096 }
1097 } else {
1098 SplitVals.push_back(OutVals[i]);
1099 Splits.push_back(Out);
1100 }
1101 }
1102
1103 // CCValAssign - represent the assignment of the return value to a location.
1104 SmallVector<CCValAssign, 48> RVLocs;
1105
1106 // CCState - Info about the registers and stack slots.
1107 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1108 *DAG.getContext());
1109
1110 // Analyze outgoing return values.
1111 AnalyzeReturn(CCInfo, Splits);
1112
1113 SDValue Flag;
1114 SmallVector<SDValue, 48> RetOps;
1115 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1116
1117 // Copy the result values into the output registers.
1118 for (unsigned i = 0, realRVLocIdx = 0;
1119 i != RVLocs.size();
1120 ++i, ++realRVLocIdx) {
1121 CCValAssign &VA = RVLocs[i];
1122 assert(VA.isRegLoc() && "Can only return in registers!");
1123
1124 SDValue Arg = SplitVals[realRVLocIdx];
1125
1126 // Copied from other backends.
1127 switch (VA.getLocInfo()) {
1128 default: llvm_unreachable("Unknown loc info!");
1129 case CCValAssign::Full:
1130 break;
1131 case CCValAssign::BCvt:
1132 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1133 break;
1134 }
1135
1136 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
1137 Flag = Chain.getValue(1);
1138 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1139 }
1140
1141 // Update chain and glue.
1142 RetOps[0] = Chain;
1143 if (Flag.getNode())
1144 RetOps.push_back(Flag);
1145
Matt Arsenault9babdf42016-06-22 20:15:28 +00001146 unsigned Opc = Info->returnsVoid() ? AMDGPUISD::ENDPGM : AMDGPUISD::RETURN;
1147 return DAG.getNode(Opc, DL, MVT::Other, RetOps);
Marek Olsak8a0f3352016-01-13 17:23:04 +00001148}
1149
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001150unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1151 SelectionDAG &DAG) const {
1152 unsigned Reg = StringSwitch<unsigned>(RegName)
1153 .Case("m0", AMDGPU::M0)
1154 .Case("exec", AMDGPU::EXEC)
1155 .Case("exec_lo", AMDGPU::EXEC_LO)
1156 .Case("exec_hi", AMDGPU::EXEC_HI)
1157 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1158 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1159 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1160 .Default(AMDGPU::NoRegister);
1161
1162 if (Reg == AMDGPU::NoRegister) {
1163 report_fatal_error(Twine("invalid register name \""
1164 + StringRef(RegName) + "\"."));
1165
1166 }
1167
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001168 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS &&
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001169 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1170 report_fatal_error(Twine("invalid register \""
1171 + StringRef(RegName) + "\" for subtarget."));
1172 }
1173
1174 switch (Reg) {
1175 case AMDGPU::M0:
1176 case AMDGPU::EXEC_LO:
1177 case AMDGPU::EXEC_HI:
1178 case AMDGPU::FLAT_SCR_LO:
1179 case AMDGPU::FLAT_SCR_HI:
1180 if (VT.getSizeInBits() == 32)
1181 return Reg;
1182 break;
1183 case AMDGPU::EXEC:
1184 case AMDGPU::FLAT_SCR:
1185 if (VT.getSizeInBits() == 64)
1186 return Reg;
1187 break;
1188 default:
1189 llvm_unreachable("missing register type checking");
1190 }
1191
1192 report_fatal_error(Twine("invalid type for register \""
1193 + StringRef(RegName) + "\"."));
1194}
1195
Matt Arsenault786724a2016-07-12 21:41:32 +00001196// If kill is not the last instruction, split the block so kill is always a
1197// proper terminator.
1198MachineBasicBlock *SITargetLowering::splitKillBlock(MachineInstr &MI,
1199 MachineBasicBlock *BB) const {
1200 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1201
1202 MachineBasicBlock::iterator SplitPoint(&MI);
1203 ++SplitPoint;
1204
1205 if (SplitPoint == BB->end()) {
1206 // Don't bother with a new block.
1207 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1208 return BB;
1209 }
1210
1211 MachineFunction *MF = BB->getParent();
1212 MachineBasicBlock *SplitBB
1213 = MF->CreateMachineBasicBlock(BB->getBasicBlock());
1214
Matt Arsenault786724a2016-07-12 21:41:32 +00001215 MF->insert(++MachineFunction::iterator(BB), SplitBB);
1216 SplitBB->splice(SplitBB->begin(), BB, SplitPoint, BB->end());
1217
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001218 SplitBB->transferSuccessorsAndUpdatePHIs(BB);
Matt Arsenault786724a2016-07-12 21:41:32 +00001219 BB->addSuccessor(SplitBB);
1220
1221 MI.setDesc(TII->get(AMDGPU::SI_KILL_TERMINATOR));
1222 return SplitBB;
1223}
1224
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001225// Do a v_movrels_b32 or v_movreld_b32 for each unique value of \p IdxReg in the
1226// wavefront. If the value is uniform and just happens to be in a VGPR, this
1227// will only do one iteration. In the worst case, this will loop 64 times.
1228//
1229// TODO: Just use v_readlane_b32 if we know the VGPR has a uniform value.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001230static MachineBasicBlock::iterator emitLoadM0FromVGPRLoop(
1231 const SIInstrInfo *TII,
1232 MachineRegisterInfo &MRI,
1233 MachineBasicBlock &OrigBB,
1234 MachineBasicBlock &LoopBB,
1235 const DebugLoc &DL,
1236 const MachineOperand &IdxReg,
1237 unsigned InitReg,
1238 unsigned ResultReg,
1239 unsigned PhiReg,
1240 unsigned InitSaveExecReg,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001241 int Offset,
1242 bool UseGPRIdxMode) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001243 MachineBasicBlock::iterator I = LoopBB.begin();
1244
1245 unsigned PhiExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1246 unsigned NewExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1247 unsigned CurrentIdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1248 unsigned CondReg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1249
1250 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiReg)
1251 .addReg(InitReg)
1252 .addMBB(&OrigBB)
1253 .addReg(ResultReg)
1254 .addMBB(&LoopBB);
1255
1256 BuildMI(LoopBB, I, DL, TII->get(TargetOpcode::PHI), PhiExec)
1257 .addReg(InitSaveExecReg)
1258 .addMBB(&OrigBB)
1259 .addReg(NewExec)
1260 .addMBB(&LoopBB);
1261
1262 // Read the next variant <- also loop target.
1263 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32), CurrentIdxReg)
1264 .addReg(IdxReg.getReg(), getUndefRegState(IdxReg.isUndef()));
1265
1266 // Compare the just read M0 value to all possible Idx values.
1267 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e64), CondReg)
1268 .addReg(CurrentIdxReg)
Matt Arsenaultf0ba86a2016-07-21 09:40:57 +00001269 .addReg(IdxReg.getReg(), 0, IdxReg.getSubReg());
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001270
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001271 if (UseGPRIdxMode) {
1272 unsigned IdxReg;
1273 if (Offset == 0) {
1274 IdxReg = CurrentIdxReg;
1275 } else {
1276 IdxReg = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
1277 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), IdxReg)
1278 .addReg(CurrentIdxReg, RegState::Kill)
1279 .addImm(Offset);
1280 }
1281
1282 MachineInstr *SetIdx =
1283 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_IDX))
1284 .addReg(IdxReg, RegState::Kill);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001285 SetIdx->getOperand(2).setIsUndef();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001286 } else {
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001287 // Move index from VCC into M0
1288 if (Offset == 0) {
1289 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1290 .addReg(CurrentIdxReg, RegState::Kill);
1291 } else {
1292 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1293 .addReg(CurrentIdxReg, RegState::Kill)
1294 .addImm(Offset);
1295 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001296 }
1297
1298 // Update EXEC, save the original EXEC value to VCC.
1299 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), NewExec)
1300 .addReg(CondReg, RegState::Kill);
1301
1302 MRI.setSimpleHint(NewExec, CondReg);
1303
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001304 // Update EXEC, switch all done bits to 0 and all todo bits to 1.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001305 MachineInstr *InsertPt =
1306 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001307 .addReg(AMDGPU::EXEC)
1308 .addReg(NewExec);
1309
1310 // XXX - s_xor_b64 sets scc to 1 if the result is nonzero, so can we use
1311 // s_cbranch_scc0?
1312
1313 // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover.
1314 BuildMI(LoopBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
1315 .addMBB(&LoopBB);
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001316
1317 return InsertPt->getIterator();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001318}
1319
1320// This has slightly sub-optimal regalloc when the source vector is killed by
1321// the read. The register allocator does not understand that the kill is
1322// per-workitem, so is kept alive for the whole loop so we end up not re-using a
1323// subregister from it, using 1 more VGPR than necessary. This was saved when
1324// this was expanded after register allocation.
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001325static MachineBasicBlock::iterator loadM0FromVGPR(const SIInstrInfo *TII,
1326 MachineBasicBlock &MBB,
1327 MachineInstr &MI,
1328 unsigned InitResultReg,
1329 unsigned PhiReg,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001330 int Offset,
1331 bool UseGPRIdxMode) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001332 MachineFunction *MF = MBB.getParent();
1333 MachineRegisterInfo &MRI = MF->getRegInfo();
1334 const DebugLoc &DL = MI.getDebugLoc();
1335 MachineBasicBlock::iterator I(&MI);
1336
1337 unsigned DstReg = MI.getOperand(0).getReg();
1338 unsigned SaveExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1339 unsigned TmpExec = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
1340
1341 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), TmpExec);
1342
1343 // Save the EXEC mask
1344 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_MOV_B64), SaveExec)
1345 .addReg(AMDGPU::EXEC);
1346
1347 // To insert the loop we need to split the block. Move everything after this
1348 // point to a new block, and insert a new empty block between the two.
1349 MachineBasicBlock *LoopBB = MF->CreateMachineBasicBlock();
1350 MachineBasicBlock *RemainderBB = MF->CreateMachineBasicBlock();
1351 MachineFunction::iterator MBBI(MBB);
1352 ++MBBI;
1353
1354 MF->insert(MBBI, LoopBB);
1355 MF->insert(MBBI, RemainderBB);
1356
1357 LoopBB->addSuccessor(LoopBB);
1358 LoopBB->addSuccessor(RemainderBB);
1359
1360 // Move the rest of the block into a new block.
Matt Arsenaultd40ded62016-07-22 17:01:15 +00001361 RemainderBB->transferSuccessorsAndUpdatePHIs(&MBB);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001362 RemainderBB->splice(RemainderBB->begin(), &MBB, I, MBB.end());
1363
1364 MBB.addSuccessor(LoopBB);
1365
1366 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1367
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001368 auto InsPt = emitLoadM0FromVGPRLoop(TII, MRI, MBB, *LoopBB, DL, *Idx,
1369 InitResultReg, DstReg, PhiReg, TmpExec,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001370 Offset, UseGPRIdxMode);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001371
1372 MachineBasicBlock::iterator First = RemainderBB->begin();
1373 BuildMI(*RemainderBB, First, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
1374 .addReg(SaveExec);
1375
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001376 return InsPt;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001377}
1378
1379// Returns subreg index, offset
1380static std::pair<unsigned, int>
1381computeIndirectRegAndOffset(const SIRegisterInfo &TRI,
1382 const TargetRegisterClass *SuperRC,
1383 unsigned VecReg,
1384 int Offset) {
1385 int NumElts = SuperRC->getSize() / 4;
1386
1387 // Skip out of bounds offsets, or else we would end up using an undefined
1388 // register.
1389 if (Offset >= NumElts || Offset < 0)
1390 return std::make_pair(AMDGPU::sub0, Offset);
1391
1392 return std::make_pair(AMDGPU::sub0 + Offset, 0);
1393}
1394
1395// Return true if the index is an SGPR and was set.
1396static bool setM0ToIndexFromSGPR(const SIInstrInfo *TII,
1397 MachineRegisterInfo &MRI,
1398 MachineInstr &MI,
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001399 int Offset,
1400 bool UseGPRIdxMode,
1401 bool IsIndirectSrc) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001402 MachineBasicBlock *MBB = MI.getParent();
1403 const DebugLoc &DL = MI.getDebugLoc();
1404 MachineBasicBlock::iterator I(&MI);
1405
1406 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1407 const TargetRegisterClass *IdxRC = MRI.getRegClass(Idx->getReg());
1408
1409 assert(Idx->getReg() != AMDGPU::NoRegister);
1410
1411 if (!TII->getRegisterInfo().isSGPRClass(IdxRC))
1412 return false;
1413
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001414 if (UseGPRIdxMode) {
1415 unsigned IdxMode = IsIndirectSrc ?
1416 VGPRIndexMode::SRC0_ENABLE : VGPRIndexMode::DST_ENABLE;
1417 if (Offset == 0) {
1418 MachineInstr *SetOn =
1419 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1420 .addOperand(*Idx)
1421 .addImm(IdxMode);
1422
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001423 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001424 } else {
1425 unsigned Tmp = MRI.createVirtualRegister(&AMDGPU::SReg_32_XM0RegClass);
1426 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), Tmp)
1427 .addOperand(*Idx)
1428 .addImm(Offset);
1429 MachineInstr *SetOn =
1430 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1431 .addReg(Tmp, RegState::Kill)
1432 .addImm(IdxMode);
1433
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001434 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001435 }
1436
1437 return true;
1438 }
1439
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001440 if (Offset == 0) {
1441 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1442 .addOperand(*Idx);
1443 } else {
1444 BuildMI(*MBB, I, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
1445 .addOperand(*Idx)
1446 .addImm(Offset);
1447 }
1448
1449 return true;
1450}
1451
1452// Control flow needs to be inserted if indexing with a VGPR.
1453static MachineBasicBlock *emitIndirectSrc(MachineInstr &MI,
1454 MachineBasicBlock &MBB,
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001455 const SISubtarget &ST) {
1456 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001457 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1458 MachineFunction *MF = MBB.getParent();
1459 MachineRegisterInfo &MRI = MF->getRegInfo();
1460
1461 unsigned Dst = MI.getOperand(0).getReg();
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001462 unsigned SrcReg = TII->getNamedOperand(MI, AMDGPU::OpName::src)->getReg();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001463 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1464
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001465 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcReg);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001466
1467 unsigned SubReg;
1468 std::tie(SubReg, Offset)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001469 = computeIndirectRegAndOffset(TRI, VecRC, SrcReg, Offset);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001470
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001471 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode;
1472
1473 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, true)) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001474 MachineBasicBlock::iterator I(&MI);
1475 const DebugLoc &DL = MI.getDebugLoc();
1476
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001477 if (UseGPRIdxMode) {
1478 // TODO: Look at the uses to avoid the copy. This may require rescheduling
1479 // to avoid interfering with other uses, so probably requires a new
1480 // optimization pass.
1481 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001482 .addReg(SrcReg, RegState::Undef, SubReg)
1483 .addReg(SrcReg, RegState::Implicit)
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001484 .addReg(AMDGPU::M0, RegState::Implicit);
1485 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1486 } else {
1487 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001488 .addReg(SrcReg, RegState::Undef, SubReg)
1489 .addReg(SrcReg, RegState::Implicit);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001490 }
1491
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001492 MI.eraseFromParent();
1493
1494 return &MBB;
1495 }
1496
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001497
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001498 const DebugLoc &DL = MI.getDebugLoc();
1499 MachineBasicBlock::iterator I(&MI);
1500
1501 unsigned PhiReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1502 unsigned InitReg = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1503
1504 BuildMI(MBB, I, DL, TII->get(TargetOpcode::IMPLICIT_DEF), InitReg);
1505
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001506 if (UseGPRIdxMode) {
1507 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1508 .addImm(0) // Reset inside loop.
1509 .addImm(VGPRIndexMode::SRC0_ENABLE);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001510 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001511
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001512 // Disable again after the loop.
1513 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1514 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001515
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001516 auto InsPt = loadM0FromVGPR(TII, MBB, MI, InitReg, PhiReg, Offset, UseGPRIdxMode);
1517 MachineBasicBlock *LoopBB = InsPt->getParent();
1518
1519 if (UseGPRIdxMode) {
1520 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_e32), Dst)
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001521 .addReg(SrcReg, RegState::Undef, SubReg)
1522 .addReg(SrcReg, RegState::Implicit)
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001523 .addReg(AMDGPU::M0, RegState::Implicit);
1524 } else {
1525 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOVRELS_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 }
1529
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001530 MI.eraseFromParent();
1531
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001532 return LoopBB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001533}
1534
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001535static unsigned getMOVRELDPseudo(const TargetRegisterClass *VecRC) {
1536 switch (VecRC->getSize()) {
1537 case 4:
1538 return AMDGPU::V_MOVRELD_B32_V1;
1539 case 8:
1540 return AMDGPU::V_MOVRELD_B32_V2;
1541 case 16:
1542 return AMDGPU::V_MOVRELD_B32_V4;
1543 case 32:
1544 return AMDGPU::V_MOVRELD_B32_V8;
1545 case 64:
1546 return AMDGPU::V_MOVRELD_B32_V16;
1547 default:
1548 llvm_unreachable("unsupported size for MOVRELD pseudos");
1549 }
1550}
1551
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001552static MachineBasicBlock *emitIndirectDst(MachineInstr &MI,
1553 MachineBasicBlock &MBB,
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001554 const SISubtarget &ST) {
1555 const SIInstrInfo *TII = ST.getInstrInfo();
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001556 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1557 MachineFunction *MF = MBB.getParent();
1558 MachineRegisterInfo &MRI = MF->getRegInfo();
1559
1560 unsigned Dst = MI.getOperand(0).getReg();
1561 const MachineOperand *SrcVec = TII->getNamedOperand(MI, AMDGPU::OpName::src);
1562 const MachineOperand *Idx = TII->getNamedOperand(MI, AMDGPU::OpName::idx);
1563 const MachineOperand *Val = TII->getNamedOperand(MI, AMDGPU::OpName::val);
1564 int Offset = TII->getNamedOperand(MI, AMDGPU::OpName::offset)->getImm();
1565 const TargetRegisterClass *VecRC = MRI.getRegClass(SrcVec->getReg());
1566
1567 // This can be an immediate, but will be folded later.
1568 assert(Val->getReg());
1569
1570 unsigned SubReg;
1571 std::tie(SubReg, Offset) = computeIndirectRegAndOffset(TRI, VecRC,
1572 SrcVec->getReg(),
1573 Offset);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001574 bool UseGPRIdxMode = ST.hasVGPRIndexMode() && EnableVGPRIndexMode;
1575
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001576 if (Idx->getReg() == AMDGPU::NoRegister) {
1577 MachineBasicBlock::iterator I(&MI);
1578 const DebugLoc &DL = MI.getDebugLoc();
1579
1580 assert(Offset == 0);
1581
1582 BuildMI(MBB, I, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dst)
1583 .addOperand(*SrcVec)
1584 .addOperand(*Val)
1585 .addImm(SubReg);
1586
1587 MI.eraseFromParent();
1588 return &MBB;
1589 }
1590
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001591 if (setM0ToIndexFromSGPR(TII, MRI, MI, Offset, UseGPRIdxMode, false)) {
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001592 MachineBasicBlock::iterator I(&MI);
1593 const DebugLoc &DL = MI.getDebugLoc();
1594
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001595 if (UseGPRIdxMode) {
1596 BuildMI(MBB, I, DL, TII->get(AMDGPU::V_MOV_B32_indirect))
1597 .addReg(SrcVec->getReg(), RegState::Undef, SubReg) // vdst
1598 .addOperand(*Val)
1599 .addReg(Dst, RegState::ImplicitDefine)
1600 .addReg(SrcVec->getReg(), RegState::Implicit)
1601 .addReg(AMDGPU::M0, RegState::Implicit);
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001602
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001603 BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1604 } else {
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001605 const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(VecRC));
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001606
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001607 BuildMI(MBB, I, DL, MovRelDesc)
1608 .addReg(Dst, RegState::Define)
1609 .addReg(SrcVec->getReg())
1610 .addOperand(*Val)
1611 .addImm(SubReg - AMDGPU::sub0);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001612 }
1613
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001614 MI.eraseFromParent();
1615 return &MBB;
1616 }
1617
1618 if (Val->isReg())
1619 MRI.clearKillFlags(Val->getReg());
1620
1621 const DebugLoc &DL = MI.getDebugLoc();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001622
1623 if (UseGPRIdxMode) {
1624 MachineBasicBlock::iterator I(&MI);
1625
1626 MachineInstr *SetOn = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_SET_GPR_IDX_ON))
1627 .addImm(0) // Reset inside loop.
1628 .addImm(VGPRIndexMode::DST_ENABLE);
Matt Arsenaultdac31db2016-10-13 12:45:16 +00001629 SetOn->getOperand(3).setIsUndef();
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001630
1631 // Disable again after the loop.
1632 BuildMI(MBB, std::next(I), DL, TII->get(AMDGPU::S_SET_GPR_IDX_OFF));
1633 }
1634
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001635 unsigned PhiReg = MRI.createVirtualRegister(VecRC);
1636
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001637 auto InsPt = loadM0FromVGPR(TII, MBB, MI, SrcVec->getReg(), PhiReg,
1638 Offset, UseGPRIdxMode);
1639 MachineBasicBlock *LoopBB = InsPt->getParent();
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001640
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001641 if (UseGPRIdxMode) {
1642 BuildMI(*LoopBB, InsPt, DL, TII->get(AMDGPU::V_MOV_B32_indirect))
1643 .addReg(PhiReg, RegState::Undef, SubReg) // vdst
1644 .addOperand(*Val) // src0
1645 .addReg(Dst, RegState::ImplicitDefine)
1646 .addReg(PhiReg, RegState::Implicit)
1647 .addReg(AMDGPU::M0, RegState::Implicit);
1648 } else {
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001649 const MCInstrDesc &MovRelDesc = TII->get(getMOVRELDPseudo(VecRC));
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001650
Nicolai Haehnlea7852092016-10-24 14:56:02 +00001651 BuildMI(*LoopBB, InsPt, DL, MovRelDesc)
1652 .addReg(Dst, RegState::Define)
1653 .addReg(PhiReg)
1654 .addOperand(*Val)
1655 .addImm(SubReg - AMDGPU::sub0);
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001656 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001657
Nicolai Haehnlebd15c322016-10-14 09:03:04 +00001658 MI.eraseFromParent();
1659
Matt Arsenaultd486d3f2016-10-12 18:49:05 +00001660 return LoopBB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001661}
1662
Matt Arsenault786724a2016-07-12 21:41:32 +00001663MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
1664 MachineInstr &MI, MachineBasicBlock *BB) const {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001665 switch (MI.getOpcode()) {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001666 case AMDGPU::SI_INIT_M0: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001667 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001668 BuildMI(*BB, MI.getIterator(), MI.getDebugLoc(),
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001669 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001670 .addOperand(MI.getOperand(0));
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001671 MI.eraseFromParent();
Matt Arsenault20711b72015-02-20 22:10:45 +00001672 return BB;
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001673 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001674 case AMDGPU::GET_GROUPSTATICSIZE: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001675 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1676
Changpeng Fang01f60622016-03-15 17:28:44 +00001677 MachineFunction *MF = BB->getParent();
1678 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001679 DebugLoc DL = MI.getDebugLoc();
Matt Arsenault3c07c812016-07-22 17:01:33 +00001680 BuildMI(*BB, MI, DL, TII->get(AMDGPU::S_MOV_B32))
1681 .addOperand(MI.getOperand(0))
Matt Arsenault52ef4012016-07-26 16:45:58 +00001682 .addImm(MFI->getLDSSize());
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00001683 MI.eraseFromParent();
Changpeng Fang01f60622016-03-15 17:28:44 +00001684 return BB;
1685 }
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001686 case AMDGPU::SI_INDIRECT_SRC_V1:
1687 case AMDGPU::SI_INDIRECT_SRC_V2:
1688 case AMDGPU::SI_INDIRECT_SRC_V4:
1689 case AMDGPU::SI_INDIRECT_SRC_V8:
1690 case AMDGPU::SI_INDIRECT_SRC_V16:
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001691 return emitIndirectSrc(MI, *BB, *getSubtarget());
Matt Arsenaultcb540bc2016-07-19 00:35:03 +00001692 case AMDGPU::SI_INDIRECT_DST_V1:
1693 case AMDGPU::SI_INDIRECT_DST_V2:
1694 case AMDGPU::SI_INDIRECT_DST_V4:
1695 case AMDGPU::SI_INDIRECT_DST_V8:
1696 case AMDGPU::SI_INDIRECT_DST_V16:
Matt Arsenaultdcf0cfc2016-10-04 01:41:05 +00001697 return emitIndirectDst(MI, *BB, *getSubtarget());
Matt Arsenault786724a2016-07-12 21:41:32 +00001698 case AMDGPU::SI_KILL:
1699 return splitKillBlock(MI, BB);
Matt Arsenault22e41792016-08-27 01:00:37 +00001700 case AMDGPU::V_CNDMASK_B64_PSEUDO: {
1701 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
1702 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
1703
1704 unsigned Dst = MI.getOperand(0).getReg();
1705 unsigned Src0 = MI.getOperand(1).getReg();
1706 unsigned Src1 = MI.getOperand(2).getReg();
1707 const DebugLoc &DL = MI.getDebugLoc();
1708 unsigned SrcCond = MI.getOperand(3).getReg();
1709
1710 unsigned DstLo = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1711 unsigned DstHi = MRI.createVirtualRegister(&AMDGPU::VGPR_32RegClass);
1712
1713 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstLo)
1714 .addReg(Src0, 0, AMDGPU::sub0)
1715 .addReg(Src1, 0, AMDGPU::sub0)
1716 .addReg(SrcCond);
1717 BuildMI(*BB, MI, DL, TII->get(AMDGPU::V_CNDMASK_B32_e64), DstHi)
1718 .addReg(Src0, 0, AMDGPU::sub1)
1719 .addReg(Src1, 0, AMDGPU::sub1)
1720 .addReg(SrcCond);
1721
1722 BuildMI(*BB, MI, DL, TII->get(AMDGPU::REG_SEQUENCE), Dst)
1723 .addReg(DstLo)
1724 .addImm(AMDGPU::sub0)
1725 .addReg(DstHi)
1726 .addImm(AMDGPU::sub1);
1727 MI.eraseFromParent();
1728 return BB;
1729 }
Changpeng Fang01f60622016-03-15 17:28:44 +00001730 default:
1731 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001732 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001733}
1734
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001735bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1736 // This currently forces unfolding various combinations of fsub into fma with
1737 // free fneg'd operands. As long as we have fast FMA (controlled by
1738 // isFMAFasterThanFMulAndFAdd), we should perform these.
1739
1740 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1741 // most of these combines appear to be cycle neutral but save on instruction
1742 // count / code size.
1743 return true;
1744}
1745
Mehdi Amini44ede332015-07-09 02:09:04 +00001746EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1747 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001748 if (!VT.isVector()) {
1749 return MVT::i1;
1750 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001751 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001752}
1753
Mehdi Aminieaabc512015-07-09 15:12:23 +00001754MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001755 return MVT::i32;
1756}
1757
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001758// Answering this is somewhat tricky and depends on the specific device which
1759// have different rates for fma or all f64 operations.
1760//
1761// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1762// regardless of which device (although the number of cycles differs between
1763// devices), so it is always profitable for f64.
1764//
1765// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1766// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1767// which we can always do even without fused FP ops since it returns the same
1768// result as the separate operations and since it is always full
1769// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1770// however does not support denormals, so we do report fma as faster if we have
1771// a fast fma device and require denormals.
1772//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001773bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1774 VT = VT.getScalarType();
1775
1776 if (!VT.isSimple())
1777 return false;
1778
1779 switch (VT.getSimpleVT().SimpleTy) {
1780 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001781 // This is as fast on some subtargets. However, we always have full rate f32
1782 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001783 // which we should prefer over fma. We can't use this if we want to support
1784 // denormals, so only report this in these cases.
1785 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001786 case MVT::f64:
1787 return true;
1788 default:
1789 break;
1790 }
1791
1792 return false;
1793}
1794
Tom Stellard75aadc22012-12-11 21:25:42 +00001795//===----------------------------------------------------------------------===//
1796// Custom DAG Lowering Operations
1797//===----------------------------------------------------------------------===//
1798
1799SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1800 switch (Op.getOpcode()) {
1801 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001802 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001803 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001804 SDValue Result = LowerLOAD(Op, DAG);
1805 assert((!Result.getNode() ||
1806 Result.getNode()->getNumValues() == 2) &&
1807 "Load should return a value and a chain");
1808 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001809 }
Tom Stellardaf775432013-10-23 00:44:32 +00001810
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001811 case ISD::FSIN:
1812 case ISD::FCOS:
1813 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001814 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001815 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001816 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001817 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001818 case ISD::GlobalAddress: {
1819 MachineFunction &MF = DAG.getMachineFunction();
1820 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1821 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001822 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001823 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001824 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001825 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Matt Arsenault99c14522016-04-25 19:27:24 +00001826 case ISD::ADDRSPACECAST: return lowerADDRSPACECAST(Op, DAG);
Matt Arsenault0bb294b2016-06-17 22:27:03 +00001827 case ISD::TRAP: return lowerTRAP(Op, DAG);
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001828
1829 case ISD::ConstantFP:
1830 return lowerConstantFP(Op, DAG);
1831 case ISD::FP_TO_SINT:
1832 case ISD::FP_TO_UINT:
1833 return lowerFpToInt(Op, DAG);
1834 case ISD::SINT_TO_FP:
1835 case ISD::UINT_TO_FP:
1836 return lowerIntToFp(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001837 }
1838 return SDValue();
1839}
1840
Tom Stellardf8794352012-12-19 22:10:31 +00001841/// \brief Helper function for LowerBRCOND
1842static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001843
Tom Stellardf8794352012-12-19 22:10:31 +00001844 SDNode *Parent = Value.getNode();
1845 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1846 I != E; ++I) {
1847
1848 if (I.getUse().get() != Value)
1849 continue;
1850
1851 if (I->getOpcode() == Opcode)
1852 return *I;
1853 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001854 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001855}
1856
Tom Stellardbc4497b2016-02-12 23:45:29 +00001857bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault6408c912016-09-16 22:11:18 +00001858 if (Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN) {
1859 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1860 case AMDGPUIntrinsic::amdgcn_if:
1861 case AMDGPUIntrinsic::amdgcn_else:
1862 case AMDGPUIntrinsic::amdgcn_end_cf:
1863 case AMDGPUIntrinsic::amdgcn_loop:
1864 return true;
1865 default:
1866 return false;
1867 }
Tom Stellardbc4497b2016-02-12 23:45:29 +00001868 }
Matt Arsenault6408c912016-09-16 22:11:18 +00001869
1870 if (Intr->getOpcode() == ISD::INTRINSIC_WO_CHAIN) {
1871 switch (cast<ConstantSDNode>(Intr->getOperand(0))->getZExtValue()) {
1872 case AMDGPUIntrinsic::amdgcn_break:
1873 case AMDGPUIntrinsic::amdgcn_if_break:
1874 case AMDGPUIntrinsic::amdgcn_else_break:
1875 return true;
1876 default:
1877 return false;
1878 }
1879 }
1880
1881 return false;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001882}
1883
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001884void SITargetLowering::createDebuggerPrologueStackObjects(
1885 MachineFunction &MF) const {
1886 // Create stack objects that are used for emitting debugger prologue.
1887 //
1888 // Debugger prologue writes work group IDs and work item IDs to scratch memory
1889 // at fixed location in the following format:
1890 // offset 0: work group ID x
1891 // offset 4: work group ID y
1892 // offset 8: work group ID z
1893 // offset 16: work item ID x
1894 // offset 20: work item ID y
1895 // offset 24: work item ID z
1896 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
1897 int ObjectIdx = 0;
1898
1899 // For each dimension:
1900 for (unsigned i = 0; i < 3; ++i) {
1901 // Create fixed stack object for work group ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001902 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001903 Info->setDebuggerWorkGroupIDStackObjectIndex(i, ObjectIdx);
1904 // Create fixed stack object for work item ID.
Matthias Braun941a7052016-07-28 18:40:00 +00001905 ObjectIdx = MF.getFrameInfo().CreateFixedObject(4, i * 4 + 16, true);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001906 Info->setDebuggerWorkItemIDStackObjectIndex(i, ObjectIdx);
1907 }
1908}
1909
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00001910bool SITargetLowering::shouldEmitFixup(const GlobalValue *GV) const {
1911 const Triple &TT = getTargetMachine().getTargetTriple();
1912 return GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS &&
1913 AMDGPU::shouldEmitConstantsToTextSection(TT);
1914}
1915
1916bool SITargetLowering::shouldEmitGOTReloc(const GlobalValue *GV) const {
1917 return (GV->getType()->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS ||
1918 GV->getType()->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) &&
1919 !shouldEmitFixup(GV) &&
1920 !getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV);
1921}
1922
1923bool SITargetLowering::shouldEmitPCReloc(const GlobalValue *GV) const {
1924 return !shouldEmitFixup(GV) && !shouldEmitGOTReloc(GV);
1925}
1926
Tom Stellardf8794352012-12-19 22:10:31 +00001927/// This transforms the control flow intrinsics to get the branch destination as
1928/// last parameter, also switches branch target with BR if the need arise
1929SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1930 SelectionDAG &DAG) const {
1931
Andrew Trickef9de2a2013-05-25 02:42:55 +00001932 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001933
1934 SDNode *Intr = BRCOND.getOperand(1).getNode();
1935 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001936 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001937 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001938
1939 if (Intr->getOpcode() == ISD::SETCC) {
1940 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001941 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001942 Intr = SetCC->getOperand(0).getNode();
1943
1944 } else {
1945 // Get the target from BR if we don't negate the condition
1946 BR = findUser(BRCOND, ISD::BR);
1947 Target = BR->getOperand(1);
1948 }
1949
Matt Arsenault6408c912016-09-16 22:11:18 +00001950 // FIXME: This changes the types of the intrinsics instead of introducing new
1951 // nodes with the correct types.
1952 // e.g. llvm.amdgcn.loop
1953
1954 // eg: i1,ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3
1955 // => t9: ch = llvm.amdgcn.loop t0, TargetConstant:i32<6271>, t3, BasicBlock:ch<bb1 0x7fee5286d088>
1956
Nicolai Haehnleffbd56a2016-05-05 17:36:36 +00001957 if (!isCFIntrinsic(Intr)) {
Tom Stellardbc4497b2016-02-12 23:45:29 +00001958 // This is a uniform branch so we don't need to legalize.
1959 return BRCOND;
1960 }
1961
Matt Arsenault6408c912016-09-16 22:11:18 +00001962 bool HaveChain = Intr->getOpcode() == ISD::INTRINSIC_VOID ||
1963 Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN;
1964
Tom Stellardbc4497b2016-02-12 23:45:29 +00001965 assert(!SetCC ||
1966 (SetCC->getConstantOperandVal(1) == 1 &&
Tom Stellardbc4497b2016-02-12 23:45:29 +00001967 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1968 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001969
Tom Stellardf8794352012-12-19 22:10:31 +00001970 // operands of the new intrinsic call
1971 SmallVector<SDValue, 4> Ops;
Matt Arsenault6408c912016-09-16 22:11:18 +00001972 if (HaveChain)
1973 Ops.push_back(BRCOND.getOperand(0));
1974
1975 Ops.append(Intr->op_begin() + (HaveChain ? 1 : 0), Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001976 Ops.push_back(Target);
1977
Matt Arsenault6408c912016-09-16 22:11:18 +00001978 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
1979
Tom Stellardf8794352012-12-19 22:10:31 +00001980 // build the new intrinsic call
1981 SDNode *Result = DAG.getNode(
1982 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001983 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001984
Matt Arsenault6408c912016-09-16 22:11:18 +00001985 if (!HaveChain) {
1986 SDValue Ops[] = {
1987 SDValue(Result, 0),
1988 BRCOND.getOperand(0)
1989 };
1990
1991 Result = DAG.getMergeValues(Ops, DL).getNode();
1992 }
1993
Tom Stellardf8794352012-12-19 22:10:31 +00001994 if (BR) {
1995 // Give the branch instruction our target
1996 SDValue Ops[] = {
1997 BR->getOperand(0),
1998 BRCOND.getOperand(2)
1999 };
Chandler Carruth356665a2014-08-01 22:09:43 +00002000 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
2001 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
2002 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00002003 }
2004
2005 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
2006
2007 // Copy the intrinsic results to registers
2008 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
2009 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
2010 if (!CopyToReg)
2011 continue;
2012
2013 Chain = DAG.getCopyToReg(
2014 Chain, DL,
2015 CopyToReg->getOperand(1),
2016 SDValue(Result, i - 1),
2017 SDValue());
2018
2019 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
2020 }
2021
2022 // Remove the old intrinsic from the chain
2023 DAG.ReplaceAllUsesOfValueWith(
2024 SDValue(Intr, Intr->getNumValues() - 1),
2025 Intr->getOperand(0));
2026
2027 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00002028}
2029
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002030SDValue SITargetLowering::getFPExtOrFPTrunc(SelectionDAG &DAG,
2031 SDValue Op,
2032 const SDLoc &DL,
2033 EVT VT) const {
2034 return Op.getValueType().bitsLE(VT) ?
2035 DAG.getNode(ISD::FP_EXTEND, DL, VT, Op) :
2036 DAG.getNode(ISD::FTRUNC, DL, VT, Op);
2037}
2038
2039SDValue SITargetLowering::lowerConstantFP(SDValue Op, SelectionDAG &DAG) const {
2040 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(Op)) {
2041 return DAG.getConstant(FP->getValueAPF().bitcastToAPInt().getZExtValue(),
2042 SDLoc(Op), MVT::i32);
2043 }
2044
2045 return SDValue();
2046}
2047
2048SDValue SITargetLowering::lowerFpToInt(SDValue Op, SelectionDAG &DAG) const {
2049 EVT DstVT = Op.getValueType();
2050 EVT SrcVT = Op.getOperand(0).getValueType();
2051 if (DstVT == MVT::i64) {
2052 return Op.getOpcode() == ISD::FP_TO_SINT ?
2053 AMDGPUTargetLowering::LowerFP_TO_SINT(Op, DAG) :
2054 AMDGPUTargetLowering::LowerFP_TO_UINT(Op, DAG);
2055 }
2056
2057 if (SrcVT == MVT::f16)
2058 return Op;
2059
2060 SDLoc DL(Op);
2061 SDValue OrigSrc = Op.getOperand(0);
2062 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, DL);
2063 SDValue FPRoundSrc =
2064 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, OrigSrc, FPRoundFlag);
2065
2066 return DAG.getNode(Op.getOpcode(), DL, DstVT, FPRoundSrc);
2067}
2068
2069SDValue SITargetLowering::lowerIntToFp(SDValue Op, SelectionDAG &DAG) const {
2070 EVT DstVT = Op.getValueType();
2071 EVT SrcVT = Op.getOperand(0).getValueType();
2072 if (SrcVT == MVT::i64) {
2073 return Op.getOpcode() == ISD::SINT_TO_FP ?
2074 AMDGPUTargetLowering::LowerSINT_TO_FP(Op, DAG) :
2075 AMDGPUTargetLowering::LowerUINT_TO_FP(Op, DAG);
2076 }
2077
2078 if (DstVT == MVT::f16)
2079 return Op;
2080
2081 SDLoc DL(Op);
2082 SDValue OrigSrc = Op.getOperand(0);
2083 SDValue SExtOrZExtOrTruncSrc = Op.getOpcode() == ISD::SINT_TO_FP ?
2084 DAG.getSExtOrTrunc(OrigSrc, DL, MVT::i32) :
2085 DAG.getZExtOrTrunc(OrigSrc, DL, MVT::i32);
2086
2087 return DAG.getNode(Op.getOpcode(), DL, DstVT, SExtOrZExtOrTruncSrc);
2088}
2089
Matt Arsenault99c14522016-04-25 19:27:24 +00002090SDValue SITargetLowering::getSegmentAperture(unsigned AS,
2091 SelectionDAG &DAG) const {
2092 SDLoc SL;
2093 MachineFunction &MF = DAG.getMachineFunction();
2094 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00002095 unsigned UserSGPR = Info->getQueuePtrUserSGPR();
2096 assert(UserSGPR != AMDGPU::NoRegister);
2097
Matt Arsenault99c14522016-04-25 19:27:24 +00002098 SDValue QueuePtr = CreateLiveInRegister(
Matt Arsenault3b2e2a52016-06-06 20:03:31 +00002099 DAG, &AMDGPU::SReg_64RegClass, UserSGPR, MVT::i64);
Matt Arsenault99c14522016-04-25 19:27:24 +00002100
2101 // Offset into amd_queue_t for group_segment_aperture_base_hi /
2102 // private_segment_aperture_base_hi.
2103 uint32_t StructOffset = (AS == AMDGPUAS::LOCAL_ADDRESS) ? 0x40 : 0x44;
2104
2105 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, QueuePtr,
2106 DAG.getConstant(StructOffset, SL, MVT::i64));
2107
2108 // TODO: Use custom target PseudoSourceValue.
2109 // TODO: We should use the value from the IR intrinsic call, but it might not
2110 // be available and how do we get it?
2111 Value *V = UndefValue::get(PointerType::get(Type::getInt8Ty(*DAG.getContext()),
2112 AMDGPUAS::CONSTANT_ADDRESS));
2113
2114 MachinePointerInfo PtrInfo(V, StructOffset);
Justin Lebar9c375812016-07-15 18:27:10 +00002115 return DAG.getLoad(MVT::i32, SL, QueuePtr.getValue(1), Ptr, PtrInfo,
2116 MinAlign(64, StructOffset),
Justin Lebaradbf09e2016-09-11 01:38:58 +00002117 MachineMemOperand::MODereferenceable |
2118 MachineMemOperand::MOInvariant);
Matt Arsenault99c14522016-04-25 19:27:24 +00002119}
2120
2121SDValue SITargetLowering::lowerADDRSPACECAST(SDValue Op,
2122 SelectionDAG &DAG) const {
2123 SDLoc SL(Op);
2124 const AddrSpaceCastSDNode *ASC = cast<AddrSpaceCastSDNode>(Op);
2125
2126 SDValue Src = ASC->getOperand(0);
2127
2128 // FIXME: Really support non-0 null pointers.
2129 SDValue SegmentNullPtr = DAG.getConstant(-1, SL, MVT::i32);
2130 SDValue FlatNullPtr = DAG.getConstant(0, SL, MVT::i64);
2131
2132 // flat -> local/private
2133 if (ASC->getSrcAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
2134 if (ASC->getDestAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
2135 ASC->getDestAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
2136 SDValue NonNull = DAG.getSetCC(SL, MVT::i1, Src, FlatNullPtr, ISD::SETNE);
2137 SDValue Ptr = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, Src);
2138
2139 return DAG.getNode(ISD::SELECT, SL, MVT::i32,
2140 NonNull, Ptr, SegmentNullPtr);
2141 }
2142 }
2143
2144 // local/private -> flat
2145 if (ASC->getDestAddressSpace() == AMDGPUAS::FLAT_ADDRESS) {
2146 if (ASC->getSrcAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
2147 ASC->getSrcAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
2148 SDValue NonNull
2149 = DAG.getSetCC(SL, MVT::i1, Src, SegmentNullPtr, ISD::SETNE);
2150
2151 SDValue Aperture = getSegmentAperture(ASC->getSrcAddressSpace(), DAG);
2152 SDValue CvtPtr
2153 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Src, Aperture);
2154
2155 return DAG.getNode(ISD::SELECT, SL, MVT::i64, NonNull,
2156 DAG.getNode(ISD::BITCAST, SL, MVT::i64, CvtPtr),
2157 FlatNullPtr);
2158 }
2159 }
2160
2161 // global <-> flat are no-ops and never emitted.
2162
2163 const MachineFunction &MF = DAG.getMachineFunction();
2164 DiagnosticInfoUnsupported InvalidAddrSpaceCast(
2165 *MF.getFunction(), "invalid addrspacecast", SL.getDebugLoc());
2166 DAG.getContext()->diagnose(InvalidAddrSpaceCast);
2167
2168 return DAG.getUNDEF(ASC->getValueType(0));
2169}
2170
Tom Stellard418beb72016-07-13 14:23:33 +00002171bool
2172SITargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2173 // We can fold offsets for anything that doesn't require a GOT relocation.
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00002174 return (GA->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS ||
2175 GA->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) &&
2176 !shouldEmitGOTReloc(GA->getGlobal());
Tom Stellard418beb72016-07-13 14:23:33 +00002177}
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002178
Tom Stellard418beb72016-07-13 14:23:33 +00002179static SDValue buildPCRelGlobalAddress(SelectionDAG &DAG, const GlobalValue *GV,
2180 SDLoc DL, unsigned Offset, EVT PtrVT,
2181 unsigned GAFlags = SIInstrInfo::MO_NONE) {
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002182 // In order to support pc-relative addressing, the PC_ADD_REL_OFFSET SDNode is
2183 // lowered to the following code sequence:
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002184 //
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002185 // For constant address space:
2186 // s_getpc_b64 s[0:1]
2187 // s_add_u32 s0, s0, $symbol
2188 // s_addc_u32 s1, s1, 0
2189 //
2190 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
2191 // a fixup or relocation is emitted to replace $symbol with a literal
2192 // constant, which is a pc-relative offset from the encoding of the $symbol
2193 // operand to the global variable.
2194 //
2195 // For global address space:
2196 // s_getpc_b64 s[0:1]
2197 // s_add_u32 s0, s0, $symbol@{gotpc}rel32@lo
2198 // s_addc_u32 s1, s1, $symbol@{gotpc}rel32@hi
2199 //
2200 // s_getpc_b64 returns the address of the s_add_u32 instruction and then
2201 // fixups or relocations are emitted to replace $symbol@*@lo and
2202 // $symbol@*@hi with lower 32 bits and higher 32 bits of a literal constant,
2203 // which is a 64-bit pc-relative offset from the encoding of the $symbol
2204 // operand to the global variable.
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002205 //
2206 // What we want here is an offset from the value returned by s_getpc
2207 // (which is the address of the s_add_u32 instruction) to the global
2208 // variable, but since the encoding of $symbol starts 4 bytes after the start
2209 // of the s_add_u32 instruction, we end up with an offset that is 4 bytes too
2210 // small. This requires us to add 4 to the global variable offset in order to
2211 // compute the correct address.
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002212 SDValue PtrLo = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
2213 GAFlags);
2214 SDValue PtrHi = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, Offset + 4,
2215 GAFlags == SIInstrInfo::MO_NONE ?
2216 GAFlags : GAFlags + 1);
2217 return DAG.getNode(AMDGPUISD::PC_ADD_REL_OFFSET, DL, PtrVT, PtrLo, PtrHi);
Tom Stellardbf3e6e52016-06-14 20:29:59 +00002218}
2219
Tom Stellard418beb72016-07-13 14:23:33 +00002220SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
2221 SDValue Op,
2222 SelectionDAG &DAG) const {
2223 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
2224
2225 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS &&
2226 GSD->getAddressSpace() != AMDGPUAS::GLOBAL_ADDRESS)
2227 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
2228
2229 SDLoc DL(GSD);
2230 const GlobalValue *GV = GSD->getGlobal();
2231 EVT PtrVT = Op.getValueType();
2232
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00002233 if (shouldEmitFixup(GV))
Tom Stellard418beb72016-07-13 14:23:33 +00002234 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT);
Konstantin Zhuravlyov08326b62016-10-20 18:12:38 +00002235 else if (shouldEmitPCReloc(GV))
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002236 return buildPCRelGlobalAddress(DAG, GV, DL, GSD->getOffset(), PtrVT,
2237 SIInstrInfo::MO_REL32);
Tom Stellard418beb72016-07-13 14:23:33 +00002238
2239 SDValue GOTAddr = buildPCRelGlobalAddress(DAG, GV, DL, 0, PtrVT,
Konstantin Zhuravlyovc96b5d72016-10-14 04:37:34 +00002240 SIInstrInfo::MO_GOTPCREL32);
Tom Stellard418beb72016-07-13 14:23:33 +00002241
2242 Type *Ty = PtrVT.getTypeForEVT(*DAG.getContext());
2243 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
2244 const DataLayout &DataLayout = DAG.getDataLayout();
2245 unsigned Align = DataLayout.getABITypeAlignment(PtrTy);
2246 // FIXME: Use a PseudoSourceValue once those can be assigned an address space.
2247 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
2248
Justin Lebar9c375812016-07-15 18:27:10 +00002249 return DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), GOTAddr, PtrInfo, Align,
Justin Lebaradbf09e2016-09-11 01:38:58 +00002250 MachineMemOperand::MODereferenceable |
2251 MachineMemOperand::MOInvariant);
Tom Stellard418beb72016-07-13 14:23:33 +00002252}
2253
Matt Arsenault0bb294b2016-06-17 22:27:03 +00002254SDValue SITargetLowering::lowerTRAP(SDValue Op,
2255 SelectionDAG &DAG) const {
2256 const MachineFunction &MF = DAG.getMachineFunction();
2257 DiagnosticInfoUnsupported NoTrap(*MF.getFunction(),
2258 "trap handler not supported",
2259 Op.getDebugLoc(),
2260 DS_Warning);
2261 DAG.getContext()->diagnose(NoTrap);
2262
2263 // Emit s_endpgm.
2264
2265 // FIXME: This should really be selected to s_trap, but that requires
2266 // setting up the trap handler for it o do anything.
Matt Arsenault9babdf42016-06-22 20:15:28 +00002267 return DAG.getNode(AMDGPUISD::ENDPGM, SDLoc(Op), MVT::Other,
2268 Op.getOperand(0));
Matt Arsenault0bb294b2016-06-17 22:27:03 +00002269}
2270
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002271SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain,
2272 const SDLoc &DL, SDValue V) const {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00002273 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
2274 // the destination register.
2275 //
Tom Stellardfc92e772015-05-12 14:18:14 +00002276 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
2277 // so we will end up with redundant moves to m0.
2278 //
Matt Arsenault4ac341c2016-04-14 21:58:15 +00002279 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
2280
2281 // A Null SDValue creates a glue result.
2282 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
2283 V, Chain);
2284 return SDValue(M0, 0);
Tom Stellardfc92e772015-05-12 14:18:14 +00002285}
2286
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002287SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
2288 SDValue Op,
2289 MVT VT,
2290 unsigned Offset) const {
2291 SDLoc SL(Op);
2292 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
2293 DAG.getEntryNode(), Offset, false);
2294 // The local size values will have the hi 16-bits as zero.
2295 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
2296 DAG.getValueType(VT));
2297}
2298
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002299static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
Matt Arsenaulte0132462016-01-30 05:19:45 +00002300 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002301 "non-hsa intrinsic with hsa target",
2302 DL.getDebugLoc());
2303 DAG.getContext()->diagnose(BadIntrin);
2304 return DAG.getUNDEF(VT);
2305}
2306
2307static SDValue emitRemovedIntrinsicError(SelectionDAG& DAG, SDLoc DL, EVT VT) {
2308 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
2309 "intrinsic not supported on subtarget",
2310 DL.getDebugLoc());
Matt Arsenaulte0132462016-01-30 05:19:45 +00002311 DAG.getContext()->diagnose(BadIntrin);
2312 return DAG.getUNDEF(VT);
2313}
2314
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002315SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
2316 SelectionDAG &DAG) const {
2317 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00002318 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002319 const SIRegisterInfo *TRI = getSubtarget()->getRegisterInfo();
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002320
2321 EVT VT = Op.getValueType();
2322 SDLoc DL(Op);
2323 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
2324
Sanjay Patela2607012015-09-16 16:31:21 +00002325 // TODO: Should this propagate fast-math-flags?
2326
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002327 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00002328 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault48ab5262016-04-25 19:27:18 +00002329 case Intrinsic::amdgcn_queue_ptr: {
Tom Stellard0b76fc4c2016-09-16 21:34:26 +00002330 if (!Subtarget->isAmdCodeObjectV2()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00002331 DiagnosticInfoUnsupported BadIntrin(
2332 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
2333 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00002334 DAG.getContext()->diagnose(BadIntrin);
2335 return DAG.getUNDEF(VT);
2336 }
2337
Matt Arsenault48ab5262016-04-25 19:27:18 +00002338 auto Reg = IntrinsicID == Intrinsic::amdgcn_dispatch_ptr ?
2339 SIRegisterInfo::DISPATCH_PTR : SIRegisterInfo::QUEUE_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00002340 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
Matt Arsenault48ab5262016-04-25 19:27:18 +00002341 TRI->getPreloadedValue(MF, Reg), VT);
2342 }
Jan Veselyfea814d2016-06-21 20:46:20 +00002343 case Intrinsic::amdgcn_implicitarg_ptr: {
2344 unsigned offset = getImplicitParameterOffset(MFI, FIRST_IMPLICIT);
2345 return LowerParameterPtr(DAG, DL, DAG.getEntryNode(), offset);
2346 }
Matt Arsenaultdc4ebad2016-04-29 21:16:52 +00002347 case Intrinsic::amdgcn_kernarg_segment_ptr: {
2348 unsigned Reg
2349 = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
2350 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2351 }
Matt Arsenault8d718dc2016-07-22 17:01:30 +00002352 case Intrinsic::amdgcn_dispatch_id: {
2353 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_ID);
2354 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass, Reg, VT);
2355 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002356 case Intrinsic::amdgcn_rcp:
2357 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
2358 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00002359 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002360 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002361 case Intrinsic::amdgcn_rsq_legacy: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002362 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002363 return emitRemovedIntrinsicError(DAG, DL, VT);
2364
2365 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
2366 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002367 case Intrinsic::amdgcn_rcp_legacy: {
2368 if (Subtarget->getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
2369 return emitRemovedIntrinsicError(DAG, DL, VT);
2370 return DAG.getNode(AMDGPUISD::RCP_LEGACY, DL, VT, Op.getOperand(1));
2371 }
Matt Arsenault09b2c4a2016-07-15 21:26:52 +00002372 case Intrinsic::amdgcn_rsq_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002373 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00002374 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00002375
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002376 Type *Type = VT.getTypeForEVT(*DAG.getContext());
2377 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
2378 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
2379
2380 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
2381 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
2382 DAG.getConstantFP(Max, DL, VT));
2383 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
2384 DAG.getConstantFP(Min, DL, VT));
2385 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002386 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002387 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002388 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002389
Tom Stellardec2e43c2014-09-22 15:35:29 +00002390 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2391 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002392 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002393 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002394 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002395
Tom Stellardec2e43c2014-09-22 15:35:29 +00002396 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2397 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002398 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002399 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002400 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002401
Tom Stellardec2e43c2014-09-22 15:35:29 +00002402 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2403 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002404 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002405 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002406 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002407
Tom Stellardec2e43c2014-09-22 15:35:29 +00002408 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2409 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002410 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002411 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002412 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002413
Tom Stellardec2e43c2014-09-22 15:35:29 +00002414 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2415 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002416 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002417 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002418 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002419
Tom Stellardec2e43c2014-09-22 15:35:29 +00002420 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
2421 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002422 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002423 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002424 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002425
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002426 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2427 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002428 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002429 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002430 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002431
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002432 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2433 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002434 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00002435 if (Subtarget->isAmdHsaOS())
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00002436 return emitNonHSAIntrinsicError(DAG, DL, VT);
Matt Arsenaulte0132462016-01-30 05:19:45 +00002437
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00002438 return lowerImplicitZextParam(DAG, Op, MVT::i16,
2439 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenault43976df2016-01-30 04:25:19 +00002440 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002441 case Intrinsic::r600_read_tgid_x:
2442 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002443 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002444 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002445 case Intrinsic::r600_read_tgid_y:
2446 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002447 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002448 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002449 case Intrinsic::r600_read_tgid_z:
2450 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002451 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002452 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002453 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002454 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002455 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002456 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002457 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002458 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002459 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00002460 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002461 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002462 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00002463 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002464 case AMDGPUIntrinsic::SI_load_const: {
2465 SDValue Ops[] = {
2466 Op.getOperand(1),
2467 Op.getOperand(2)
2468 };
2469
2470 MachineMemOperand *MMO = MF.getMachineMemOperand(
Justin Lebaradbf09e2016-09-11 01:38:58 +00002471 MachinePointerInfo(),
2472 MachineMemOperand::MOLoad | MachineMemOperand::MODereferenceable |
2473 MachineMemOperand::MOInvariant,
2474 VT.getStoreSize(), 4);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002475 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
2476 Op->getVTList(), Ops, VT, MMO);
2477 }
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002478 case AMDGPUIntrinsic::amdgcn_fdiv_fast: {
2479 return lowerFDIV_FAST(Op, DAG);
2480 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002481 case AMDGPUIntrinsic::SI_vs_load_input:
2482 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
2483 Op.getOperand(1),
2484 Op.getOperand(2),
2485 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00002486
Tom Stellard2a9d9472015-05-12 15:00:46 +00002487 case AMDGPUIntrinsic::SI_fs_constant: {
2488 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2489 SDValue Glue = M0.getValue(1);
2490 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
2491 DAG.getConstant(2, DL, MVT::i32), // P0
2492 Op.getOperand(1), Op.getOperand(2), Glue);
2493 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00002494 case AMDGPUIntrinsic::SI_packf16:
2495 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
2496 return DAG.getUNDEF(MVT::i32);
2497 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00002498 case AMDGPUIntrinsic::SI_fs_interp: {
2499 SDValue IJ = Op.getOperand(4);
2500 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2501 DAG.getConstant(0, DL, MVT::i32));
2502 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
2503 DAG.getConstant(1, DL, MVT::i32));
2504 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
2505 SDValue Glue = M0.getValue(1);
2506 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
2507 DAG.getVTList(MVT::f32, MVT::Glue),
2508 I, Op.getOperand(1), Op.getOperand(2), Glue);
2509 Glue = SDValue(P1.getNode(), 1);
2510 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
2511 Op.getOperand(1), Op.getOperand(2), Glue);
2512 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00002513 case Intrinsic::amdgcn_interp_p1: {
2514 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
2515 SDValue Glue = M0.getValue(1);
2516 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
2517 Op.getOperand(2), Op.getOperand(3), Glue);
2518 }
2519 case Intrinsic::amdgcn_interp_p2: {
2520 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
2521 SDValue Glue = SDValue(M0.getNode(), 1);
2522 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
2523 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
2524 Glue);
2525 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002526 case Intrinsic::amdgcn_sin:
2527 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
2528
2529 case Intrinsic::amdgcn_cos:
2530 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
2531
2532 case Intrinsic::amdgcn_log_clamp: {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002533 if (Subtarget->getGeneration() < SISubtarget::VOLCANIC_ISLANDS)
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00002534 return SDValue();
2535
2536 DiagnosticInfoUnsupported BadIntrin(
2537 *MF.getFunction(), "intrinsic not supported on subtarget",
2538 DL.getDebugLoc());
2539 DAG.getContext()->diagnose(BadIntrin);
2540 return DAG.getUNDEF(VT);
2541 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002542 case Intrinsic::amdgcn_ldexp:
2543 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
2544 Op.getOperand(1), Op.getOperand(2));
Matt Arsenault74015162016-05-28 00:19:52 +00002545
2546 case Intrinsic::amdgcn_fract:
2547 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
2548
Matt Arsenaultf75257a2016-01-23 05:32:20 +00002549 case Intrinsic::amdgcn_class:
2550 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
2551 Op.getOperand(1), Op.getOperand(2));
2552 case Intrinsic::amdgcn_div_fmas:
2553 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
2554 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
2555 Op.getOperand(4));
2556
2557 case Intrinsic::amdgcn_div_fixup:
2558 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
2559 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
2560
2561 case Intrinsic::amdgcn_trig_preop:
2562 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
2563 Op.getOperand(1), Op.getOperand(2));
2564 case Intrinsic::amdgcn_div_scale: {
2565 // 3rd parameter required to be a constant.
2566 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2567 if (!Param)
2568 return DAG.getUNDEF(VT);
2569
2570 // Translate to the operands expected by the machine instruction. The
2571 // first parameter must be the same as the first instruction.
2572 SDValue Numerator = Op.getOperand(1);
2573 SDValue Denominator = Op.getOperand(2);
2574
2575 // Note this order is opposite of the machine instruction's operations,
2576 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
2577 // intrinsic has the numerator as the first operand to match a normal
2578 // division operation.
2579
2580 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
2581
2582 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
2583 Denominator, Numerator);
2584 }
Wei Ding07e03712016-07-28 16:42:13 +00002585 case Intrinsic::amdgcn_icmp: {
2586 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2587 int CondCode = CD->getSExtValue();
2588
2589 if (CondCode < ICmpInst::Predicate::FIRST_ICMP_PREDICATE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002590 CondCode >= ICmpInst::Predicate::BAD_ICMP_PREDICATE)
Wei Ding07e03712016-07-28 16:42:13 +00002591 return DAG.getUNDEF(VT);
2592
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002593 ICmpInst::Predicate IcInput = static_cast<ICmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002594 ISD::CondCode CCOpcode = getICmpCondCode(IcInput);
2595 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2596 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2597 }
2598 case Intrinsic::amdgcn_fcmp: {
2599 const auto *CD = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2600 int CondCode = CD->getSExtValue();
2601
2602 if (CondCode <= FCmpInst::Predicate::FCMP_FALSE ||
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002603 CondCode >= FCmpInst::Predicate::FCMP_TRUE)
Wei Ding07e03712016-07-28 16:42:13 +00002604 return DAG.getUNDEF(VT);
2605
NAKAMURA Takumi59a20642016-08-22 00:58:04 +00002606 FCmpInst::Predicate IcInput = static_cast<FCmpInst::Predicate>(CondCode);
Wei Ding07e03712016-07-28 16:42:13 +00002607 ISD::CondCode CCOpcode = getFCmpCondCode(IcInput);
2608 return DAG.getNode(AMDGPUISD::SETCC, DL, VT, Op.getOperand(1),
2609 Op.getOperand(2), DAG.getCondCode(CCOpcode));
2610 }
Matt Arsenault32fc5272016-07-26 16:45:45 +00002611 case Intrinsic::amdgcn_fmul_legacy:
2612 return DAG.getNode(AMDGPUISD::FMUL_LEGACY, DL, VT,
2613 Op.getOperand(1), Op.getOperand(2));
Matt Arsenaultc96e1de2016-07-18 18:35:05 +00002614 case Intrinsic::amdgcn_sffbh:
2615 case AMDGPUIntrinsic::AMDGPU_flbit_i32: // Legacy name.
2616 return DAG.getNode(AMDGPUISD::FFBH_I32, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002617 default:
2618 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
2619 }
2620}
2621
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002622SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
2623 SelectionDAG &DAG) const {
2624 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2625 switch (IntrID) {
2626 case Intrinsic::amdgcn_atomic_inc:
2627 case Intrinsic::amdgcn_atomic_dec: {
2628 MemSDNode *M = cast<MemSDNode>(Op);
2629 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
2630 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
2631 SDValue Ops[] = {
2632 M->getOperand(0), // Chain
2633 M->getOperand(2), // Ptr
2634 M->getOperand(3) // Value
2635 };
2636
2637 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
2638 M->getMemoryVT(), M->getMemOperand());
2639 }
2640 default:
2641 return SDValue();
2642 }
2643}
2644
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002645SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
2646 SelectionDAG &DAG) const {
2647 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00002648 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002649 SDValue Chain = Op.getOperand(0);
2650 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
2651
2652 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00002653 case AMDGPUIntrinsic::SI_sendmsg: {
2654 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
2655 SDValue Glue = Chain.getValue(1);
2656 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
2657 Op.getOperand(2), Glue);
2658 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002659 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002660 SDValue Ops[] = {
2661 Chain,
2662 Op.getOperand(2),
2663 Op.getOperand(3),
2664 Op.getOperand(4),
2665 Op.getOperand(5),
2666 Op.getOperand(6),
2667 Op.getOperand(7),
2668 Op.getOperand(8),
2669 Op.getOperand(9),
2670 Op.getOperand(10),
2671 Op.getOperand(11),
2672 Op.getOperand(12),
2673 Op.getOperand(13),
2674 Op.getOperand(14)
2675 };
2676
2677 EVT VT = Op.getOperand(3).getValueType();
2678
2679 MachineMemOperand *MMO = MF.getMachineMemOperand(
2680 MachinePointerInfo(),
2681 MachineMemOperand::MOStore,
2682 VT.getStoreSize(), 4);
2683 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
2684 Op->getVTList(), Ops, VT, MMO);
2685 }
Matt Arsenault00568682016-07-13 06:04:22 +00002686 case AMDGPUIntrinsic::AMDGPU_kill: {
Matt Arsenault03006fd2016-07-19 16:27:56 +00002687 SDValue Src = Op.getOperand(2);
2688 if (const ConstantFPSDNode *K = dyn_cast<ConstantFPSDNode>(Src)) {
Matt Arsenault00568682016-07-13 06:04:22 +00002689 if (!K->isNegative())
2690 return Chain;
Matt Arsenault03006fd2016-07-19 16:27:56 +00002691
2692 SDValue NegOne = DAG.getTargetConstant(FloatToBits(-1.0f), DL, MVT::i32);
2693 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, NegOne);
Matt Arsenault00568682016-07-13 06:04:22 +00002694 }
2695
Matt Arsenault03006fd2016-07-19 16:27:56 +00002696 SDValue Cast = DAG.getNode(ISD::BITCAST, DL, MVT::i32, Src);
2697 return DAG.getNode(AMDGPUISD::KILL, DL, MVT::Other, Chain, Cast);
Matt Arsenault00568682016-07-13 06:04:22 +00002698 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00002699 default:
2700 return SDValue();
2701 }
2702}
2703
Tom Stellard81d871d2013-11-13 23:36:50 +00002704SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2705 SDLoc DL(Op);
2706 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00002707 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00002708 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00002709
Matt Arsenaulta1436412016-02-10 18:21:45 +00002710 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
Matt Arsenault6dfda962016-02-10 18:21:39 +00002711 // FIXME: Copied from PPC
2712 // First, load into 32 bits, then truncate to 1 bit.
2713
2714 SDValue Chain = Load->getChain();
2715 SDValue BasePtr = Load->getBasePtr();
2716 MachineMemOperand *MMO = Load->getMemOperand();
2717
Tom Stellard115a6152016-11-10 16:02:37 +00002718 EVT RealMemVT = (MemVT == MVT::i1) ? MVT::i8 : MVT::i16;
2719
Matt Arsenault6dfda962016-02-10 18:21:39 +00002720 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
Tom Stellard115a6152016-11-10 16:02:37 +00002721 BasePtr, RealMemVT, MMO);
Matt Arsenault6dfda962016-02-10 18:21:39 +00002722
2723 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002724 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00002725 NewLD.getValue(1)
2726 };
2727
2728 return DAG.getMergeValues(Ops, DL);
2729 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002730
Matt Arsenaulta1436412016-02-10 18:21:45 +00002731 if (!MemVT.isVector())
2732 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002733
Matt Arsenaulta1436412016-02-10 18:21:45 +00002734 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
2735 "Custom lowering for non-i32 vectors hasn't been implemented.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00002736
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002737 unsigned AS = Load->getAddressSpace();
2738 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), MemVT,
2739 AS, Load->getAlignment())) {
2740 SDValue Ops[2];
2741 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(Load, DAG);
2742 return DAG.getMergeValues(Ops, DL);
2743 }
2744
Tom Stellardf8e6eaf2016-10-26 14:38:47 +00002745 MachineFunction &MF = DAG.getMachineFunction();
2746 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
2747 // If there is a possibilty that flat instruction access scratch memory
2748 // then we need to use the same legalization rules we use for private.
2749 if (AS == AMDGPUAS::FLAT_ADDRESS)
2750 AS = MFI->hasFlatScratchInit() ?
2751 AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS;
2752
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002753 unsigned NumElements = MemVT.getVectorNumElements();
2754 switch (AS) {
Matt Arsenaulta1436412016-02-10 18:21:45 +00002755 case AMDGPUAS::CONSTANT_ADDRESS:
2756 if (isMemOpUniform(Load))
2757 return SDValue();
2758 // Non-uniform loads will be selected to MUBUF instructions, so they
2759 // have the same legalization requires ments as global and private
2760 // loads.
2761 //
Justin Bognerb03fd122016-08-17 05:10:15 +00002762 LLVM_FALLTHROUGH;
Matt Arsenaulta1436412016-02-10 18:21:45 +00002763 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002764 case AMDGPUAS::FLAT_ADDRESS:
2765 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00002766 return SplitVectorLoad(Op, DAG);
2767 // v4 loads are supported for private and global memory.
2768 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002769 case AMDGPUAS::PRIVATE_ADDRESS: {
2770 // Depending on the setting of the private_element_size field in the
2771 // resource descriptor, we can only make private accesses up to a certain
2772 // size.
2773 switch (Subtarget->getMaxPrivateElementSize()) {
2774 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00002775 return scalarizeVectorLoad(Load, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002776 case 8:
2777 if (NumElements > 2)
2778 return SplitVectorLoad(Op, DAG);
2779 return SDValue();
2780 case 16:
2781 // Same as global/flat
2782 if (NumElements > 4)
2783 return SplitVectorLoad(Op, DAG);
2784 return SDValue();
2785 default:
2786 llvm_unreachable("unsupported private_element_size");
2787 }
2788 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002789 case AMDGPUAS::LOCAL_ADDRESS: {
2790 if (NumElements > 2)
2791 return SplitVectorLoad(Op, DAG);
2792
2793 if (NumElements == 2)
2794 return SDValue();
2795
Matt Arsenaulta1436412016-02-10 18:21:45 +00002796 // If properly aligned, if we split we might be able to use ds_read_b64.
2797 return SplitVectorLoad(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00002798 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00002799 default:
2800 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00002801 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002802}
2803
Tom Stellard0ec134f2014-02-04 17:18:40 +00002804SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
2805 if (Op.getValueType() != MVT::i64)
2806 return SDValue();
2807
2808 SDLoc DL(Op);
2809 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002810
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002811 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2812 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002813
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002814 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
2815 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
2816
2817 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
2818 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002819
2820 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
2821
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002822 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
2823 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002824
2825 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
2826
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002827 SDValue Res = DAG.getBuildVector(MVT::v2i32, DL, {Lo, Hi});
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00002828 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00002829}
2830
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002831// Catch division cases where we can use shortcuts with rcp and rsq
2832// instructions.
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002833SDValue SITargetLowering::lowerFastUnsafeFDIV(SDValue Op,
2834 SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002835 SDLoc SL(Op);
2836 SDValue LHS = Op.getOperand(0);
2837 SDValue RHS = Op.getOperand(1);
2838 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002839 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002840
2841 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault979902b2016-08-02 22:25:04 +00002842 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals()))) {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002843
Matt Arsenault979902b2016-08-02 22:25:04 +00002844 if (CLHS->isExactlyValue(1.0)) {
2845 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
2846 // the CI documentation has a worst case error of 1 ulp.
2847 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
2848 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002849
Matt Arsenault979902b2016-08-02 22:25:04 +00002850 // 1.0 / sqrt(x) -> rsq(x)
2851 //
2852 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
2853 // error seems really high at 2^29 ULP.
2854 if (RHS.getOpcode() == ISD::FSQRT)
2855 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
2856
2857 // 1.0 / x -> rcp(x)
2858 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
2859 }
2860
2861 // Same as for 1.0, but expand the sign out of the constant.
2862 if (CLHS->isExactlyValue(-1.0)) {
2863 // -1.0 / x -> rcp (fneg x)
2864 SDValue FNegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2865 return DAG.getNode(AMDGPUISD::RCP, SL, VT, FNegRHS);
2866 }
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002867 }
2868 }
2869
Wei Dinged0f97f2016-06-09 19:17:15 +00002870 const SDNodeFlags *Flags = Op->getFlags();
2871
2872 if (Unsafe || Flags->hasAllowReciprocal()) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002873 // Turn into multiply by the reciprocal.
2874 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00002875 SDNodeFlags Flags;
2876 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002877 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00002878 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002879 }
2880
2881 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002882}
2883
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002884// Faster 2.5 ULP division that does not support denormals.
2885SDValue SITargetLowering::lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const {
2886 SDLoc SL(Op);
2887 SDValue LHS = Op.getOperand(1);
2888 SDValue RHS = Op.getOperand(2);
2889
2890 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
2891
2892 const APFloat K0Val(BitsToFloat(0x6f800000));
2893 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
2894
2895 const APFloat K1Val(BitsToFloat(0x2f800000));
2896 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
2897
2898 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
2899
2900 EVT SetCCVT =
2901 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
2902
2903 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
2904
2905 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
2906
2907 // TODO: Should this propagate fast-math-flags?
2908 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
2909
2910 // rcp does not support denormals.
2911 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
2912
2913 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
2914
2915 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
2916}
2917
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002918SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002919 if (SDValue FastLowered = lowerFastUnsafeFDIV(Op, DAG))
Eric Christopher538d09d02016-06-07 20:27:12 +00002920 return FastLowered;
Matt Arsenault22ca3f82014-07-15 23:50:10 +00002921
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002922 SDLoc SL(Op);
2923 SDValue LHS = Op.getOperand(0);
2924 SDValue RHS = Op.getOperand(1);
2925
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002926 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002927
Wei Dinged0f97f2016-06-09 19:17:15 +00002928 SDVTList ScaleVT = DAG.getVTList(MVT::f32, MVT::i1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002929
Wei Dinged0f97f2016-06-09 19:17:15 +00002930 SDValue DenominatorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, RHS, RHS, LHS);
2931 SDValue NumeratorScaled = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, LHS, RHS, LHS);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002932
Matt Arsenaultdfec5ce2016-07-09 07:48:11 +00002933 // Denominator is scaled to not be denormal, so using rcp is ok.
Wei Dinged0f97f2016-06-09 19:17:15 +00002934 SDValue ApproxRcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002935
Wei Dinged0f97f2016-06-09 19:17:15 +00002936 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f32, DenominatorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002937
Wei Dinged0f97f2016-06-09 19:17:15 +00002938 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, ApproxRcp, One);
2939 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma0, ApproxRcp, ApproxRcp);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002940
Wei Dinged0f97f2016-06-09 19:17:15 +00002941 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, NumeratorScaled, Fma1);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002942
Wei Dinged0f97f2016-06-09 19:17:15 +00002943 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Mul, NumeratorScaled);
2944 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f32, Fma2, Fma1, Mul);
2945 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f32, NegDivScale0, Fma3, NumeratorScaled);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002946
Wei Dinged0f97f2016-06-09 19:17:15 +00002947 SDValue Scale = NumeratorScaled.getValue(1);
2948 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f32, Fma4, Fma1, Fma3, Scale);
Matt Arsenault37fefd62016-06-10 02:18:02 +00002949
Wei Dinged0f97f2016-06-09 19:17:15 +00002950 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f32, Fmas, RHS, LHS);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002951}
2952
2953SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002954 if (DAG.getTarget().Options.UnsafeFPMath)
Matt Arsenaulta1fe17c2016-07-19 23:16:53 +00002955 return lowerFastUnsafeFDIV(Op, DAG);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002956
2957 SDLoc SL(Op);
2958 SDValue X = Op.getOperand(0);
2959 SDValue Y = Op.getOperand(1);
2960
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002961 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002962
2963 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
2964
2965 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
2966
2967 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
2968
2969 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
2970
2971 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
2972
2973 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
2974
2975 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
2976
2977 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
2978
2979 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
2980 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
2981
2982 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
2983 NegDivScale0, Mul, DivScale1);
2984
2985 SDValue Scale;
2986
Matt Arsenault43e92fe2016-06-24 06:30:11 +00002987 if (Subtarget->getGeneration() == SISubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002988 // Workaround a hardware bug on SI where the condition output from div_scale
2989 // is not usable.
2990
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002991 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00002992
2993 // Figure out if the scale to use for div_fmas.
2994 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
2995 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
2996 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
2997 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
2998
2999 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
3000 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
3001
3002 SDValue Scale0Hi
3003 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
3004 SDValue Scale1Hi
3005 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
3006
3007 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
3008 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
3009 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
3010 } else {
3011 Scale = DivScale1.getValue(1);
3012 }
3013
3014 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
3015 Fma4, Fma3, Mul, Scale);
3016
3017 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00003018}
3019
3020SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
3021 EVT VT = Op.getValueType();
3022
3023 if (VT == MVT::f32)
3024 return LowerFDIV32(Op, DAG);
3025
3026 if (VT == MVT::f64)
3027 return LowerFDIV64(Op, DAG);
3028
3029 llvm_unreachable("Unexpected type for fdiv");
3030}
3031
Tom Stellard81d871d2013-11-13 23:36:50 +00003032SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
3033 SDLoc DL(Op);
3034 StoreSDNode *Store = cast<StoreSDNode>(Op);
3035 EVT VT = Store->getMemoryVT();
3036
Matt Arsenault95245662016-02-11 05:32:46 +00003037 if (VT == MVT::i1) {
3038 return DAG.getTruncStore(Store->getChain(), DL,
3039 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
3040 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00003041 }
3042
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003043 assert(VT.isVector() &&
3044 Store->getValue().getValueType().getScalarType() == MVT::i32);
3045
3046 unsigned AS = Store->getAddressSpace();
3047 if (!allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
3048 AS, Store->getAlignment())) {
3049 return expandUnalignedStore(Store, DAG);
3050 }
Tom Stellard81d871d2013-11-13 23:36:50 +00003051
Tom Stellardf8e6eaf2016-10-26 14:38:47 +00003052 MachineFunction &MF = DAG.getMachineFunction();
3053 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
3054 // If there is a possibilty that flat instruction access scratch memory
3055 // then we need to use the same legalization rules we use for private.
3056 if (AS == AMDGPUAS::FLAT_ADDRESS)
3057 AS = MFI->hasFlatScratchInit() ?
3058 AMDGPUAS::PRIVATE_ADDRESS : AMDGPUAS::GLOBAL_ADDRESS;
3059
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003060 unsigned NumElements = VT.getVectorNumElements();
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003061 switch (AS) {
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003062 case AMDGPUAS::GLOBAL_ADDRESS:
3063 case AMDGPUAS::FLAT_ADDRESS:
3064 if (NumElements > 4)
3065 return SplitVectorStore(Op, DAG);
3066 return SDValue();
3067 case AMDGPUAS::PRIVATE_ADDRESS: {
3068 switch (Subtarget->getMaxPrivateElementSize()) {
3069 case 4:
Matt Arsenault9c499c32016-04-14 23:31:26 +00003070 return scalarizeVectorStore(Store, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003071 case 8:
3072 if (NumElements > 2)
3073 return SplitVectorStore(Op, DAG);
3074 return SDValue();
3075 case 16:
3076 if (NumElements > 4)
3077 return SplitVectorStore(Op, DAG);
3078 return SDValue();
3079 default:
3080 llvm_unreachable("unsupported private_element_size");
3081 }
3082 }
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003083 case AMDGPUAS::LOCAL_ADDRESS: {
3084 if (NumElements > 2)
3085 return SplitVectorStore(Op, DAG);
3086
3087 if (NumElements == 2)
3088 return Op;
3089
Matt Arsenault95245662016-02-11 05:32:46 +00003090 // If properly aligned, if we split we might be able to use ds_write_b64.
3091 return SplitVectorStore(Op, DAG);
Matt Arsenaultbcdfee72016-05-02 20:13:51 +00003092 }
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00003093 default:
3094 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00003095 }
Tom Stellard81d871d2013-11-13 23:36:50 +00003096}
3097
Matt Arsenaultad14ce82014-07-19 18:44:39 +00003098SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003099 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00003100 EVT VT = Op.getValueType();
3101 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00003102 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003103 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
3104 DAG.getNode(ISD::FMUL, DL, VT, Arg,
3105 DAG.getConstantFP(0.5/M_PI, DL,
3106 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00003107
3108 switch (Op.getOpcode()) {
3109 case ISD::FCOS:
3110 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
3111 case ISD::FSIN:
3112 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
3113 default:
3114 llvm_unreachable("Wrong trig opcode");
3115 }
3116}
3117
Tom Stellard354a43c2016-04-01 18:27:37 +00003118SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
3119 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
3120 assert(AtomicNode->isCompareAndSwap());
3121 unsigned AS = AtomicNode->getAddressSpace();
3122
3123 // No custom lowering required for local address space
3124 if (!isFlatGlobalAddrSpace(AS))
3125 return Op;
3126
3127 // Non-local address space requires custom lowering for atomic compare
3128 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
3129 SDLoc DL(Op);
3130 SDValue ChainIn = Op.getOperand(0);
3131 SDValue Addr = Op.getOperand(1);
3132 SDValue Old = Op.getOperand(2);
3133 SDValue New = Op.getOperand(3);
3134 EVT VT = Op.getValueType();
3135 MVT SimpleVT = VT.getSimpleVT();
3136 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
3137
Ahmed Bougacha128f8732016-04-26 21:15:30 +00003138 SDValue NewOld = DAG.getBuildVector(VecType, DL, {New, Old});
Tom Stellard354a43c2016-04-01 18:27:37 +00003139 SDValue Ops[] = { ChainIn, Addr, NewOld };
Matt Arsenault88701812016-06-09 23:42:48 +00003140
3141 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL, Op->getVTList(),
3142 Ops, VT, AtomicNode->getMemOperand());
Tom Stellard354a43c2016-04-01 18:27:37 +00003143}
3144
Tom Stellard75aadc22012-12-11 21:25:42 +00003145//===----------------------------------------------------------------------===//
3146// Custom DAG optimizations
3147//===----------------------------------------------------------------------===//
3148
Matt Arsenault364a6742014-06-11 17:50:44 +00003149SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00003150 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00003151 EVT VT = N->getValueType(0);
3152 EVT ScalarVT = VT.getScalarType();
3153 if (ScalarVT != MVT::f32)
3154 return SDValue();
3155
3156 SelectionDAG &DAG = DCI.DAG;
3157 SDLoc DL(N);
3158
3159 SDValue Src = N->getOperand(0);
3160 EVT SrcVT = Src.getValueType();
3161
3162 // TODO: We could try to match extracting the higher bytes, which would be
3163 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
3164 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
3165 // about in practice.
3166 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
3167 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
3168 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
3169 DCI.AddToWorklist(Cvt.getNode());
3170 return Cvt;
3171 }
3172 }
3173
Matt Arsenault364a6742014-06-11 17:50:44 +00003174 return SDValue();
3175}
3176
Eric Christopher6c5b5112015-03-11 18:43:21 +00003177/// \brief Return true if the given offset Size in bytes can be folded into
3178/// the immediate offsets of a memory instruction for the given address space.
3179static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003180 const SISubtarget &STI) {
Eric Christopher6c5b5112015-03-11 18:43:21 +00003181 switch (AS) {
3182 case AMDGPUAS::GLOBAL_ADDRESS: {
3183 // MUBUF instructions a 12-bit offset in bytes.
3184 return isUInt<12>(OffsetSize);
3185 }
3186 case AMDGPUAS::CONSTANT_ADDRESS: {
3187 // SMRD instructions have an 8-bit offset in dwords on SI and
3188 // a 20-bit offset in bytes on VI.
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003189 if (STI.getGeneration() >= SISubtarget::VOLCANIC_ISLANDS)
Eric Christopher6c5b5112015-03-11 18:43:21 +00003190 return isUInt<20>(OffsetSize);
3191 else
3192 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
3193 }
3194 case AMDGPUAS::LOCAL_ADDRESS:
3195 case AMDGPUAS::REGION_ADDRESS: {
3196 // The single offset versions have a 16-bit offset in bytes.
3197 return isUInt<16>(OffsetSize);
3198 }
3199 case AMDGPUAS::PRIVATE_ADDRESS:
3200 // Indirect register addressing does not use any offsets.
3201 default:
3202 return 0;
3203 }
3204}
3205
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003206// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
3207
3208// This is a variant of
3209// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
3210//
3211// The normal DAG combiner will do this, but only if the add has one use since
3212// that would increase the number of instructions.
3213//
3214// This prevents us from seeing a constant offset that can be folded into a
3215// memory instruction's addressing mode. If we know the resulting add offset of
3216// a pointer can be folded into an addressing offset, we can replace the pointer
3217// operand with the add of new constant offset. This eliminates one of the uses,
3218// and may allow the remaining use to also be simplified.
3219//
3220SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
3221 unsigned AddrSpace,
3222 DAGCombinerInfo &DCI) const {
3223 SDValue N0 = N->getOperand(0);
3224 SDValue N1 = N->getOperand(1);
3225
3226 if (N0.getOpcode() != ISD::ADD)
3227 return SDValue();
3228
3229 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
3230 if (!CN1)
3231 return SDValue();
3232
3233 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
3234 if (!CAdd)
3235 return SDValue();
3236
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003237 // If the resulting offset is too large, we can't fold it into the addressing
3238 // mode offset.
3239 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00003240 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *getSubtarget()))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003241 return SDValue();
3242
3243 SelectionDAG &DAG = DCI.DAG;
3244 SDLoc SL(N);
3245 EVT VT = N->getValueType(0);
3246
3247 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003248 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003249
3250 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
3251}
3252
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003253static bool bitOpWithConstantIsReducible(unsigned Opc, uint32_t Val) {
3254 return (Opc == ISD::AND && (Val == 0 || Val == 0xffffffff)) ||
3255 (Opc == ISD::OR && (Val == 0xffffffff || Val == 0)) ||
3256 (Opc == ISD::XOR && Val == 0);
3257}
3258
3259// Break up 64-bit bit operation of a constant into two 32-bit and/or/xor. This
3260// will typically happen anyway for a VALU 64-bit and. This exposes other 32-bit
3261// integer combine opportunities since most 64-bit operations are decomposed
3262// this way. TODO: We won't want this for SALU especially if it is an inline
3263// immediate.
3264SDValue SITargetLowering::splitBinaryBitConstantOp(
3265 DAGCombinerInfo &DCI,
3266 const SDLoc &SL,
3267 unsigned Opc, SDValue LHS,
3268 const ConstantSDNode *CRHS) const {
3269 uint64_t Val = CRHS->getZExtValue();
3270 uint32_t ValLo = Lo_32(Val);
3271 uint32_t ValHi = Hi_32(Val);
3272 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
3273
3274 if ((bitOpWithConstantIsReducible(Opc, ValLo) ||
3275 bitOpWithConstantIsReducible(Opc, ValHi)) ||
3276 (CRHS->hasOneUse() && !TII->isInlineConstant(CRHS->getAPIntValue()))) {
3277 // If we need to materialize a 64-bit immediate, it will be split up later
3278 // anyway. Avoid creating the harder to understand 64-bit immediate
3279 // materialization.
3280 return splitBinaryBitConstantOpImpl(DCI, SL, Opc, LHS, ValLo, ValHi);
3281 }
3282
3283 return SDValue();
3284}
3285
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003286SDValue SITargetLowering::performAndCombine(SDNode *N,
3287 DAGCombinerInfo &DCI) const {
3288 if (DCI.isBeforeLegalize())
3289 return SDValue();
3290
3291 SelectionDAG &DAG = DCI.DAG;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003292 EVT VT = N->getValueType(0);
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003293 SDValue LHS = N->getOperand(0);
3294 SDValue RHS = N->getOperand(1);
3295
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003296
3297 if (VT == MVT::i64) {
3298 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
3299 if (CRHS) {
3300 if (SDValue Split
3301 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::AND, LHS, CRHS))
3302 return Split;
3303 }
3304 }
3305
3306 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
3307 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
3308 if (LHS.getOpcode() == ISD::SETCC && RHS.getOpcode() == ISD::SETCC) {
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003309 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
3310 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
3311
3312 SDValue X = LHS.getOperand(0);
3313 SDValue Y = RHS.getOperand(0);
3314 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
3315 return SDValue();
3316
3317 if (LCC == ISD::SETO) {
3318 if (X != LHS.getOperand(1))
3319 return SDValue();
3320
3321 if (RCC == ISD::SETUNE) {
3322 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
3323 if (!C1 || !C1->isInfinity() || C1->isNegative())
3324 return SDValue();
3325
3326 const uint32_t Mask = SIInstrFlags::N_NORMAL |
3327 SIInstrFlags::N_SUBNORMAL |
3328 SIInstrFlags::N_ZERO |
3329 SIInstrFlags::P_ZERO |
3330 SIInstrFlags::P_SUBNORMAL |
3331 SIInstrFlags::P_NORMAL;
3332
3333 static_assert(((~(SIInstrFlags::S_NAN |
3334 SIInstrFlags::Q_NAN |
3335 SIInstrFlags::N_INFINITY |
3336 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
3337 "mask not equal");
3338
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003339 SDLoc DL(N);
3340 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
3341 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003342 }
3343 }
3344 }
3345
3346 return SDValue();
3347}
3348
Matt Arsenaultf2290332015-01-06 23:00:39 +00003349SDValue SITargetLowering::performOrCombine(SDNode *N,
3350 DAGCombinerInfo &DCI) const {
3351 SelectionDAG &DAG = DCI.DAG;
3352 SDValue LHS = N->getOperand(0);
3353 SDValue RHS = N->getOperand(1);
3354
Matt Arsenault3b082382016-04-12 18:24:38 +00003355 EVT VT = N->getValueType(0);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003356 if (VT == MVT::i1) {
3357 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
3358 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
3359 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
3360 SDValue Src = LHS.getOperand(0);
3361 if (Src != RHS.getOperand(0))
3362 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003363
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003364 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
3365 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
3366 if (!CLHS || !CRHS)
3367 return SDValue();
Matt Arsenault3b082382016-04-12 18:24:38 +00003368
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003369 // Only 10 bits are used.
3370 static const uint32_t MaxMask = 0x3ff;
Matt Arsenault3b082382016-04-12 18:24:38 +00003371
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003372 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
3373 SDLoc DL(N);
3374 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
3375 Src, DAG.getConstant(NewMask, DL, MVT::i32));
3376 }
Matt Arsenault3b082382016-04-12 18:24:38 +00003377
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003378 return SDValue();
3379 }
3380
3381 if (VT != MVT::i64)
3382 return SDValue();
3383
3384 // TODO: This could be a generic combine with a predicate for extracting the
3385 // high half of an integer being free.
3386
3387 // (or i64:x, (zero_extend i32:y)) ->
3388 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
3389 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
3390 RHS.getOpcode() != ISD::ZERO_EXTEND)
3391 std::swap(LHS, RHS);
3392
3393 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
3394 SDValue ExtSrc = RHS.getOperand(0);
3395 EVT SrcVT = ExtSrc.getValueType();
3396 if (SrcVT == MVT::i32) {
3397 SDLoc SL(N);
3398 SDValue LowLHS, HiBits;
3399 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
3400 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
3401
3402 DCI.AddToWorklist(LowOr.getNode());
3403 DCI.AddToWorklist(HiBits.getNode());
3404
3405 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3406 LowOr, HiBits);
3407 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
Matt Arsenault3b082382016-04-12 18:24:38 +00003408 }
3409 }
3410
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003411 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
3412 if (CRHS) {
3413 if (SDValue Split
3414 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::OR, LHS, CRHS))
3415 return Split;
3416 }
Matt Arsenaultf2290332015-01-06 23:00:39 +00003417
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003418 return SDValue();
3419}
Matt Arsenaultf2290332015-01-06 23:00:39 +00003420
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003421SDValue SITargetLowering::performXorCombine(SDNode *N,
3422 DAGCombinerInfo &DCI) const {
3423 EVT VT = N->getValueType(0);
3424 if (VT != MVT::i64)
3425 return SDValue();
Matt Arsenaultf2290332015-01-06 23:00:39 +00003426
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003427 SDValue LHS = N->getOperand(0);
3428 SDValue RHS = N->getOperand(1);
3429
3430 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS);
3431 if (CRHS) {
3432 if (SDValue Split
3433 = splitBinaryBitConstantOp(DCI, SDLoc(N), ISD::XOR, LHS, CRHS))
3434 return Split;
Matt Arsenaultf2290332015-01-06 23:00:39 +00003435 }
3436
3437 return SDValue();
3438}
3439
3440SDValue SITargetLowering::performClassCombine(SDNode *N,
3441 DAGCombinerInfo &DCI) const {
3442 SelectionDAG &DAG = DCI.DAG;
3443 SDValue Mask = N->getOperand(1);
3444
3445 // fp_class x, 0 -> false
3446 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
3447 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003448 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003449 }
3450
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003451 if (N->getOperand(0).isUndef())
3452 return DAG.getUNDEF(MVT::i1);
3453
Matt Arsenaultf2290332015-01-06 23:00:39 +00003454 return SDValue();
3455}
3456
Matt Arsenault9cd90712016-04-14 01:42:16 +00003457// Constant fold canonicalize.
3458SDValue SITargetLowering::performFCanonicalizeCombine(
3459 SDNode *N,
3460 DAGCombinerInfo &DCI) const {
3461 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
3462 if (!CFP)
3463 return SDValue();
3464
3465 SelectionDAG &DAG = DCI.DAG;
3466 const APFloat &C = CFP->getValueAPF();
3467
3468 // Flush denormals to 0 if not enabled.
3469 if (C.isDenormal()) {
3470 EVT VT = N->getValueType(0);
3471 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
3472 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3473
3474 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
3475 return DAG.getConstantFP(0.0, SDLoc(N), VT);
3476 }
3477
3478 if (C.isNaN()) {
3479 EVT VT = N->getValueType(0);
3480 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
3481 if (C.isSignaling()) {
3482 // Quiet a signaling NaN.
3483 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3484 }
3485
3486 // Make sure it is the canonical NaN bitpattern.
3487 //
3488 // TODO: Can we use -1 as the canonical NaN value since it's an inline
3489 // immediate?
3490 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
3491 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
3492 }
3493
3494 return SDValue(CFP, 0);
3495}
3496
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003497static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
3498 switch (Opc) {
3499 case ISD::FMAXNUM:
3500 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003501 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003502 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003503 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003504 return AMDGPUISD::UMAX3;
3505 case ISD::FMINNUM:
3506 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003507 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003508 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003509 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003510 return AMDGPUISD::UMIN3;
3511 default:
3512 llvm_unreachable("Not a min/max opcode");
3513 }
3514}
3515
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003516static SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3517 SDValue Op0, SDValue Op1, bool Signed) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003518 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
3519 if (!K1)
3520 return SDValue();
3521
3522 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
3523 if (!K0)
3524 return SDValue();
3525
Matt Arsenaultf639c322016-01-28 20:53:42 +00003526 if (Signed) {
3527 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
3528 return SDValue();
3529 } else {
3530 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
3531 return SDValue();
3532 }
3533
3534 EVT VT = K0->getValueType(0);
Tom Stellard115a6152016-11-10 16:02:37 +00003535
3536 MVT NVT = MVT::i32;
3537 unsigned ExtOp = Signed ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
3538
3539 SDValue Tmp1, Tmp2, Tmp3;
3540 Tmp1 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(0));
3541 Tmp2 = DAG.getNode(ExtOp, SL, NVT, Op0->getOperand(1));
3542 Tmp3 = DAG.getNode(ExtOp, SL, NVT, Op1);
3543
3544 if (VT == MVT::i16) {
3545 Tmp1 = DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, NVT,
3546 Tmp1, Tmp2, Tmp3);
3547
3548 return DAG.getNode(ISD::TRUNCATE, SL, VT, Tmp1);
3549 } else
3550 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
3551 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
Matt Arsenaultf639c322016-01-28 20:53:42 +00003552}
3553
3554static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
3555 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
3556 return true;
3557
3558 return DAG.isKnownNeverNaN(Op);
3559}
3560
Benjamin Kramerbdc49562016-06-12 15:39:02 +00003561static SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL,
3562 SDValue Op0, SDValue Op1) {
Matt Arsenaultf639c322016-01-28 20:53:42 +00003563 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
3564 if (!K1)
3565 return SDValue();
3566
3567 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
3568 if (!K0)
3569 return SDValue();
3570
3571 // Ordered >= (although NaN inputs should have folded away by now).
3572 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
3573 if (Cmp == APFloat::cmpGreaterThan)
3574 return SDValue();
3575
3576 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
3577 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
3578 // give the other result, which is different from med3 with a NaN input.
3579 SDValue Var = Op0.getOperand(0);
3580 if (!isKnownNeverSNan(DAG, Var))
3581 return SDValue();
3582
3583 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
3584 Var, SDValue(K0, 0), SDValue(K1, 0));
3585}
3586
3587SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
3588 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003589 SelectionDAG &DAG = DCI.DAG;
3590
3591 unsigned Opc = N->getOpcode();
3592 SDValue Op0 = N->getOperand(0);
3593 SDValue Op1 = N->getOperand(1);
3594
3595 // Only do this if the inner op has one use since this will just increases
3596 // register pressure for no benefit.
3597
Matt Arsenault5b39b342016-01-28 20:53:48 +00003598 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
3599 // max(max(a, b), c) -> max3(a, b, c)
3600 // min(min(a, b), c) -> min3(a, b, c)
3601 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
3602 SDLoc DL(N);
3603 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3604 DL,
3605 N->getValueType(0),
3606 Op0.getOperand(0),
3607 Op0.getOperand(1),
3608 Op1);
3609 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003610
Matt Arsenault5b39b342016-01-28 20:53:48 +00003611 // Try commuted.
3612 // max(a, max(b, c)) -> max3(a, b, c)
3613 // min(a, min(b, c)) -> min3(a, b, c)
3614 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
3615 SDLoc DL(N);
3616 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
3617 DL,
3618 N->getValueType(0),
3619 Op0,
3620 Op1.getOperand(0),
3621 Op1.getOperand(1));
3622 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003623 }
3624
Matt Arsenaultf639c322016-01-28 20:53:42 +00003625 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
3626 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
3627 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
3628 return Med3;
3629 }
3630
3631 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
3632 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
3633 return Med3;
3634 }
3635
3636 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00003637 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
3638 (Opc == AMDGPUISD::FMIN_LEGACY &&
3639 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00003640 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
3641 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
3642 return Res;
3643 }
3644
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003645 return SDValue();
3646}
3647
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003648SDValue SITargetLowering::performSetCCCombine(SDNode *N,
3649 DAGCombinerInfo &DCI) const {
3650 SelectionDAG &DAG = DCI.DAG;
3651 SDLoc SL(N);
3652
3653 SDValue LHS = N->getOperand(0);
3654 SDValue RHS = N->getOperand(1);
3655 EVT VT = LHS.getValueType();
3656
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00003657 if (VT != MVT::f32 && VT != MVT::f64 && (Subtarget->has16BitInsts() &&
3658 VT != MVT::f16))
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003659 return SDValue();
3660
3661 // Match isinf pattern
3662 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
3663 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
3664 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
3665 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
3666 if (!CRHS)
3667 return SDValue();
3668
3669 const APFloat &APF = CRHS->getValueAPF();
3670 if (APF.isInfinity() && !APF.isNegative()) {
3671 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003672 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
3673 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003674 }
3675 }
3676
3677 return SDValue();
3678}
3679
Tom Stellard75aadc22012-12-11 21:25:42 +00003680SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
3681 DAGCombinerInfo &DCI) const {
3682 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00003683 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00003684
3685 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00003686 default:
3687 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00003688 case ISD::SETCC:
3689 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00003690 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003691 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00003692 case ISD::SMAX:
3693 case ISD::SMIN:
3694 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00003695 case ISD::UMIN:
3696 case AMDGPUISD::FMIN_LEGACY:
3697 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003698 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00003699 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003700 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00003701 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003702 break;
3703 }
Matt Arsenault364a6742014-06-11 17:50:44 +00003704
3705 case AMDGPUISD::CVT_F32_UBYTE0:
3706 case AMDGPUISD::CVT_F32_UBYTE1:
3707 case AMDGPUISD::CVT_F32_UBYTE2:
3708 case AMDGPUISD::CVT_F32_UBYTE3: {
3709 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +00003710
Matt Arsenault364a6742014-06-11 17:50:44 +00003711 SDValue Src = N->getOperand(0);
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +00003712 SDValue Srl = N->getOperand(0);
3713 if (Srl.getOpcode() == ISD::ZERO_EXTEND)
3714 Srl = Srl.getOperand(0);
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003715
Matt Arsenault327bb5a2016-07-01 22:47:50 +00003716 // TODO: Handle (or x, (srl y, 8)) pattern when known bits are zero.
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +00003717 if (Srl.getOpcode() == ISD::SRL) {
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003718 // cvt_f32_ubyte0 (srl x, 16) -> cvt_f32_ubyte2 x
3719 // cvt_f32_ubyte1 (srl x, 16) -> cvt_f32_ubyte3 x
3720 // cvt_f32_ubyte0 (srl x, 8) -> cvt_f32_ubyte1 x
3721
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +00003722 if (const ConstantSDNode *C =
3723 dyn_cast<ConstantSDNode>(Srl.getOperand(1))) {
3724 Srl = DAG.getZExtOrTrunc(Srl.getOperand(0), SDLoc(Srl.getOperand(0)),
3725 EVT(MVT::i32));
3726
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003727 unsigned SrcOffset = C->getZExtValue() + 8 * Offset;
3728 if (SrcOffset < 32 && SrcOffset % 8 == 0) {
3729 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0 + SrcOffset / 8, DL,
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +00003730 MVT::f32, Srl);
Matt Arsenaulta949dc62016-05-09 16:29:50 +00003731 }
3732 }
3733 }
3734
Matt Arsenault364a6742014-06-11 17:50:44 +00003735 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
3736
3737 APInt KnownZero, KnownOne;
3738 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3739 !DCI.isBeforeLegalizeOps());
3740 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3741 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
3742 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
3743 DCI.CommitTargetLoweringOpt(TLO);
3744 }
3745
3746 break;
3747 }
Konstantin Zhuravlyovfda33ea2016-10-21 22:10:03 +00003748 case ISD::SINT_TO_FP:
Matt Arsenault364a6742014-06-11 17:50:44 +00003749 case ISD::UINT_TO_FP: {
3750 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003751 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003752 case ISD::FADD: {
3753 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3754 break;
3755
3756 EVT VT = N->getValueType(0);
3757 if (VT != MVT::f32)
3758 break;
3759
Matt Arsenault8d630032015-02-20 22:10:41 +00003760 // Only do this if we are not trying to support denormals. v_mad_f32 does
3761 // not support denormals ever.
3762 if (Subtarget->hasFP32Denormals())
3763 break;
3764
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003765 SDValue LHS = N->getOperand(0);
3766 SDValue RHS = N->getOperand(1);
3767
3768 // These should really be instruction patterns, but writing patterns with
3769 // source modiifiers is a pain.
3770
3771 // fadd (fadd (a, a), b) -> mad 2.0, a, b
3772 if (LHS.getOpcode() == ISD::FADD) {
3773 SDValue A = LHS.getOperand(0);
3774 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003775 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003776 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003777 }
3778 }
3779
3780 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
3781 if (RHS.getOpcode() == ISD::FADD) {
3782 SDValue A = RHS.getOperand(0);
3783 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003784 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003785 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003786 }
3787 }
3788
Matt Arsenault8d630032015-02-20 22:10:41 +00003789 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00003790 }
Matt Arsenault8675db12014-08-29 16:01:14 +00003791 case ISD::FSUB: {
3792 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3793 break;
3794
3795 EVT VT = N->getValueType(0);
3796
3797 // Try to get the fneg to fold into the source modifier. This undoes generic
3798 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00003799 //
3800 // Only do this if we are not trying to support denormals. v_mad_f32 does
3801 // not support denormals ever.
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00003802 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00003803 SDValue LHS = N->getOperand(0);
3804 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003805 if (LHS.getOpcode() == ISD::FADD) {
3806 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
3807
3808 SDValue A = LHS.getOperand(0);
3809 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003810 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003811 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
3812
Matt Arsenault8d630032015-02-20 22:10:41 +00003813 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003814 }
3815 }
3816
3817 if (RHS.getOpcode() == ISD::FADD) {
3818 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
3819
3820 SDValue A = RHS.getOperand(0);
3821 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003822 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00003823 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00003824 }
3825 }
Matt Arsenault8d630032015-02-20 22:10:41 +00003826
3827 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00003828 }
3829
3830 break;
3831 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003832 case ISD::LOAD:
3833 case ISD::STORE:
3834 case ISD::ATOMIC_LOAD:
3835 case ISD::ATOMIC_STORE:
3836 case ISD::ATOMIC_CMP_SWAP:
3837 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
3838 case ISD::ATOMIC_SWAP:
3839 case ISD::ATOMIC_LOAD_ADD:
3840 case ISD::ATOMIC_LOAD_SUB:
3841 case ISD::ATOMIC_LOAD_AND:
3842 case ISD::ATOMIC_LOAD_OR:
3843 case ISD::ATOMIC_LOAD_XOR:
3844 case ISD::ATOMIC_LOAD_NAND:
3845 case ISD::ATOMIC_LOAD_MIN:
3846 case ISD::ATOMIC_LOAD_MAX:
3847 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003848 case ISD::ATOMIC_LOAD_UMAX:
3849 case AMDGPUISD::ATOMIC_INC:
3850 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003851 if (DCI.isBeforeLegalize())
3852 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003853
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003854 MemSDNode *MemNode = cast<MemSDNode>(N);
3855 SDValue Ptr = MemNode->getBasePtr();
3856
3857 // TODO: We could also do this for multiplies.
3858 unsigned AS = MemNode->getAddressSpace();
3859 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
3860 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
3861 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00003862 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003863
3864 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
3865 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
3866 }
3867 }
3868 break;
3869 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00003870 case ISD::AND:
3871 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003872 case ISD::OR:
3873 return performOrCombine(N, DCI);
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00003874 case ISD::XOR:
3875 return performXorCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00003876 case AMDGPUISD::FP_CLASS:
3877 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00003878 case ISD::FCANONICALIZE:
3879 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003880 case AMDGPUISD::FRACT:
3881 case AMDGPUISD::RCP:
3882 case AMDGPUISD::RSQ:
Matt Arsenault32fc5272016-07-26 16:45:45 +00003883 case AMDGPUISD::RCP_LEGACY:
Matt Arsenaultb6d8c372016-06-20 18:33:56 +00003884 case AMDGPUISD::RSQ_LEGACY:
3885 case AMDGPUISD::RSQ_CLAMP:
3886 case AMDGPUISD::LDEXP: {
3887 SDValue Src = N->getOperand(0);
3888 if (Src.isUndef())
3889 return Src;
3890 break;
3891 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00003892 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003893 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00003894}
Christian Konigd910b7d2013-02-26 17:52:16 +00003895
Christian Konig8e06e2a2013-04-10 08:39:08 +00003896/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00003897static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00003898 switch (Idx) {
3899 default: return 0;
3900 case AMDGPU::sub0: return 0;
3901 case AMDGPU::sub1: return 1;
3902 case AMDGPU::sub2: return 2;
3903 case AMDGPU::sub3: return 3;
3904 }
3905}
3906
3907/// \brief Adjust the writemask of MIMG instructions
3908void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
3909 SelectionDAG &DAG) const {
3910 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00003911 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003912 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
3913 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00003914 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003915
3916 // Try to figure out the used register components
3917 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
3918 I != E; ++I) {
3919
3920 // Abort if we can't understand the usage
3921 if (!I->isMachineOpcode() ||
3922 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
3923 return;
3924
Tom Stellard54774e52013-10-23 02:53:47 +00003925 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
3926 // Note that subregs are packed, i.e. Lane==0 is the first bit set
3927 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
3928 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00003929 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00003930
Tom Stellard54774e52013-10-23 02:53:47 +00003931 // Set which texture component corresponds to the lane.
3932 unsigned Comp;
3933 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
3934 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00003935 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00003936 Dmask &= ~(1 << Comp);
3937 }
3938
Christian Konig8e06e2a2013-04-10 08:39:08 +00003939 // Abort if we have more than one user per component
3940 if (Users[Lane])
3941 return;
3942
3943 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00003944 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00003945 }
3946
Tom Stellard54774e52013-10-23 02:53:47 +00003947 // Abort if there's no change
3948 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00003949 return;
3950
3951 // Adjust the writemask in the node
3952 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003953 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003954 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003955 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00003956 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003957
Christian Konig8b1ed282013-04-10 08:39:16 +00003958 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00003959 // (if NewDmask has only one bit set...)
3960 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003961 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
3962 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00003963 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00003964 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00003965 SDValue(Node, 0), RC);
3966 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
3967 return;
3968 }
3969
Christian Konig8e06e2a2013-04-10 08:39:08 +00003970 // Update the users of the node with the new indices
3971 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
3972
3973 SDNode *User = Users[i];
3974 if (!User)
3975 continue;
3976
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003977 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00003978 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
3979
3980 switch (Idx) {
3981 default: break;
3982 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
3983 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
3984 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
3985 }
3986 }
3987}
3988
Tom Stellardc98ee202015-07-16 19:40:07 +00003989static bool isFrameIndexOp(SDValue Op) {
3990 if (Op.getOpcode() == ISD::AssertZext)
3991 Op = Op.getOperand(0);
3992
3993 return isa<FrameIndexSDNode>(Op);
3994}
3995
Tom Stellard3457a842014-10-09 19:06:00 +00003996/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
3997/// with frame index operands.
3998/// LLVM assumes that inputs are to these instructions are registers.
3999void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
4000 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00004001
4002 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00004003 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00004004 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00004005 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00004006 continue;
4007 }
4008
Tom Stellard3457a842014-10-09 19:06:00 +00004009 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00004010 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00004011 Node->getOperand(i).getValueType(),
4012 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00004013 }
4014
Tom Stellard3457a842014-10-09 19:06:00 +00004015 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00004016}
4017
Matt Arsenault08d84942014-06-03 23:06:13 +00004018/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00004019SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
4020 SelectionDAG &DAG) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00004021 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00004022 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00004023
Nicolai Haehnlec06bfa12016-07-11 21:59:43 +00004024 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore() &&
4025 !TII->isGather4(Opcode))
Christian Konig8e06e2a2013-04-10 08:39:08 +00004026 adjustWritemask(Node, DAG);
4027
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00004028 if (Opcode == AMDGPU::INSERT_SUBREG ||
4029 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00004030 legalizeTargetIndependentNode(Node, DAG);
4031 return Node;
4032 }
Tom Stellard654d6692015-01-08 15:08:17 +00004033 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00004034}
Christian Konig8b1ed282013-04-10 08:39:16 +00004035
4036/// \brief Assign the register class depending on the number of
4037/// bits set in the writemask
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004038void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr &MI,
Christian Konig8b1ed282013-04-10 08:39:16 +00004039 SDNode *Node) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00004040 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004041
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004042 MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00004043
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004044 if (TII->isVOP3(MI.getOpcode())) {
Matt Arsenault6005fcb2015-10-21 21:51:02 +00004045 // Make sure constant bus requirements are respected.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004046 TII->legalizeOperandsVOP3(MRI, MI);
Matt Arsenault6005fcb2015-10-21 21:51:02 +00004047 return;
4048 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00004049
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004050 if (TII->isMIMG(MI)) {
4051 unsigned VReg = MI.getOperand(0).getReg();
Changpeng Fang8236fe12016-11-14 18:33:18 +00004052 const TargetRegisterClass *RC = MRI.getRegClass(VReg);
4053 // TODO: Need mapping tables to handle other cases (register classes).
4054 if (RC != &AMDGPU::VReg_128RegClass)
4055 return;
4056
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004057 unsigned DmaskIdx = MI.getNumOperands() == 12 ? 3 : 4;
4058 unsigned Writemask = MI.getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004059 unsigned BitsSet = 0;
4060 for (unsigned i = 0; i < 4; ++i)
4061 BitsSet += Writemask & (1 << i) ? 1 : 0;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004062 switch (BitsSet) {
4063 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00004064 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004065 case 2: RC = &AMDGPU::VReg_64RegClass; break;
4066 case 3: RC = &AMDGPU::VReg_96RegClass; break;
4067 }
4068
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004069 unsigned NewOpcode = TII->getMaskedMIMGOp(MI.getOpcode(), BitsSet);
4070 MI.setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004071 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00004072 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00004073 }
4074
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004075 // Replace unused atomics with the no return version.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004076 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI.getOpcode());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004077 if (NoRetAtomicOp != -1) {
4078 if (!Node->hasAnyUseOfValue(0)) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004079 MI.setDesc(TII->get(NoRetAtomicOp));
4080 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00004081 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004082 }
4083
Tom Stellard354a43c2016-04-01 18:27:37 +00004084 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
4085 // instruction, because the return type of these instructions is a vec2 of
4086 // the memory type, so it can be tied to the input operand.
4087 // This means these instructions always have a use, so we need to add a
4088 // special case to check if the atomic has only one extract_subreg use,
4089 // which itself has no uses.
4090 if ((Node->hasNUsesOfValue(1, 0) &&
Nicolai Haehnle750082d2016-04-15 14:42:36 +00004091 Node->use_begin()->isMachineOpcode() &&
Tom Stellard354a43c2016-04-01 18:27:37 +00004092 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
4093 !Node->use_begin()->hasAnyUseOfValue(0))) {
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004094 unsigned Def = MI.getOperand(0).getReg();
Tom Stellard354a43c2016-04-01 18:27:37 +00004095
4096 // Change this into a noret atomic.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004097 MI.setDesc(TII->get(NoRetAtomicOp));
4098 MI.RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00004099
4100 // If we only remove the def operand from the atomic instruction, the
4101 // extract_subreg will be left with a use of a vreg without a def.
4102 // So we need to insert an implicit_def to avoid machine verifier
4103 // errors.
Duncan P. N. Exon Smithe4f5e4f2016-06-30 22:52:52 +00004104 BuildMI(*MI.getParent(), MI, MI.getDebugLoc(),
Tom Stellard354a43c2016-04-01 18:27:37 +00004105 TII->get(AMDGPU::IMPLICIT_DEF), Def);
4106 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00004107 return;
4108 }
Christian Konig8b1ed282013-04-10 08:39:16 +00004109}
Tom Stellard0518ff82013-06-03 17:39:58 +00004110
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004111static SDValue buildSMovImm32(SelectionDAG &DAG, const SDLoc &DL,
4112 uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004113 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00004114 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
4115}
4116
4117MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004118 const SDLoc &DL,
Matt Arsenault485defe2014-11-05 19:01:17 +00004119 SDValue Ptr) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +00004120 const SIInstrInfo *TII = getSubtarget()->getInstrInfo();
Matt Arsenault485defe2014-11-05 19:01:17 +00004121
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004122 // Build the half of the subregister with the constants before building the
4123 // full 128-bit register. If we are building multiple resource descriptors,
4124 // this will allow CSEing of the 2-component register.
4125 const SDValue Ops0[] = {
4126 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
4127 buildSMovImm32(DAG, DL, 0),
4128 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
4129 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
4130 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
4131 };
Matt Arsenault485defe2014-11-05 19:01:17 +00004132
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004133 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
4134 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00004135
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004136 // Combine the constants and the pointer.
4137 const SDValue Ops1[] = {
4138 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
4139 Ptr,
4140 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
4141 SubRegHi,
4142 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
4143 };
Matt Arsenault485defe2014-11-05 19:01:17 +00004144
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00004145 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00004146}
4147
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004148/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00004149/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
4150/// of the resource descriptor) to create an offset, which is added to
4151/// the resource pointer.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00004152MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG, const SDLoc &DL,
4153 SDValue Ptr, uint32_t RsrcDword1,
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004154 uint64_t RsrcDword2And3) const {
4155 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
4156 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
4157 if (RsrcDword1) {
4158 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004159 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
4160 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004161 }
4162
4163 SDValue DataLo = buildSMovImm32(DAG, DL,
4164 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
4165 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
4166
4167 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004168 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004169 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004170 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004171 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004172 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004173 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004174 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004175 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00004176 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00004177 };
4178
4179 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
4180}
4181
Tom Stellard94593ee2013-06-03 17:40:18 +00004182SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
4183 const TargetRegisterClass *RC,
4184 unsigned Reg, EVT VT) const {
4185 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
4186
4187 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
4188 cast<RegisterSDNode>(VReg)->getReg(), VT);
4189}
Tom Stellardd7e6f132015-04-08 01:09:26 +00004190
4191//===----------------------------------------------------------------------===//
4192// SI Inline Assembly Support
4193//===----------------------------------------------------------------------===//
4194
4195std::pair<unsigned, const TargetRegisterClass *>
4196SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00004197 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00004198 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004199
4200 if (Constraint.size() == 1) {
4201 switch (Constraint[0]) {
4202 case 's':
4203 case 'r':
4204 switch (VT.getSizeInBits()) {
4205 default:
4206 return std::make_pair(0U, nullptr);
4207 case 32:
Matt Arsenaulta609e2d2016-08-30 20:50:08 +00004208 return std::make_pair(0U, &AMDGPU::SReg_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004209 case 64:
4210 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
4211 case 128:
4212 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
4213 case 256:
4214 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
4215 }
4216
4217 case 'v':
4218 switch (VT.getSizeInBits()) {
4219 default:
4220 return std::make_pair(0U, nullptr);
4221 case 32:
4222 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
4223 case 64:
4224 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
4225 case 96:
4226 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
4227 case 128:
4228 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
4229 case 256:
4230 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
4231 case 512:
4232 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
4233 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00004234 }
4235 }
4236
4237 if (Constraint.size() > 1) {
4238 const TargetRegisterClass *RC = nullptr;
4239 if (Constraint[1] == 'v') {
4240 RC = &AMDGPU::VGPR_32RegClass;
4241 } else if (Constraint[1] == 's') {
4242 RC = &AMDGPU::SGPR_32RegClass;
4243 }
4244
4245 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00004246 uint32_t Idx;
4247 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
4248 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00004249 return std::make_pair(RC->getRegister(Idx), RC);
4250 }
4251 }
4252 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
4253}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00004254
4255SITargetLowering::ConstraintType
4256SITargetLowering::getConstraintType(StringRef Constraint) const {
4257 if (Constraint.size() == 1) {
4258 switch (Constraint[0]) {
4259 default: break;
4260 case 's':
4261 case 'v':
4262 return C_RegisterClass;
4263 }
4264 }
4265 return TargetLowering::getConstraintType(Constraint);
4266}