blob: 52880a282d91819378ffaebbde1f7a04fe58f5ec [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief Custom DAG lowering for SI
12//
13//===----------------------------------------------------------------------===//
14
NAKAMURA Takumi45e0a832014-07-20 11:15:07 +000015#ifdef _MSC_VER
16// Provide M_PI.
17#define _USE_MATH_DEFINES
18#include <cmath>
19#endif
20
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "SIISelLowering.h"
Christian Konig99ee0f42013-03-07 09:04:14 +000022#include "AMDGPU.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000023#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000024#include "AMDGPUSubtarget.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"
Oliver Stannard7e7d9832016-02-02 13:52:43 +000034#include "llvm/IR/DiagnosticInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000035#include "llvm/IR/Function.h"
Matt Arsenault364a6742014-06-11 17:50:44 +000036#include "llvm/ADT/SmallString.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000037
38using namespace llvm;
39
Tom Stellardf110f8f2016-04-14 16:27:03 +000040static unsigned findFirstFreeSGPR(CCState &CCInfo) {
41 unsigned NumSGPRs = AMDGPU::SGPR_32RegClass.getNumRegs();
42 for (unsigned Reg = 0; Reg < NumSGPRs; ++Reg) {
43 if (!CCInfo.isAllocated(AMDGPU::SGPR0 + Reg)) {
44 return AMDGPU::SGPR0 + Reg;
45 }
46 }
47 llvm_unreachable("Cannot allocate sgpr");
48}
49
Eric Christopher7792e322015-01-30 23:24:40 +000050SITargetLowering::SITargetLowering(TargetMachine &TM,
51 const AMDGPUSubtarget &STI)
52 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000053 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000054 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000055
Tom Stellard334b29c2014-04-17 21:00:09 +000056 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000057 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000058
Tom Stellard436780b2014-05-15 14:41:57 +000059 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
60 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
61 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000062
Matt Arsenault61001bb2015-11-25 19:58:34 +000063 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
64 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
65
Tom Stellard436780b2014-05-15 14:41:57 +000066 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
67 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000068
Tom Stellardf0a21072014-11-18 20:39:39 +000069 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000070 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
71
Tom Stellardf0a21072014-11-18 20:39:39 +000072 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000073 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000074
Eric Christopher23a3a7c2015-02-26 00:00:24 +000075 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000076
Christian Konig2989ffc2013-03-18 11:34:16 +000077 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
78 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
79 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
80 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
81
Tom Stellard75aadc22012-12-11 21:25:42 +000082 setOperationAction(ISD::ADD, MVT::i32, Legal);
Matt Arsenaulte8d21462013-11-18 20:09:40 +000083 setOperationAction(ISD::ADDC, MVT::i32, Legal);
84 setOperationAction(ISD::ADDE, MVT::i32, Legal);
Matt Arsenaultb8b51532014-06-23 18:00:38 +000085 setOperationAction(ISD::SUBC, MVT::i32, Legal);
86 setOperationAction(ISD::SUBE, MVT::i32, Legal);
Aaron Watrydaabb202013-06-25 13:55:52 +000087
Matt Arsenaultad14ce82014-07-19 18:44:39 +000088 setOperationAction(ISD::FSIN, MVT::f32, Custom);
89 setOperationAction(ISD::FCOS, MVT::f32, Custom);
90
Matt Arsenault7c936902014-10-21 23:01:01 +000091 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
92 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
93
Tom Stellard35bb18c2013-08-26 15:06:04 +000094 // We need to custom lower vector stores from local memory
Tom Stellard35bb18c2013-08-26 15:06:04 +000095 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000096 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
97 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
98
99 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
100 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000101
Tom Stellard1c8788e2014-03-07 20:12:33 +0000102 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +0000103 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
104
Tom Stellard0ec134f2014-02-04 17:18:40 +0000105 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +0000106 setOperationAction(ISD::SELECT, MVT::f64, Promote);
107 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000108
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000109 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
110 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
111 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
112 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000113
Tom Stellardd1efda82016-01-20 21:48:24 +0000114 setOperationAction(ISD::SETCC, MVT::i1, Promote);
Tom Stellard83747202013-07-18 21:43:53 +0000115 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
116 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
117
Matt Arsenaulte306a322014-10-21 16:25:08 +0000118 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
Matt Arsenaultd0792852015-12-14 17:25:38 +0000119 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000120
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
124
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000125 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
127 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
128
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000129 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000130 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
131 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
132
Matt Arsenault94812212014-11-14 18:18:16 +0000133 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000134 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
135
Tom Stellard94593ee2013-06-03 17:40:18 +0000136 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000137 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
138 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
139 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000140
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000141 setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
142
Tom Stellardafcf12f2013-09-12 02:55:14 +0000143 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000144 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000145 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
146 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
147 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
148 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000149
Matt Arsenault61738cb2016-02-27 08:53:46 +0000150 // On SI this is s_memtime and s_memrealtime on VI.
151 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
152
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000153 for (MVT VT : MVT::integer_valuetypes()) {
Matt Arsenaultbd223422015-01-14 01:35:17 +0000154 if (VT == MVT::i64)
155 continue;
156
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000157 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000158 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
159 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000160 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000161
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000162 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000163 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
164 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000165 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000166
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000167 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000168 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
169 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000170 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
171 }
172
173 for (MVT VT : MVT::integer_vector_valuetypes()) {
174 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
175 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
176 }
177
178 for (MVT VT : MVT::fp_valuetypes())
179 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000180
Matt Arsenault61001bb2015-11-25 19:58:34 +0000181 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
182 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
183
Matt Arsenault6f243792013-09-05 19:41:10 +0000184 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000185 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
Matt Arsenaulte1ce3442015-07-31 04:12:04 +0000186 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000187 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000188
Matt Arsenault61001bb2015-11-25 19:58:34 +0000189
190 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
191
192 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
193 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
194
Matt Arsenault470acd82014-04-15 22:28:39 +0000195 setOperationAction(ISD::LOAD, MVT::i1, Custom);
196
Matt Arsenault61001bb2015-11-25 19:58:34 +0000197 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
198 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
199
200 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
201 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
202
203 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
204
Tom Stellardfd155822013-08-26 15:05:36 +0000205 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000206 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000207 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000208
Tom Stellard5f337882014-04-29 23:12:43 +0000209 // These should use UDIVREM, so set them to expand
210 setOperationAction(ISD::UDIV, MVT::i64, Expand);
211 setOperationAction(ISD::UREM, MVT::i64, Expand);
212
Matt Arsenault0d89e842014-07-15 21:44:37 +0000213 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
214 setOperationAction(ISD::SELECT, MVT::i1, Promote);
215
Matt Arsenault61001bb2015-11-25 19:58:34 +0000216 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
217
218
219 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
220
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000221 // We only support LOAD/STORE and vector manipulation ops for vectors
222 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000223 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000224 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
225 switch(Op) {
226 case ISD::LOAD:
227 case ISD::STORE:
228 case ISD::BUILD_VECTOR:
229 case ISD::BITCAST:
230 case ISD::EXTRACT_VECTOR_ELT:
231 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000232 case ISD::INSERT_SUBVECTOR:
233 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000234 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000235 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000236 case ISD::CONCAT_VECTORS:
237 setOperationAction(Op, VT, Custom);
238 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000239 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000240 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000241 break;
242 }
243 }
244 }
245
Matt Arsenault61001bb2015-11-25 19:58:34 +0000246 // Most operations are naturally 32-bit vector operations. We only support
247 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
248 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
249 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
250 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
251
252 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
253 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
254
255 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
256 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
257
258 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
259 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
260 }
261
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000262 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
263 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
264 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000265 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000266 }
267
Marek Olsak7d777282015-03-24 13:40:15 +0000268 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000269 setOperationAction(ISD::FDIV, MVT::f32, Custom);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +0000270 setOperationAction(ISD::FDIV, MVT::f64, Custom);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000271
Tom Stellard354a43c2016-04-01 18:27:37 +0000272 // BUFFER/FLAT_ATOMIC_CMP_SWAP on GCN GPUs needs input marshalling,
273 // and output demarshalling
274 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
275 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
276
277 // We can't return success/failure, only the old value,
278 // let LLVM add the comparison
279 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Expand);
280 setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Expand);
281
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000282 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000283 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000284 setTargetDAGCombine(ISD::FMINNUM);
285 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000286 setTargetDAGCombine(ISD::SMIN);
287 setTargetDAGCombine(ISD::SMAX);
288 setTargetDAGCombine(ISD::UMIN);
289 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000290 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000291 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000292 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000293 setTargetDAGCombine(ISD::UINT_TO_FP);
Matt Arsenault9cd90712016-04-14 01:42:16 +0000294 setTargetDAGCombine(ISD::FCANONICALIZE);
Matt Arsenault364a6742014-06-11 17:50:44 +0000295
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000296 // All memory operations. Some folding on the pointer operand is done to help
297 // matching the constant offsets in the addressing modes.
298 setTargetDAGCombine(ISD::LOAD);
299 setTargetDAGCombine(ISD::STORE);
300 setTargetDAGCombine(ISD::ATOMIC_LOAD);
301 setTargetDAGCombine(ISD::ATOMIC_STORE);
302 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
303 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
304 setTargetDAGCombine(ISD::ATOMIC_SWAP);
305 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
306 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
307 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
308 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
309 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
310 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
311 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
312 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
313 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
314 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
315
Christian Konigeecebd02013-03-26 14:04:02 +0000316 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000317}
318
Tom Stellard0125f2a2013-06-25 02:39:35 +0000319//===----------------------------------------------------------------------===//
320// TargetLowering queries
321//===----------------------------------------------------------------------===//
322
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +0000323bool SITargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info,
324 const CallInst &CI,
325 unsigned IntrID) const {
326 switch (IntrID) {
327 case Intrinsic::amdgcn_atomic_inc:
328 case Intrinsic::amdgcn_atomic_dec:
329 Info.opc = ISD::INTRINSIC_W_CHAIN;
330 Info.memVT = MVT::getVT(CI.getType());
331 Info.ptrVal = CI.getOperand(0);
332 Info.align = 0;
333 Info.vol = false;
334 Info.readMem = true;
335 Info.writeMem = true;
336 return true;
337 default:
338 return false;
339 }
340}
341
Matt Arsenaulte306a322014-10-21 16:25:08 +0000342bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
343 EVT) const {
344 // SI has some legal vector types, but no legal vector operations. Say no
345 // shuffles are legal in order to prefer scalarizing some vector operations.
346 return false;
347}
348
Tom Stellard70580f82015-07-20 14:28:41 +0000349bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
350 // Flat instructions do not have offsets, and only have the register
351 // address.
352 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
353}
354
Matt Arsenault711b3902015-08-07 20:18:34 +0000355bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
356 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
357 // additionally can do r + r + i with addr64. 32-bit has more addressing
358 // mode options. Depending on the resource constant, it can also do
359 // (i64 r0) + (i32 r1) * (i14 i).
360 //
361 // Private arrays end up using a scratch buffer most of the time, so also
362 // assume those use MUBUF instructions. Scratch loads / stores are currently
363 // implemented as mubuf instructions with offen bit set, so slightly
364 // different than the normal addr64.
365 if (!isUInt<12>(AM.BaseOffs))
366 return false;
367
368 // FIXME: Since we can split immediate into soffset and immediate offset,
369 // would it make sense to allow any immediate?
370
371 switch (AM.Scale) {
372 case 0: // r + i or just i, depending on HasBaseReg.
373 return true;
374 case 1:
375 return true; // We have r + r or r + i.
376 case 2:
377 if (AM.HasBaseReg) {
378 // Reject 2 * r + r.
379 return false;
380 }
381
382 // Allow 2 * r as r + r
383 // Or 2 * r + i is allowed as r + r + i.
384 return true;
385 default: // Don't allow n * r
386 return false;
387 }
388}
389
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000390bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
391 const AddrMode &AM, Type *Ty,
392 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000393 // No global is ever allowed as a base.
394 if (AM.BaseGV)
395 return false;
396
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000397 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000398 case AMDGPUAS::GLOBAL_ADDRESS: {
Tom Stellard70580f82015-07-20 14:28:41 +0000399 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
400 // Assume the we will use FLAT for all global memory accesses
401 // on VI.
402 // FIXME: This assumption is currently wrong. On VI we still use
403 // MUBUF instructions for the r + i addressing mode. As currently
404 // implemented, the MUBUF instructions only work on buffer < 4GB.
405 // It may be possible to support > 4GB buffers with MUBUF instructions,
406 // by setting the stride value in the resource descriptor which would
407 // increase the size limit to (stride * 4GB). However, this is risky,
408 // because it has never been validated.
409 return isLegalFlatAddressingMode(AM);
410 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000411
Matt Arsenault711b3902015-08-07 20:18:34 +0000412 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000413 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000414 case AMDGPUAS::CONSTANT_ADDRESS: {
415 // If the offset isn't a multiple of 4, it probably isn't going to be
416 // correctly aligned.
417 if (AM.BaseOffs % 4 != 0)
418 return isLegalMUBUFAddressingMode(AM);
419
420 // There are no SMRD extloads, so if we have to do a small type access we
421 // will use a MUBUF load.
422 // FIXME?: We also need to do this if unaligned, but we don't know the
423 // alignment here.
424 if (DL.getTypeStoreSize(Ty) < 4)
425 return isLegalMUBUFAddressingMode(AM);
426
427 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
428 // SMRD instructions have an 8-bit, dword offset on SI.
429 if (!isUInt<8>(AM.BaseOffs / 4))
430 return false;
431 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) {
432 // On CI+, this can also be a 32-bit literal constant offset. If it fits
433 // in 8-bits, it can use a smaller encoding.
434 if (!isUInt<32>(AM.BaseOffs / 4))
435 return false;
436 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) {
437 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
438 if (!isUInt<20>(AM.BaseOffs))
439 return false;
440 } else
441 llvm_unreachable("unhandled generation");
442
443 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
444 return true;
445
446 if (AM.Scale == 1 && AM.HasBaseReg)
447 return true;
448
449 return false;
450 }
451
452 case AMDGPUAS::PRIVATE_ADDRESS:
453 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
454 return isLegalMUBUFAddressingMode(AM);
455
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000456 case AMDGPUAS::LOCAL_ADDRESS:
457 case AMDGPUAS::REGION_ADDRESS: {
458 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
459 // field.
460 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
461 // an 8-bit dword offset but we don't know the alignment here.
462 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000463 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000464
465 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
466 return true;
467
468 if (AM.Scale == 1 && AM.HasBaseReg)
469 return true;
470
Matt Arsenault5015a892014-08-15 17:17:07 +0000471 return false;
472 }
Tom Stellard70580f82015-07-20 14:28:41 +0000473 case AMDGPUAS::FLAT_ADDRESS:
474 return isLegalFlatAddressingMode(AM);
475
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000476 default:
477 llvm_unreachable("unhandled address space");
478 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000479}
480
Matt Arsenaulte6986632015-01-14 01:35:22 +0000481bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000482 unsigned AddrSpace,
483 unsigned Align,
484 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000485 if (IsFast)
486 *IsFast = false;
487
Matt Arsenault1018c892014-04-24 17:08:26 +0000488 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
489 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000490 if (!VT.isSimple() || VT == MVT::Other)
491 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000492
Tom Stellardc6b299c2015-02-02 18:02:28 +0000493 // TODO - CI+ supports unaligned memory accesses, but this requires driver
494 // support.
Matt Arsenault1018c892014-04-24 17:08:26 +0000495
Matt Arsenault1018c892014-04-24 17:08:26 +0000496 // XXX - The only mention I see of this in the ISA manual is for LDS direct
497 // reads the "byte address and must be dword aligned". Is it also true for the
498 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000499 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
500 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
501 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
502 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000503 bool AlignedBy4 = (Align % 4 == 0);
504 if (IsFast)
505 *IsFast = AlignedBy4;
506 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000507 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000508
Tom Stellard33e64c62015-02-04 20:49:52 +0000509 // Smaller than dword value must be aligned.
510 // FIXME: This should be allowed on CI+
511 if (VT.bitsLT(MVT::i32))
512 return false;
513
Matt Arsenault1018c892014-04-24 17:08:26 +0000514 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
515 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000516 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000517 if (IsFast)
518 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000519
520 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000521}
522
Matt Arsenault46645fa2014-07-28 17:49:26 +0000523EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
524 unsigned SrcAlign, bool IsMemset,
525 bool ZeroMemset,
526 bool MemcpyStrSrc,
527 MachineFunction &MF) const {
528 // FIXME: Should account for address space here.
529
530 // The default fallback uses the private pointer size as a guess for a type to
531 // use. Make sure we switch these to 64-bit accesses.
532
533 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
534 return MVT::v4i32;
535
536 if (Size >= 8 && DstAlign >= 4)
537 return MVT::v2i32;
538
539 // Use the default.
540 return MVT::Other;
541}
542
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000543static bool isFlatGlobalAddrSpace(unsigned AS) {
544 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
545 AS == AMDGPUAS::FLAT_ADDRESS ||
546 AS == AMDGPUAS::CONSTANT_ADDRESS;
547}
548
549bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
550 unsigned DestAS) const {
551 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
552}
553
Tom Stellarda6f24c62015-12-15 20:55:55 +0000554
555bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
556 const MemSDNode *MemNode = cast<MemSDNode>(N);
557 const Value *Ptr = MemNode->getMemOperand()->getValue();
558
559 // UndefValue means this is a load of a kernel input. These are uniform.
560 // Sometimes LDS instructions have constant pointers
561 if (isa<UndefValue>(Ptr) || isa<Argument>(Ptr) || isa<Constant>(Ptr) ||
562 isa<GlobalValue>(Ptr))
563 return true;
564
565 const Instruction *I = dyn_cast_or_null<Instruction>(Ptr);
566 return I && I->getMetadata("amdgpu.uniform");
567}
568
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000569TargetLoweringBase::LegalizeTypeAction
570SITargetLowering::getPreferredVectorAction(EVT VT) const {
571 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
572 return TypeSplitVector;
573
574 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000575}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000576
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000577bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
578 Type *Ty) const {
Eric Christopher7792e322015-01-30 23:24:40 +0000579 const SIInstrInfo *TII =
580 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000581 return TII->isInlineConstant(Imm);
582}
583
Tom Stellard2e045bb2016-01-20 00:13:22 +0000584bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
585
586 // SimplifySetCC uses this function to determine whether or not it should
587 // create setcc with i1 operands. We don't have instructions for i1 setcc.
588 if (VT == MVT::i1 && Op == ISD::SETCC)
589 return false;
590
591 return TargetLowering::isTypeDesirableForOp(Op, VT);
592}
593
Tom Stellardaf775432013-10-23 00:44:32 +0000594SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000595 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000596 unsigned Offset, bool Signed) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000597 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000598 MachineFunction &MF = DAG.getMachineFunction();
599 const SIRegisterInfo *TRI =
600 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
Matt Arsenaultac234b62015-11-30 21:15:57 +0000601 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000602
Matt Arsenault86033ca2014-07-28 17:31:39 +0000603 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
604
605 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000606 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000607 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000608 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
609 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
610 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
611 DAG.getConstant(Offset, SL, PtrVT));
Mehdi Amini44ede332015-07-09 02:09:04 +0000612 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000613 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
614
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000615 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000616
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000617 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000618 if (MemVT.isFloatingPoint())
619 ExtTy = ISD::EXTLOAD;
620
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000621 return DAG.getLoad(ISD::UNINDEXED, ExtTy,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000622 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
623 false, // isVolatile
624 true, // isNonTemporal
625 true, // isInvariant
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000626 Align); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000627}
628
Christian Konig2c8f6d52013-03-07 09:03:52 +0000629SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000630 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
631 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
632 SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardec2e43c2014-09-22 15:35:29 +0000633 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000634 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000635
636 MachineFunction &MF = DAG.getMachineFunction();
637 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000638 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000639 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000640
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000641 if (Subtarget->isAmdHsaOS() && AMDGPU::isShader(CallConv)) {
Matt Arsenaultd48da142015-11-02 23:23:02 +0000642 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000643 DiagnosticInfoUnsupported NoGraphicsHSA(
644 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000645 DAG.getContext()->diagnose(NoGraphicsHSA);
646 return SDValue();
647 }
648
Christian Konig2c8f6d52013-03-07 09:03:52 +0000649 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000650 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000651
652 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000653 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000654
655 // First check if it's a PS input addr
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000656 if (CallConv == CallingConv::AMDGPU_PS && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000657 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000658
Marek Olsakfccabaf2016-01-13 11:45:36 +0000659 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000660 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000661 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000662 ++PSInputNum;
663 continue;
664 }
665
Marek Olsakfccabaf2016-01-13 11:45:36 +0000666 Info->markPSInputAllocated(PSInputNum);
667 if (Arg.Used)
668 Info->PSInputEna |= 1 << PSInputNum;
669
670 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000671 }
672
673 // Second split vertices into their elements
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000674 if (AMDGPU::isShader(CallConv) &&
675 Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000676 ISD::InputArg NewArg = Arg;
677 NewArg.Flags.setSplit();
678 NewArg.VT = Arg.VT.getVectorElementType();
679
680 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
681 // three or five element vertex only needs three or five registers,
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000682 // NOT four or eight.
Andrew Trick05938a52015-02-16 18:10:47 +0000683 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000684 unsigned NumElements = ParamType->getVectorNumElements();
685
686 for (unsigned j = 0; j != NumElements; ++j) {
687 Splits.push_back(NewArg);
688 NewArg.PartOffset += NewArg.VT.getStoreSize();
689 }
690
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000691 } else if (AMDGPU::isShader(CallConv)) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000692 Splits.push_back(Arg);
693 }
694 }
695
696 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000697 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
698 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000699
Christian Konig99ee0f42013-03-07 09:04:14 +0000700 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000701 //
702 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
703 // PSInputAddr, the user wants to enable some bits after the compilation
704 // based on run-time states. Since we can't know what the final PSInputEna
705 // will look like, so we shouldn't do anything here and the user should take
706 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000707 //
708 // Otherwise, the following restrictions apply:
709 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
710 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
711 // enabled too.
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000712 if (CallConv == CallingConv::AMDGPU_PS &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000713 ((Info->getPSInputAddr() & 0x7F) == 0 ||
714 ((Info->getPSInputAddr() & 0xF) == 0 &&
715 Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000716 CCInfo.AllocateReg(AMDGPU::VGPR0);
717 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000718 Info->markPSInputAllocated(0);
719 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000720 }
721
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000722 if (!AMDGPU::isShader(CallConv)) {
Tom Stellardaf775432013-10-23 00:44:32 +0000723 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
724 Splits);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000725
726 assert(Info->hasWorkGroupIDX() && Info->hasWorkItemIDX());
727 } else {
728 assert(!Info->hasPrivateSegmentBuffer() && !Info->hasDispatchPtr() &&
729 !Info->hasKernargSegmentPtr() && !Info->hasFlatScratchInit() &&
730 !Info->hasWorkGroupIDX() && !Info->hasWorkGroupIDY() &&
731 !Info->hasWorkGroupIDZ() && !Info->hasWorkGroupInfo() &&
732 !Info->hasWorkItemIDX() && !Info->hasWorkItemIDY() &&
733 !Info->hasWorkItemIDZ());
Tom Stellardaf775432013-10-23 00:44:32 +0000734 }
735
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000736 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
737 if (Info->hasPrivateSegmentBuffer()) {
738 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
739 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
740 CCInfo.AllocateReg(PrivateSegmentBufferReg);
741 }
742
743 if (Info->hasDispatchPtr()) {
744 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
745 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
746 CCInfo.AllocateReg(DispatchPtrReg);
747 }
748
749 if (Info->hasKernargSegmentPtr()) {
750 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
751 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
752 CCInfo.AllocateReg(InputPtrReg);
753 }
754
Matt Arsenault296b8492016-02-12 06:31:30 +0000755 if (Info->hasFlatScratchInit()) {
756 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
757 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass);
758 CCInfo.AllocateReg(FlatScratchInitReg);
759 }
760
Christian Konig2c8f6d52013-03-07 09:03:52 +0000761 AnalyzeFormalArguments(CCInfo, Splits);
762
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000763 SmallVector<SDValue, 16> Chains;
764
Christian Konig2c8f6d52013-03-07 09:03:52 +0000765 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
766
Christian Konigb7be72d2013-05-17 09:46:48 +0000767 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000768 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000769 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000770 continue;
771 }
772
Christian Konig2c8f6d52013-03-07 09:03:52 +0000773 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000774 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000775
776 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000777 VT = Ins[i].VT;
778 EVT MemVT = Splits[i].VT;
Tom Stellardb5798b02015-06-26 21:15:03 +0000779 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
780 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000781 // The first 36 bytes of the input buffer contains information about
782 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000783 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000784 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000785 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000786
Craig Toppere3dcce92015-08-01 22:20:21 +0000787 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000788 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000789 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
790 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
791 // On SI local pointers are just offsets into LDS, so they are always
792 // less than 16-bits. On CI and newer they could potentially be
793 // real pointers, so we can't guarantee their size.
794 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
795 DAG.getValueType(MVT::i16));
796 }
797
Tom Stellarded882c22013-06-03 17:40:11 +0000798 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000799 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000800 continue;
801 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000802 assert(VA.isRegLoc() && "Parameter must be in a register!");
803
804 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000805
806 if (VT == MVT::i64) {
807 // For now assume it is a pointer
808 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
809 &AMDGPU::SReg_64RegClass);
810 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000811 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
812 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000813 continue;
814 }
815
816 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
817
818 Reg = MF.addLiveIn(Reg, RC);
819 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
820
Christian Konig2c8f6d52013-03-07 09:03:52 +0000821 if (Arg.VT.isVector()) {
822
823 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000824 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000825 unsigned NumElements = ParamType->getVectorNumElements();
826
827 SmallVector<SDValue, 4> Regs;
828 Regs.push_back(Val);
829 for (unsigned j = 1; j != NumElements; ++j) {
830 Reg = ArgLocs[ArgIdx++].getLocReg();
831 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000832
833 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
834 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000835 }
836
837 // Fill up the missing vector elements
838 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000839 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000840
Craig Topper48d114b2014-04-26 18:35:24 +0000841 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000842 continue;
843 }
844
845 InVals.push_back(Val);
846 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000847
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000848 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
849 // these from the dispatch pointer.
850
851 // Start adding system SGPRs.
852 if (Info->hasWorkGroupIDX()) {
853 unsigned Reg = Info->addWorkGroupIDX();
854 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
855 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000856 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000857
858 if (Info->hasWorkGroupIDY()) {
859 unsigned Reg = Info->addWorkGroupIDY();
860 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
861 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000862 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000863
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000864 if (Info->hasWorkGroupIDZ()) {
865 unsigned Reg = Info->addWorkGroupIDZ();
866 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
867 CCInfo.AllocateReg(Reg);
868 }
869
870 if (Info->hasWorkGroupInfo()) {
871 unsigned Reg = Info->addWorkGroupInfo();
872 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
873 CCInfo.AllocateReg(Reg);
874 }
875
876 if (Info->hasPrivateSegmentWaveByteOffset()) {
877 // Scratch wave offset passed in system SGPR.
Tom Stellardf110f8f2016-04-14 16:27:03 +0000878 unsigned PrivateSegmentWaveByteOffsetReg;
879
880 if (AMDGPU::isShader(CallConv)) {
881 PrivateSegmentWaveByteOffsetReg = findFirstFreeSGPR(CCInfo);
882 Info->setPrivateSegmentWaveByteOffset(PrivateSegmentWaveByteOffsetReg);
883 } else
884 PrivateSegmentWaveByteOffsetReg = Info->addPrivateSegmentWaveByteOffset();
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000885
886 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
887 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
888 }
889
890 // Now that we've figured out where the scratch register inputs are, see if
891 // should reserve the arguments and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000892 bool HasStackObjects = MF.getFrameInfo()->hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +0000893 // Record that we know we have non-spill stack objects so we don't need to
894 // check all stack objects later.
895 if (HasStackObjects)
896 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000897
898 if (ST.isAmdHsaOS()) {
899 // TODO: Assume we will spill without optimizations.
900 if (HasStackObjects) {
901 // If we have stack objects, we unquestionably need the private buffer
902 // resource. For the HSA ABI, this will be the first 4 user SGPR
903 // inputs. We can reserve those and use them directly.
904
905 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
906 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
907 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
908
909 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
910 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
911 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
912 } else {
913 unsigned ReservedBufferReg
914 = TRI->reservedPrivateSegmentBufferReg(MF);
915 unsigned ReservedOffsetReg
916 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
917
918 // We tentatively reserve the last registers (skipping the last two
919 // which may contain VCC). After register allocation, we'll replace
920 // these with the ones immediately after those which were really
921 // allocated. In the prologue copies will be inserted from the argument
922 // to these reserved registers.
923 Info->setScratchRSrcReg(ReservedBufferReg);
924 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
925 }
926 } else {
927 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
928
929 // Without HSA, relocations are used for the scratch pointer and the
930 // buffer resource setup is always inserted in the prologue. Scratch wave
931 // offset is still in an input SGPR.
932 Info->setScratchRSrcReg(ReservedBufferReg);
933
934 if (HasStackObjects) {
935 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
936 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
937 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
938 } else {
939 unsigned ReservedOffsetReg
940 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
941 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
942 }
943 }
944
945 if (Info->hasWorkItemIDX()) {
946 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
947 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
948 CCInfo.AllocateReg(Reg);
Tom Stellardf110f8f2016-04-14 16:27:03 +0000949 }
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000950
951 if (Info->hasWorkItemIDY()) {
952 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
953 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
954 CCInfo.AllocateReg(Reg);
955 }
956
957 if (Info->hasWorkItemIDZ()) {
958 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
959 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
960 CCInfo.AllocateReg(Reg);
961 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000962
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000963 if (Chains.empty())
964 return Chain;
965
966 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000967}
968
Marek Olsak8a0f3352016-01-13 17:23:04 +0000969SDValue SITargetLowering::LowerReturn(SDValue Chain,
970 CallingConv::ID CallConv,
971 bool isVarArg,
972 const SmallVectorImpl<ISD::OutputArg> &Outs,
973 const SmallVectorImpl<SDValue> &OutVals,
974 SDLoc DL, SelectionDAG &DAG) const {
975 MachineFunction &MF = DAG.getMachineFunction();
976 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
977
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000978 if (!AMDGPU::isShader(CallConv))
Marek Olsak8a0f3352016-01-13 17:23:04 +0000979 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
980 OutVals, DL, DAG);
981
Marek Olsak8e9cc632016-01-13 17:23:09 +0000982 Info->setIfReturnsVoid(Outs.size() == 0);
983
Marek Olsak8a0f3352016-01-13 17:23:04 +0000984 SmallVector<ISD::OutputArg, 48> Splits;
985 SmallVector<SDValue, 48> SplitVals;
986
987 // Split vectors into their elements.
988 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
989 const ISD::OutputArg &Out = Outs[i];
990
991 if (Out.VT.isVector()) {
992 MVT VT = Out.VT.getVectorElementType();
993 ISD::OutputArg NewOut = Out;
994 NewOut.Flags.setSplit();
995 NewOut.VT = VT;
996
997 // We want the original number of vector elements here, e.g.
998 // three or five, not four or eight.
999 unsigned NumElements = Out.ArgVT.getVectorNumElements();
1000
1001 for (unsigned j = 0; j != NumElements; ++j) {
1002 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
1003 DAG.getConstant(j, DL, MVT::i32));
1004 SplitVals.push_back(Elem);
1005 Splits.push_back(NewOut);
1006 NewOut.PartOffset += NewOut.VT.getStoreSize();
1007 }
1008 } else {
1009 SplitVals.push_back(OutVals[i]);
1010 Splits.push_back(Out);
1011 }
1012 }
1013
1014 // CCValAssign - represent the assignment of the return value to a location.
1015 SmallVector<CCValAssign, 48> RVLocs;
1016
1017 // CCState - Info about the registers and stack slots.
1018 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
1019 *DAG.getContext());
1020
1021 // Analyze outgoing return values.
1022 AnalyzeReturn(CCInfo, Splits);
1023
1024 SDValue Flag;
1025 SmallVector<SDValue, 48> RetOps;
1026 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1027
1028 // Copy the result values into the output registers.
1029 for (unsigned i = 0, realRVLocIdx = 0;
1030 i != RVLocs.size();
1031 ++i, ++realRVLocIdx) {
1032 CCValAssign &VA = RVLocs[i];
1033 assert(VA.isRegLoc() && "Can only return in registers!");
1034
1035 SDValue Arg = SplitVals[realRVLocIdx];
1036
1037 // Copied from other backends.
1038 switch (VA.getLocInfo()) {
1039 default: llvm_unreachable("Unknown loc info!");
1040 case CCValAssign::Full:
1041 break;
1042 case CCValAssign::BCvt:
1043 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1044 break;
1045 }
1046
1047 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
1048 Flag = Chain.getValue(1);
1049 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
1050 }
1051
1052 // Update chain and glue.
1053 RetOps[0] = Chain;
1054 if (Flag.getNode())
1055 RetOps.push_back(Flag);
1056
1057 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, RetOps);
1058}
1059
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001060unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1061 SelectionDAG &DAG) const {
1062 unsigned Reg = StringSwitch<unsigned>(RegName)
1063 .Case("m0", AMDGPU::M0)
1064 .Case("exec", AMDGPU::EXEC)
1065 .Case("exec_lo", AMDGPU::EXEC_LO)
1066 .Case("exec_hi", AMDGPU::EXEC_HI)
1067 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1068 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1069 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1070 .Default(AMDGPU::NoRegister);
1071
1072 if (Reg == AMDGPU::NoRegister) {
1073 report_fatal_error(Twine("invalid register name \""
1074 + StringRef(RegName) + "\"."));
1075
1076 }
1077
1078 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
1079 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1080 report_fatal_error(Twine("invalid register \""
1081 + StringRef(RegName) + "\" for subtarget."));
1082 }
1083
1084 switch (Reg) {
1085 case AMDGPU::M0:
1086 case AMDGPU::EXEC_LO:
1087 case AMDGPU::EXEC_HI:
1088 case AMDGPU::FLAT_SCR_LO:
1089 case AMDGPU::FLAT_SCR_HI:
1090 if (VT.getSizeInBits() == 32)
1091 return Reg;
1092 break;
1093 case AMDGPU::EXEC:
1094 case AMDGPU::FLAT_SCR:
1095 if (VT.getSizeInBits() == 64)
1096 return Reg;
1097 break;
1098 default:
1099 llvm_unreachable("missing register type checking");
1100 }
1101
1102 report_fatal_error(Twine("invalid type for register \""
1103 + StringRef(RegName) + "\"."));
1104}
1105
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001106MachineBasicBlock *SITargetLowering::EmitInstrWithCustomInserter(
1107 MachineInstr *MI, MachineBasicBlock *BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +00001108 switch (MI->getOpcode()) {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001109 case AMDGPU::SI_INIT_M0: {
1110 const SIInstrInfo *TII =
1111 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
1112 BuildMI(*BB, MI->getIterator(), MI->getDebugLoc(),
1113 TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
1114 .addOperand(MI->getOperand(0));
1115 MI->eraseFromParent();
1116 break;
1117 }
Matt Arsenault20711b72015-02-20 22:10:45 +00001118 case AMDGPU::BRANCH:
1119 return BB;
Changpeng Fang01f60622016-03-15 17:28:44 +00001120 case AMDGPU::GET_GROUPSTATICSIZE: {
1121 const SIInstrInfo *TII =
1122 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
1123 MachineFunction *MF = BB->getParent();
1124 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1125 DebugLoc DL = MI->getDebugLoc();
1126 BuildMI (*BB, MI, DL, TII->get(AMDGPU::S_MOVK_I32))
1127 .addOperand(MI->getOperand(0))
1128 .addImm(MFI->LDSSize);
1129 MI->eraseFromParent();
1130 return BB;
1131 }
1132 default:
1133 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001134 }
1135 return BB;
1136}
1137
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001138bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1139 // This currently forces unfolding various combinations of fsub into fma with
1140 // free fneg'd operands. As long as we have fast FMA (controlled by
1141 // isFMAFasterThanFMulAndFAdd), we should perform these.
1142
1143 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1144 // most of these combines appear to be cycle neutral but save on instruction
1145 // count / code size.
1146 return true;
1147}
1148
Mehdi Amini44ede332015-07-09 02:09:04 +00001149EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1150 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001151 if (!VT.isVector()) {
1152 return MVT::i1;
1153 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001154 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001155}
1156
Mehdi Aminieaabc512015-07-09 15:12:23 +00001157MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001158 return MVT::i32;
1159}
1160
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001161// Answering this is somewhat tricky and depends on the specific device which
1162// have different rates for fma or all f64 operations.
1163//
1164// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1165// regardless of which device (although the number of cycles differs between
1166// devices), so it is always profitable for f64.
1167//
1168// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1169// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1170// which we can always do even without fused FP ops since it returns the same
1171// result as the separate operations and since it is always full
1172// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1173// however does not support denormals, so we do report fma as faster if we have
1174// a fast fma device and require denormals.
1175//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001176bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1177 VT = VT.getScalarType();
1178
1179 if (!VT.isSimple())
1180 return false;
1181
1182 switch (VT.getSimpleVT().SimpleTy) {
1183 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001184 // This is as fast on some subtargets. However, we always have full rate f32
1185 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001186 // which we should prefer over fma. We can't use this if we want to support
1187 // denormals, so only report this in these cases.
1188 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001189 case MVT::f64:
1190 return true;
1191 default:
1192 break;
1193 }
1194
1195 return false;
1196}
1197
Tom Stellard75aadc22012-12-11 21:25:42 +00001198//===----------------------------------------------------------------------===//
1199// Custom DAG Lowering Operations
1200//===----------------------------------------------------------------------===//
1201
1202SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1203 switch (Op.getOpcode()) {
1204 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001205 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001206 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001207 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001208 SDValue Result = LowerLOAD(Op, DAG);
1209 assert((!Result.getNode() ||
1210 Result.getNode()->getNumValues() == 2) &&
1211 "Load should return a value and a chain");
1212 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001213 }
Tom Stellardaf775432013-10-23 00:44:32 +00001214
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001215 case ISD::FSIN:
1216 case ISD::FCOS:
1217 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001218 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001219 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001220 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001221 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001222 case ISD::GlobalAddress: {
1223 MachineFunction &MF = DAG.getMachineFunction();
1224 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1225 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001226 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001227 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001228 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001229 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001230 }
1231 return SDValue();
1232}
1233
Tom Stellardf8794352012-12-19 22:10:31 +00001234/// \brief Helper function for LowerBRCOND
1235static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001236
Tom Stellardf8794352012-12-19 22:10:31 +00001237 SDNode *Parent = Value.getNode();
1238 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1239 I != E; ++I) {
1240
1241 if (I.getUse().get() != Value)
1242 continue;
1243
1244 if (I->getOpcode() == Opcode)
1245 return *I;
1246 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001247 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001248}
1249
Tom Stellardb02094e2014-07-21 15:45:01 +00001250SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
1251
Tom Stellardc98ee202015-07-16 19:40:07 +00001252 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +00001253 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
1254 unsigned FrameIndex = FINode->getIndex();
1255
Matt Arsenault3a619852016-02-27 20:26:57 +00001256 // A FrameIndex node represents a 32-bit offset into scratch memory. If the
1257 // high bit of a frame index offset were to be set, this would mean that it
1258 // represented an offset of ~2GB * 64 = ~128GB from the start of the scratch
1259 // buffer, with 64 being the number of threads per wave.
Tom Stellardc98ee202015-07-16 19:40:07 +00001260 //
Matt Arsenault3a619852016-02-27 20:26:57 +00001261 // The maximum private allocation for the entire GPU is 4G, and we are
1262 // concerned with the largest the index could ever be for an individual
1263 // workitem. This will occur with the minmum dispatch size. If a program
1264 // requires more, the dispatch size will be reduced.
1265 //
1266 // With this limit, we can mark the high bit of the FrameIndex node as known
1267 // zero, which is important, because it means in most situations we can prove
1268 // that values derived from FrameIndex nodes are non-negative. This enables us
1269 // to take advantage of more addressing modes when accessing scratch buffers,
1270 // since for scratch reads/writes, the register offset must always be
1271 // positive.
1272
1273 uint64_t MaxGPUAlloc = UINT64_C(4) * 1024 * 1024 * 1024;
1274
1275 // XXX - It is unclear if partial dispatch works. Assume it works at half wave
1276 // granularity. It is probably a full wave.
1277 uint64_t MinGranularity = 32;
1278
1279 unsigned KnownBits = Log2_64(MaxGPUAlloc / MinGranularity);
1280 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), KnownBits);
Tom Stellardc98ee202015-07-16 19:40:07 +00001281
1282 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
Tom Stellardc98ee202015-07-16 19:40:07 +00001283 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
Matt Arsenault3a619852016-02-27 20:26:57 +00001284 DAG.getValueType(ExtVT));
Tom Stellardb02094e2014-07-21 15:45:01 +00001285}
1286
Tom Stellardbc4497b2016-02-12 23:45:29 +00001287bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault16f48d72016-02-13 00:36:10 +00001288 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN)
Tom Stellardbc4497b2016-02-12 23:45:29 +00001289 return false;
1290
1291 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1292 default: return false;
1293 case AMDGPUIntrinsic::amdgcn_if:
1294 case AMDGPUIntrinsic::amdgcn_else:
1295 case AMDGPUIntrinsic::amdgcn_break:
1296 case AMDGPUIntrinsic::amdgcn_if_break:
1297 case AMDGPUIntrinsic::amdgcn_else_break:
1298 case AMDGPUIntrinsic::amdgcn_loop:
1299 case AMDGPUIntrinsic::amdgcn_end_cf:
1300 return true;
1301 }
1302}
1303
Tom Stellardf8794352012-12-19 22:10:31 +00001304/// This transforms the control flow intrinsics to get the branch destination as
1305/// last parameter, also switches branch target with BR if the need arise
1306SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1307 SelectionDAG &DAG) const {
1308
Andrew Trickef9de2a2013-05-25 02:42:55 +00001309 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001310
1311 SDNode *Intr = BRCOND.getOperand(1).getNode();
1312 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001313 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001314 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001315
1316 if (Intr->getOpcode() == ISD::SETCC) {
1317 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001318 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001319 Intr = SetCC->getOperand(0).getNode();
1320
1321 } else {
1322 // Get the target from BR if we don't negate the condition
1323 BR = findUser(BRCOND, ISD::BR);
1324 Target = BR->getOperand(1);
1325 }
1326
Tom Stellardbc4497b2016-02-12 23:45:29 +00001327 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN) {
1328 // This is a uniform branch so we don't need to legalize.
1329 return BRCOND;
1330 }
1331
1332 assert(!SetCC ||
1333 (SetCC->getConstantOperandVal(1) == 1 &&
1334 isCFIntrinsic(Intr) &&
1335 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1336 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001337
1338 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001339 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001340
1341 // operands of the new intrinsic call
1342 SmallVector<SDValue, 4> Ops;
1343 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001344 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001345 Ops.push_back(Target);
1346
1347 // build the new intrinsic call
1348 SDNode *Result = DAG.getNode(
1349 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001350 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001351
1352 if (BR) {
1353 // Give the branch instruction our target
1354 SDValue Ops[] = {
1355 BR->getOperand(0),
1356 BRCOND.getOperand(2)
1357 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001358 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1359 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1360 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001361 }
1362
1363 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1364
1365 // Copy the intrinsic results to registers
1366 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1367 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1368 if (!CopyToReg)
1369 continue;
1370
1371 Chain = DAG.getCopyToReg(
1372 Chain, DL,
1373 CopyToReg->getOperand(1),
1374 SDValue(Result, i - 1),
1375 SDValue());
1376
1377 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1378 }
1379
1380 // Remove the old intrinsic from the chain
1381 DAG.ReplaceAllUsesOfValueWith(
1382 SDValue(Intr, Intr->getNumValues() - 1),
1383 Intr->getOperand(0));
1384
1385 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001386}
1387
Tom Stellard067c8152014-07-21 14:01:14 +00001388SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1389 SDValue Op,
1390 SelectionDAG &DAG) const {
1391 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1392
1393 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
1394 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1395
1396 SDLoc DL(GSD);
1397 const GlobalValue *GV = GSD->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001398 MVT PtrVT = getPointerTy(DAG.getDataLayout(), GSD->getAddressSpace());
Tom Stellard067c8152014-07-21 14:01:14 +00001399
Tom Stellard067c8152014-07-21 14:01:14 +00001400 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
Tom Stellardc93fc112015-12-10 02:13:01 +00001401 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT, GA);
Tom Stellard067c8152014-07-21 14:01:14 +00001402}
1403
Tom Stellardfc92e772015-05-12 14:18:14 +00001404SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL,
1405 SDValue V) const {
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001406 // We can't use S_MOV_B32 directly, because there is no way to specify m0 as
1407 // the destination register.
1408 //
Tom Stellardfc92e772015-05-12 14:18:14 +00001409 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1410 // so we will end up with redundant moves to m0.
1411 //
Matt Arsenault4ac341c2016-04-14 21:58:15 +00001412 // We use a pseudo to ensure we emit s_mov_b32 with m0 as the direct result.
1413
1414 // A Null SDValue creates a glue result.
1415 SDNode *M0 = DAG.getMachineNode(AMDGPU::SI_INIT_M0, DL, MVT::Other, MVT::Glue,
1416 V, Chain);
1417 return SDValue(M0, 0);
Tom Stellardfc92e772015-05-12 14:18:14 +00001418}
1419
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001420SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1421 SDValue Op,
1422 MVT VT,
1423 unsigned Offset) const {
1424 SDLoc SL(Op);
1425 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1426 DAG.getEntryNode(), Offset, false);
1427 // The local size values will have the hi 16-bits as zero.
1428 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1429 DAG.getValueType(VT));
1430}
1431
Matt Arsenaulte0132462016-01-30 05:19:45 +00001432static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, EVT VT) {
1433 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
1434 "non-hsa intrinsic with hsa target");
1435 DAG.getContext()->diagnose(BadIntrin);
1436 return DAG.getUNDEF(VT);
1437}
1438
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001439SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1440 SelectionDAG &DAG) const {
1441 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001442 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardec2e43c2014-09-22 15:35:29 +00001443 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +00001444 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001445
1446 EVT VT = Op.getValueType();
1447 SDLoc DL(Op);
1448 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1449
Sanjay Patela2607012015-09-16 16:31:21 +00001450 // TODO: Should this propagate fast-math-flags?
1451
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001452 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001453 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault800fecf2016-01-11 21:18:33 +00001454 if (!Subtarget->isAmdHsaOS()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00001455 DiagnosticInfoUnsupported BadIntrin(
1456 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
1457 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00001458 DAG.getContext()->diagnose(BadIntrin);
1459 return DAG.getUNDEF(VT);
1460 }
1461
Tom Stellard48f29f22015-11-26 00:43:29 +00001462 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
1463 TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_PTR), VT);
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001464 case Intrinsic::amdgcn_rcp:
1465 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
1466 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001467 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001468 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenault79963e82016-02-13 01:03:00 +00001469 case Intrinsic::amdgcn_rsq_clamp:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001470 case AMDGPUIntrinsic::AMDGPU_rsq_clamped: { // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001471 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00001472 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00001473
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001474 Type *Type = VT.getTypeForEVT(*DAG.getContext());
1475 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
1476 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
1477
1478 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
1479 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
1480 DAG.getConstantFP(Max, DL, VT));
1481 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
1482 DAG.getConstantFP(Min, DL, VT));
1483 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001484 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001485 if (Subtarget->isAmdHsaOS())
1486 return emitNonHSAIntrinsicError(DAG, VT);
1487
Tom Stellardec2e43c2014-09-22 15:35:29 +00001488 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1489 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001490 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001491 if (Subtarget->isAmdHsaOS())
1492 return emitNonHSAIntrinsicError(DAG, VT);
1493
Tom Stellardec2e43c2014-09-22 15:35:29 +00001494 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1495 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001496 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001497 if (Subtarget->isAmdHsaOS())
1498 return emitNonHSAIntrinsicError(DAG, VT);
1499
Tom Stellardec2e43c2014-09-22 15:35:29 +00001500 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1501 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001502 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001503 if (Subtarget->isAmdHsaOS())
1504 return emitNonHSAIntrinsicError(DAG, VT);
1505
Tom Stellardec2e43c2014-09-22 15:35:29 +00001506 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1507 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001508 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001509 if (Subtarget->isAmdHsaOS())
1510 return emitNonHSAIntrinsicError(DAG, VT);
1511
Tom Stellardec2e43c2014-09-22 15:35:29 +00001512 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1513 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001514 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001515 if (Subtarget->isAmdHsaOS())
1516 return emitNonHSAIntrinsicError(DAG, VT);
1517
Tom Stellardec2e43c2014-09-22 15:35:29 +00001518 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1519 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001520 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001521 if (Subtarget->isAmdHsaOS())
1522 return emitNonHSAIntrinsicError(DAG, VT);
1523
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001524 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1525 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001526 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001527 if (Subtarget->isAmdHsaOS())
1528 return emitNonHSAIntrinsicError(DAG, VT);
1529
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001530 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1531 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001532 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001533 if (Subtarget->isAmdHsaOS())
1534 return emitNonHSAIntrinsicError(DAG, VT);
1535
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001536 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1537 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenaultbef34e22016-01-22 21:30:34 +00001538 case Intrinsic::amdgcn_read_workdim:
1539 case AMDGPUIntrinsic::AMDGPU_read_workdim: // Legacy name.
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001540 // Really only 2 bits.
1541 return lowerImplicitZextParam(DAG, Op, MVT::i8,
1542 getImplicitParameterOffset(MFI, GRID_DIM));
Matt Arsenault43976df2016-01-30 04:25:19 +00001543 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001544 case Intrinsic::r600_read_tgid_x:
1545 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001546 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001547 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001548 case Intrinsic::r600_read_tgid_y:
1549 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001550 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001551 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001552 case Intrinsic::r600_read_tgid_z:
1553 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001554 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001555 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001556 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001557 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001558 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001559 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001560 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001561 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001562 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001563 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001564 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001565 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001566 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001567 case AMDGPUIntrinsic::SI_load_const: {
1568 SDValue Ops[] = {
1569 Op.getOperand(1),
1570 Op.getOperand(2)
1571 };
1572
1573 MachineMemOperand *MMO = MF.getMachineMemOperand(
1574 MachinePointerInfo(),
1575 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
1576 VT.getStoreSize(), 4);
1577 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
1578 Op->getVTList(), Ops, VT, MMO);
1579 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001580 case AMDGPUIntrinsic::SI_vs_load_input:
1581 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1582 Op.getOperand(1),
1583 Op.getOperand(2),
1584 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00001585
Tom Stellard2a9d9472015-05-12 15:00:46 +00001586 case AMDGPUIntrinsic::SI_fs_constant: {
1587 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1588 SDValue Glue = M0.getValue(1);
1589 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
1590 DAG.getConstant(2, DL, MVT::i32), // P0
1591 Op.getOperand(1), Op.getOperand(2), Glue);
1592 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00001593 case AMDGPUIntrinsic::SI_packf16:
1594 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
1595 return DAG.getUNDEF(MVT::i32);
1596 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00001597 case AMDGPUIntrinsic::SI_fs_interp: {
1598 SDValue IJ = Op.getOperand(4);
1599 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1600 DAG.getConstant(0, DL, MVT::i32));
1601 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1602 DAG.getConstant(1, DL, MVT::i32));
1603 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1604 SDValue Glue = M0.getValue(1);
1605 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
1606 DAG.getVTList(MVT::f32, MVT::Glue),
1607 I, Op.getOperand(1), Op.getOperand(2), Glue);
1608 Glue = SDValue(P1.getNode(), 1);
1609 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
1610 Op.getOperand(1), Op.getOperand(2), Glue);
1611 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00001612 case Intrinsic::amdgcn_interp_p1: {
1613 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
1614 SDValue Glue = M0.getValue(1);
1615 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
1616 Op.getOperand(2), Op.getOperand(3), Glue);
1617 }
1618 case Intrinsic::amdgcn_interp_p2: {
1619 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
1620 SDValue Glue = SDValue(M0.getNode(), 1);
1621 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
1622 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
1623 Glue);
1624 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00001625 case Intrinsic::amdgcn_sin:
1626 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
1627
1628 case Intrinsic::amdgcn_cos:
1629 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
1630
1631 case Intrinsic::amdgcn_log_clamp: {
1632 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
1633 return SDValue();
1634
1635 DiagnosticInfoUnsupported BadIntrin(
1636 *MF.getFunction(), "intrinsic not supported on subtarget",
1637 DL.getDebugLoc());
1638 DAG.getContext()->diagnose(BadIntrin);
1639 return DAG.getUNDEF(VT);
1640 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001641 case Intrinsic::amdgcn_ldexp:
1642 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
1643 Op.getOperand(1), Op.getOperand(2));
1644 case Intrinsic::amdgcn_class:
1645 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
1646 Op.getOperand(1), Op.getOperand(2));
1647 case Intrinsic::amdgcn_div_fmas:
1648 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
1649 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
1650 Op.getOperand(4));
1651
1652 case Intrinsic::amdgcn_div_fixup:
1653 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
1654 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
1655
1656 case Intrinsic::amdgcn_trig_preop:
1657 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
1658 Op.getOperand(1), Op.getOperand(2));
1659 case Intrinsic::amdgcn_div_scale: {
1660 // 3rd parameter required to be a constant.
1661 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1662 if (!Param)
1663 return DAG.getUNDEF(VT);
1664
1665 // Translate to the operands expected by the machine instruction. The
1666 // first parameter must be the same as the first instruction.
1667 SDValue Numerator = Op.getOperand(1);
1668 SDValue Denominator = Op.getOperand(2);
1669
1670 // Note this order is opposite of the machine instruction's operations,
1671 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
1672 // intrinsic has the numerator as the first operand to match a normal
1673 // division operation.
1674
1675 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
1676
1677 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
1678 Denominator, Numerator);
1679 }
1680 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte0:
1681 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Op.getOperand(1));
1682 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte1:
1683 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE1, DL, VT, Op.getOperand(1));
1684 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte2:
1685 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE2, DL, VT, Op.getOperand(1));
1686 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte3:
1687 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE3, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001688 default:
1689 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1690 }
1691}
1692
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001693SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
1694 SelectionDAG &DAG) const {
1695 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1696 switch (IntrID) {
1697 case Intrinsic::amdgcn_atomic_inc:
1698 case Intrinsic::amdgcn_atomic_dec: {
1699 MemSDNode *M = cast<MemSDNode>(Op);
1700 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
1701 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
1702 SDValue Ops[] = {
1703 M->getOperand(0), // Chain
1704 M->getOperand(2), // Ptr
1705 M->getOperand(3) // Value
1706 };
1707
1708 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
1709 M->getMemoryVT(), M->getMemOperand());
1710 }
1711 default:
1712 return SDValue();
1713 }
1714}
1715
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001716SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1717 SelectionDAG &DAG) const {
1718 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00001719 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001720 SDValue Chain = Op.getOperand(0);
1721 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1722
1723 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00001724 case AMDGPUIntrinsic::SI_sendmsg: {
1725 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1726 SDValue Glue = Chain.getValue(1);
1727 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1728 Op.getOperand(2), Glue);
1729 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001730 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001731 SDValue Ops[] = {
1732 Chain,
1733 Op.getOperand(2),
1734 Op.getOperand(3),
1735 Op.getOperand(4),
1736 Op.getOperand(5),
1737 Op.getOperand(6),
1738 Op.getOperand(7),
1739 Op.getOperand(8),
1740 Op.getOperand(9),
1741 Op.getOperand(10),
1742 Op.getOperand(11),
1743 Op.getOperand(12),
1744 Op.getOperand(13),
1745 Op.getOperand(14)
1746 };
1747
1748 EVT VT = Op.getOperand(3).getValueType();
1749
1750 MachineMemOperand *MMO = MF.getMachineMemOperand(
1751 MachinePointerInfo(),
1752 MachineMemOperand::MOStore,
1753 VT.getStoreSize(), 4);
1754 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1755 Op->getVTList(), Ops, VT, MMO);
1756 }
1757 default:
1758 return SDValue();
1759 }
1760}
1761
Tom Stellard81d871d2013-11-13 23:36:50 +00001762SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1763 SDLoc DL(Op);
1764 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001765 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00001766 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00001767
Matt Arsenaulta1436412016-02-10 18:21:45 +00001768 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
1769 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected");
Matt Arsenault6dfda962016-02-10 18:21:39 +00001770 // FIXME: Copied from PPC
1771 // First, load into 32 bits, then truncate to 1 bit.
1772
1773 SDValue Chain = Load->getChain();
1774 SDValue BasePtr = Load->getBasePtr();
1775 MachineMemOperand *MMO = Load->getMemOperand();
1776
1777 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
1778 BasePtr, MVT::i8, MMO);
1779
1780 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00001781 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00001782 NewLD.getValue(1)
1783 };
1784
1785 return DAG.getMergeValues(Ops, DL);
1786 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001787
Matt Arsenaulta1436412016-02-10 18:21:45 +00001788 if (!MemVT.isVector())
1789 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001790
Matt Arsenaulta1436412016-02-10 18:21:45 +00001791 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1792 "Custom lowering for non-i32 vectors hasn't been implemented.");
1793 unsigned NumElements = MemVT.getVectorNumElements();
1794 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001795
Matt Arsenaulta1436412016-02-10 18:21:45 +00001796 switch (Load->getAddressSpace()) {
1797 case AMDGPUAS::CONSTANT_ADDRESS:
1798 if (isMemOpUniform(Load))
1799 return SDValue();
1800 // Non-uniform loads will be selected to MUBUF instructions, so they
1801 // have the same legalization requires ments as global and private
1802 // loads.
1803 //
1804 // Fall-through
1805 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001806 case AMDGPUAS::FLAT_ADDRESS:
1807 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00001808 return SplitVectorLoad(Op, DAG);
1809 // v4 loads are supported for private and global memory.
1810 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001811 case AMDGPUAS::PRIVATE_ADDRESS: {
1812 // Depending on the setting of the private_element_size field in the
1813 // resource descriptor, we can only make private accesses up to a certain
1814 // size.
1815 switch (Subtarget->getMaxPrivateElementSize()) {
1816 case 4:
1817 return ScalarizeVectorLoad(Op, DAG);
1818 case 8:
1819 if (NumElements > 2)
1820 return SplitVectorLoad(Op, DAG);
1821 return SDValue();
1822 case 16:
1823 // Same as global/flat
1824 if (NumElements > 4)
1825 return SplitVectorLoad(Op, DAG);
1826 return SDValue();
1827 default:
1828 llvm_unreachable("unsupported private_element_size");
1829 }
1830 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00001831 case AMDGPUAS::LOCAL_ADDRESS:
1832 // If properly aligned, if we split we might be able to use ds_read_b64.
1833 return SplitVectorLoad(Op, DAG);
1834 default:
1835 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00001836 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001837}
1838
Tom Stellard0ec134f2014-02-04 17:18:40 +00001839SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1840 if (Op.getValueType() != MVT::i64)
1841 return SDValue();
1842
1843 SDLoc DL(Op);
1844 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001845
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001846 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1847 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001848
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001849 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1850 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1851
1852 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1853 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001854
1855 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1856
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001857 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1858 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001859
1860 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1861
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001862 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1863 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001864}
1865
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001866// Catch division cases where we can use shortcuts with rcp and rsq
1867// instructions.
1868SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001869 SDLoc SL(Op);
1870 SDValue LHS = Op.getOperand(0);
1871 SDValue RHS = Op.getOperand(1);
1872 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001873 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001874
1875 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001876 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1877 CLHS->isExactlyValue(1.0)) {
1878 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1879 // the CI documentation has a worst case error of 1 ulp.
1880 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1881 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001882
1883 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001884 //
1885 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1886 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001887 if (RHS.getOpcode() == ISD::FSQRT)
1888 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1889
1890 // 1.0 / x -> rcp(x)
1891 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1892 }
1893 }
1894
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001895 if (Unsafe) {
1896 // Turn into multiply by the reciprocal.
1897 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00001898 SDNodeFlags Flags;
1899 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001900 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00001901 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001902 }
1903
1904 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001905}
1906
1907SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001908 if (SDValue FastLowered = LowerFastFDIV(Op, DAG))
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001909 return FastLowered;
1910
1911 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1912 // selection error for now rather than do something incorrect.
1913 if (Subtarget->hasFP32Denormals())
1914 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001915
1916 SDLoc SL(Op);
1917 SDValue LHS = Op.getOperand(0);
1918 SDValue RHS = Op.getOperand(1);
1919
1920 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1921
1922 const APFloat K0Val(BitsToFloat(0x6f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001923 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001924
1925 const APFloat K1Val(BitsToFloat(0x2f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001926 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001927
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001928 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001929
Mehdi Amini44ede332015-07-09 02:09:04 +00001930 EVT SetCCVT =
1931 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001932
1933 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1934
1935 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1936
Sanjay Patela2607012015-09-16 16:31:21 +00001937 // TODO: Should this propagate fast-math-flags?
1938
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001939 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1940
1941 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1942
1943 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1944
1945 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1946}
1947
1948SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001949 if (DAG.getTarget().Options.UnsafeFPMath)
1950 return LowerFastFDIV(Op, DAG);
1951
1952 SDLoc SL(Op);
1953 SDValue X = Op.getOperand(0);
1954 SDValue Y = Op.getOperand(1);
1955
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001956 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001957
1958 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
1959
1960 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
1961
1962 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
1963
1964 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
1965
1966 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
1967
1968 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
1969
1970 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
1971
1972 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
1973
1974 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
1975 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
1976
1977 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
1978 NegDivScale0, Mul, DivScale1);
1979
1980 SDValue Scale;
1981
1982 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1983 // Workaround a hardware bug on SI where the condition output from div_scale
1984 // is not usable.
1985
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001986 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001987
1988 // Figure out if the scale to use for div_fmas.
1989 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1990 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
1991 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
1992 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
1993
1994 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
1995 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
1996
1997 SDValue Scale0Hi
1998 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
1999 SDValue Scale1Hi
2000 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
2001
2002 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
2003 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
2004 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
2005 } else {
2006 Scale = DivScale1.getValue(1);
2007 }
2008
2009 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
2010 Fma4, Fma3, Mul, Scale);
2011
2012 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002013}
2014
2015SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
2016 EVT VT = Op.getValueType();
2017
2018 if (VT == MVT::f32)
2019 return LowerFDIV32(Op, DAG);
2020
2021 if (VT == MVT::f64)
2022 return LowerFDIV64(Op, DAG);
2023
2024 llvm_unreachable("Unexpected type for fdiv");
2025}
2026
Tom Stellard81d871d2013-11-13 23:36:50 +00002027SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2028 SDLoc DL(Op);
2029 StoreSDNode *Store = cast<StoreSDNode>(Op);
2030 EVT VT = Store->getMemoryVT();
2031
Matt Arsenault95245662016-02-11 05:32:46 +00002032 if (VT == MVT::i1) {
2033 return DAG.getTruncStore(Store->getChain(), DL,
2034 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
2035 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00002036 }
2037
Matt Arsenault95245662016-02-11 05:32:46 +00002038 assert(Store->getValue().getValueType().getScalarType() == MVT::i32);
Tom Stellard81d871d2013-11-13 23:36:50 +00002039
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002040 unsigned NumElements = VT.getVectorNumElements();
2041 switch (Store->getAddressSpace()) {
2042 case AMDGPUAS::GLOBAL_ADDRESS:
2043 case AMDGPUAS::FLAT_ADDRESS:
2044 if (NumElements > 4)
2045 return SplitVectorStore(Op, DAG);
2046 return SDValue();
2047 case AMDGPUAS::PRIVATE_ADDRESS: {
2048 switch (Subtarget->getMaxPrivateElementSize()) {
2049 case 4:
2050 return ScalarizeVectorStore(Op, DAG);
2051 case 8:
2052 if (NumElements > 2)
2053 return SplitVectorStore(Op, DAG);
2054 return SDValue();
2055 case 16:
2056 if (NumElements > 4)
2057 return SplitVectorStore(Op, DAG);
2058 return SDValue();
2059 default:
2060 llvm_unreachable("unsupported private_element_size");
2061 }
2062 }
2063 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenault95245662016-02-11 05:32:46 +00002064 // If properly aligned, if we split we might be able to use ds_write_b64.
2065 return SplitVectorStore(Op, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002066 default:
2067 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00002068 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002069}
2070
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002071SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002072 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002073 EVT VT = Op.getValueType();
2074 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00002075 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002076 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
2077 DAG.getNode(ISD::FMUL, DL, VT, Arg,
2078 DAG.getConstantFP(0.5/M_PI, DL,
2079 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002080
2081 switch (Op.getOpcode()) {
2082 case ISD::FCOS:
2083 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
2084 case ISD::FSIN:
2085 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
2086 default:
2087 llvm_unreachable("Wrong trig opcode");
2088 }
2089}
2090
Tom Stellard354a43c2016-04-01 18:27:37 +00002091SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
2092 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
2093 assert(AtomicNode->isCompareAndSwap());
2094 unsigned AS = AtomicNode->getAddressSpace();
2095
2096 // No custom lowering required for local address space
2097 if (!isFlatGlobalAddrSpace(AS))
2098 return Op;
2099
2100 // Non-local address space requires custom lowering for atomic compare
2101 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
2102 SDLoc DL(Op);
2103 SDValue ChainIn = Op.getOperand(0);
2104 SDValue Addr = Op.getOperand(1);
2105 SDValue Old = Op.getOperand(2);
2106 SDValue New = Op.getOperand(3);
2107 EVT VT = Op.getValueType();
2108 MVT SimpleVT = VT.getSimpleVT();
2109 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
2110
2111 SDValue NewOld = DAG.getNode(ISD::BUILD_VECTOR, DL, VecType,
2112 New, Old);
2113 SDValue Ops[] = { ChainIn, Addr, NewOld };
2114 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2115 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL,
2116 VTList, Ops, VT, AtomicNode->getMemOperand());
2117}
2118
Tom Stellard75aadc22012-12-11 21:25:42 +00002119//===----------------------------------------------------------------------===//
2120// Custom DAG optimizations
2121//===----------------------------------------------------------------------===//
2122
Matt Arsenault364a6742014-06-11 17:50:44 +00002123SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00002124 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00002125 EVT VT = N->getValueType(0);
2126 EVT ScalarVT = VT.getScalarType();
2127 if (ScalarVT != MVT::f32)
2128 return SDValue();
2129
2130 SelectionDAG &DAG = DCI.DAG;
2131 SDLoc DL(N);
2132
2133 SDValue Src = N->getOperand(0);
2134 EVT SrcVT = Src.getValueType();
2135
2136 // TODO: We could try to match extracting the higher bytes, which would be
2137 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
2138 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
2139 // about in practice.
2140 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
2141 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
2142 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
2143 DCI.AddToWorklist(Cvt.getNode());
2144 return Cvt;
2145 }
2146 }
2147
2148 // We are primarily trying to catch operations on illegal vector types
2149 // before they are expanded.
2150 // For scalars, we can use the more flexible method of checking masked bits
2151 // after legalization.
2152 if (!DCI.isBeforeLegalize() ||
2153 !SrcVT.isVector() ||
2154 SrcVT.getVectorElementType() != MVT::i8) {
2155 return SDValue();
2156 }
2157
2158 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
2159
2160 // Weird sized vectors are a pain to handle, but we know 3 is really the same
2161 // size as 4.
2162 unsigned NElts = SrcVT.getVectorNumElements();
2163 if (!SrcVT.isSimple() && NElts != 3)
2164 return SDValue();
2165
2166 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
2167 // prevent a mess from expanding to v4i32 and repacking.
2168 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
2169 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
2170 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
2171 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00002172 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00002173
2174 unsigned AS = Load->getAddressSpace();
2175 unsigned Align = Load->getAlignment();
2176 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +00002177 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
Matt Arsenaulte6986632015-01-14 01:35:22 +00002178
2179 // Don't try to replace the load if we have to expand it due to alignment
2180 // problems. Otherwise we will end up scalarizing the load, and trying to
2181 // repack into the vector for no real reason.
2182 if (Align < ABIAlignment &&
2183 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
2184 return SDValue();
2185 }
2186
Matt Arsenault364a6742014-06-11 17:50:44 +00002187 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
2188 Load->getChain(),
2189 Load->getBasePtr(),
2190 LoadVT,
2191 Load->getMemOperand());
2192
2193 // Make sure successors of the original load stay after it by updating
2194 // them to use the new Chain.
2195 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
2196
2197 SmallVector<SDValue, 4> Elts;
2198 if (RegVT.isVector())
2199 DAG.ExtractVectorElements(NewLoad, Elts);
2200 else
2201 Elts.push_back(NewLoad);
2202
2203 SmallVector<SDValue, 4> Ops;
2204
2205 unsigned EltIdx = 0;
2206 for (SDValue Elt : Elts) {
2207 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
2208 for (unsigned I = 0; I < ComponentsInElt; ++I) {
2209 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
2210 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
2211 DCI.AddToWorklist(Cvt.getNode());
2212 Ops.push_back(Cvt);
2213 }
2214
2215 ++EltIdx;
2216 }
2217
2218 assert(Ops.size() == NElts);
2219
2220 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
2221 }
2222
2223 return SDValue();
2224}
2225
Eric Christopher6c5b5112015-03-11 18:43:21 +00002226/// \brief Return true if the given offset Size in bytes can be folded into
2227/// the immediate offsets of a memory instruction for the given address space.
2228static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
2229 const AMDGPUSubtarget &STI) {
2230 switch (AS) {
2231 case AMDGPUAS::GLOBAL_ADDRESS: {
2232 // MUBUF instructions a 12-bit offset in bytes.
2233 return isUInt<12>(OffsetSize);
2234 }
2235 case AMDGPUAS::CONSTANT_ADDRESS: {
2236 // SMRD instructions have an 8-bit offset in dwords on SI and
2237 // a 20-bit offset in bytes on VI.
2238 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
2239 return isUInt<20>(OffsetSize);
2240 else
2241 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
2242 }
2243 case AMDGPUAS::LOCAL_ADDRESS:
2244 case AMDGPUAS::REGION_ADDRESS: {
2245 // The single offset versions have a 16-bit offset in bytes.
2246 return isUInt<16>(OffsetSize);
2247 }
2248 case AMDGPUAS::PRIVATE_ADDRESS:
2249 // Indirect register addressing does not use any offsets.
2250 default:
2251 return 0;
2252 }
2253}
2254
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002255// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
2256
2257// This is a variant of
2258// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
2259//
2260// The normal DAG combiner will do this, but only if the add has one use since
2261// that would increase the number of instructions.
2262//
2263// This prevents us from seeing a constant offset that can be folded into a
2264// memory instruction's addressing mode. If we know the resulting add offset of
2265// a pointer can be folded into an addressing offset, we can replace the pointer
2266// operand with the add of new constant offset. This eliminates one of the uses,
2267// and may allow the remaining use to also be simplified.
2268//
2269SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
2270 unsigned AddrSpace,
2271 DAGCombinerInfo &DCI) const {
2272 SDValue N0 = N->getOperand(0);
2273 SDValue N1 = N->getOperand(1);
2274
2275 if (N0.getOpcode() != ISD::ADD)
2276 return SDValue();
2277
2278 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
2279 if (!CN1)
2280 return SDValue();
2281
2282 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2283 if (!CAdd)
2284 return SDValue();
2285
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002286 // If the resulting offset is too large, we can't fold it into the addressing
2287 // mode offset.
2288 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Eric Christopher6c5b5112015-03-11 18:43:21 +00002289 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002290 return SDValue();
2291
2292 SelectionDAG &DAG = DCI.DAG;
2293 SDLoc SL(N);
2294 EVT VT = N->getValueType(0);
2295
2296 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002297 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002298
2299 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
2300}
2301
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002302SDValue SITargetLowering::performAndCombine(SDNode *N,
2303 DAGCombinerInfo &DCI) const {
2304 if (DCI.isBeforeLegalize())
2305 return SDValue();
2306
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002307 if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI))
2308 return Base;
2309
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002310 SelectionDAG &DAG = DCI.DAG;
2311
2312 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
2313 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
2314 SDValue LHS = N->getOperand(0);
2315 SDValue RHS = N->getOperand(1);
2316
2317 if (LHS.getOpcode() == ISD::SETCC &&
2318 RHS.getOpcode() == ISD::SETCC) {
2319 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2320 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2321
2322 SDValue X = LHS.getOperand(0);
2323 SDValue Y = RHS.getOperand(0);
2324 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2325 return SDValue();
2326
2327 if (LCC == ISD::SETO) {
2328 if (X != LHS.getOperand(1))
2329 return SDValue();
2330
2331 if (RCC == ISD::SETUNE) {
2332 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2333 if (!C1 || !C1->isInfinity() || C1->isNegative())
2334 return SDValue();
2335
2336 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2337 SIInstrFlags::N_SUBNORMAL |
2338 SIInstrFlags::N_ZERO |
2339 SIInstrFlags::P_ZERO |
2340 SIInstrFlags::P_SUBNORMAL |
2341 SIInstrFlags::P_NORMAL;
2342
2343 static_assert(((~(SIInstrFlags::S_NAN |
2344 SIInstrFlags::Q_NAN |
2345 SIInstrFlags::N_INFINITY |
2346 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2347 "mask not equal");
2348
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002349 SDLoc DL(N);
2350 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2351 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002352 }
2353 }
2354 }
2355
2356 return SDValue();
2357}
2358
Matt Arsenaultf2290332015-01-06 23:00:39 +00002359SDValue SITargetLowering::performOrCombine(SDNode *N,
2360 DAGCombinerInfo &DCI) const {
2361 SelectionDAG &DAG = DCI.DAG;
2362 SDValue LHS = N->getOperand(0);
2363 SDValue RHS = N->getOperand(1);
2364
Matt Arsenault3b082382016-04-12 18:24:38 +00002365 EVT VT = N->getValueType(0);
2366 if (VT == MVT::i64) {
2367 // TODO: This could be a generic combine with a predicate for extracting the
2368 // high half of an integer being free.
2369
2370 // (or i64:x, (zero_extend i32:y)) ->
2371 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
2372 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
2373 RHS.getOpcode() != ISD::ZERO_EXTEND)
2374 std::swap(LHS, RHS);
2375
2376 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
2377 SDValue ExtSrc = RHS.getOperand(0);
2378 EVT SrcVT = ExtSrc.getValueType();
2379 if (SrcVT == MVT::i32) {
2380 SDLoc SL(N);
2381 SDValue LowLHS, HiBits;
2382 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
2383 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
2384
2385 DCI.AddToWorklist(LowOr.getNode());
2386 DCI.AddToWorklist(HiBits.getNode());
2387
2388 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
2389 LowOr, HiBits);
2390 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2391 }
2392 }
2393 }
2394
Matt Arsenaultf2290332015-01-06 23:00:39 +00002395 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
2396 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
2397 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
2398 SDValue Src = LHS.getOperand(0);
2399 if (Src != RHS.getOperand(0))
2400 return SDValue();
2401
2402 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
2403 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
2404 if (!CLHS || !CRHS)
2405 return SDValue();
2406
2407 // Only 10 bits are used.
2408 static const uint32_t MaxMask = 0x3ff;
2409
2410 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002411 SDLoc DL(N);
2412 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2413 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00002414 }
2415
2416 return SDValue();
2417}
2418
2419SDValue SITargetLowering::performClassCombine(SDNode *N,
2420 DAGCombinerInfo &DCI) const {
2421 SelectionDAG &DAG = DCI.DAG;
2422 SDValue Mask = N->getOperand(1);
2423
2424 // fp_class x, 0 -> false
2425 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
2426 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002427 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002428 }
2429
2430 return SDValue();
2431}
2432
Matt Arsenault9cd90712016-04-14 01:42:16 +00002433// Constant fold canonicalize.
2434SDValue SITargetLowering::performFCanonicalizeCombine(
2435 SDNode *N,
2436 DAGCombinerInfo &DCI) const {
2437 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
2438 if (!CFP)
2439 return SDValue();
2440
2441 SelectionDAG &DAG = DCI.DAG;
2442 const APFloat &C = CFP->getValueAPF();
2443
2444 // Flush denormals to 0 if not enabled.
2445 if (C.isDenormal()) {
2446 EVT VT = N->getValueType(0);
2447 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
2448 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2449
2450 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
2451 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2452 }
2453
2454 if (C.isNaN()) {
2455 EVT VT = N->getValueType(0);
2456 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
2457 if (C.isSignaling()) {
2458 // Quiet a signaling NaN.
2459 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
2460 }
2461
2462 // Make sure it is the canonical NaN bitpattern.
2463 //
2464 // TODO: Can we use -1 as the canonical NaN value since it's an inline
2465 // immediate?
2466 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
2467 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
2468 }
2469
2470 return SDValue(CFP, 0);
2471}
2472
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002473static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
2474 switch (Opc) {
2475 case ISD::FMAXNUM:
2476 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002477 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002478 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002479 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002480 return AMDGPUISD::UMAX3;
2481 case ISD::FMINNUM:
2482 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002483 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002484 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002485 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002486 return AMDGPUISD::UMIN3;
2487 default:
2488 llvm_unreachable("Not a min/max opcode");
2489 }
2490}
2491
Matt Arsenaultf639c322016-01-28 20:53:42 +00002492static SDValue performIntMed3ImmCombine(SelectionDAG &DAG,
2493 SDLoc SL,
2494 SDValue Op0,
2495 SDValue Op1,
2496 bool Signed) {
2497 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
2498 if (!K1)
2499 return SDValue();
2500
2501 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
2502 if (!K0)
2503 return SDValue();
2504
2505
2506 if (Signed) {
2507 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
2508 return SDValue();
2509 } else {
2510 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
2511 return SDValue();
2512 }
2513
2514 EVT VT = K0->getValueType(0);
2515 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
2516 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
2517}
2518
2519static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
2520 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
2521 return true;
2522
2523 return DAG.isKnownNeverNaN(Op);
2524}
2525
2526static SDValue performFPMed3ImmCombine(SelectionDAG &DAG,
2527 SDLoc SL,
2528 SDValue Op0,
2529 SDValue Op1) {
2530 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
2531 if (!K1)
2532 return SDValue();
2533
2534 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
2535 if (!K0)
2536 return SDValue();
2537
2538 // Ordered >= (although NaN inputs should have folded away by now).
2539 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
2540 if (Cmp == APFloat::cmpGreaterThan)
2541 return SDValue();
2542
2543 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
2544 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
2545 // give the other result, which is different from med3 with a NaN input.
2546 SDValue Var = Op0.getOperand(0);
2547 if (!isKnownNeverSNan(DAG, Var))
2548 return SDValue();
2549
2550 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
2551 Var, SDValue(K0, 0), SDValue(K1, 0));
2552}
2553
2554SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
2555 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002556 SelectionDAG &DAG = DCI.DAG;
2557
2558 unsigned Opc = N->getOpcode();
2559 SDValue Op0 = N->getOperand(0);
2560 SDValue Op1 = N->getOperand(1);
2561
2562 // Only do this if the inner op has one use since this will just increases
2563 // register pressure for no benefit.
2564
Matt Arsenault5b39b342016-01-28 20:53:48 +00002565 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
2566 // max(max(a, b), c) -> max3(a, b, c)
2567 // min(min(a, b), c) -> min3(a, b, c)
2568 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
2569 SDLoc DL(N);
2570 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2571 DL,
2572 N->getValueType(0),
2573 Op0.getOperand(0),
2574 Op0.getOperand(1),
2575 Op1);
2576 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002577
Matt Arsenault5b39b342016-01-28 20:53:48 +00002578 // Try commuted.
2579 // max(a, max(b, c)) -> max3(a, b, c)
2580 // min(a, min(b, c)) -> min3(a, b, c)
2581 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
2582 SDLoc DL(N);
2583 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2584 DL,
2585 N->getValueType(0),
2586 Op0,
2587 Op1.getOperand(0),
2588 Op1.getOperand(1));
2589 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002590 }
2591
Matt Arsenaultf639c322016-01-28 20:53:42 +00002592 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
2593 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
2594 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
2595 return Med3;
2596 }
2597
2598 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
2599 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
2600 return Med3;
2601 }
2602
2603 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00002604 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
2605 (Opc == AMDGPUISD::FMIN_LEGACY &&
2606 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00002607 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
2608 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
2609 return Res;
2610 }
2611
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002612 return SDValue();
2613}
2614
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002615SDValue SITargetLowering::performSetCCCombine(SDNode *N,
2616 DAGCombinerInfo &DCI) const {
2617 SelectionDAG &DAG = DCI.DAG;
2618 SDLoc SL(N);
2619
2620 SDValue LHS = N->getOperand(0);
2621 SDValue RHS = N->getOperand(1);
2622 EVT VT = LHS.getValueType();
2623
2624 if (VT != MVT::f32 && VT != MVT::f64)
2625 return SDValue();
2626
2627 // Match isinf pattern
2628 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
2629 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
2630 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
2631 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2632 if (!CRHS)
2633 return SDValue();
2634
2635 const APFloat &APF = CRHS->getValueAPF();
2636 if (APF.isInfinity() && !APF.isNegative()) {
2637 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002638 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
2639 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002640 }
2641 }
2642
2643 return SDValue();
2644}
2645
Tom Stellard75aadc22012-12-11 21:25:42 +00002646SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
2647 DAGCombinerInfo &DCI) const {
2648 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002649 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00002650
2651 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00002652 default:
2653 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002654 case ISD::SETCC:
2655 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00002656 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002657 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002658 case ISD::SMAX:
2659 case ISD::SMIN:
2660 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00002661 case ISD::UMIN:
2662 case AMDGPUISD::FMIN_LEGACY:
2663 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002664 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00002665 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002666 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00002667 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002668 break;
2669 }
Matt Arsenault364a6742014-06-11 17:50:44 +00002670
2671 case AMDGPUISD::CVT_F32_UBYTE0:
2672 case AMDGPUISD::CVT_F32_UBYTE1:
2673 case AMDGPUISD::CVT_F32_UBYTE2:
2674 case AMDGPUISD::CVT_F32_UBYTE3: {
2675 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
2676
2677 SDValue Src = N->getOperand(0);
2678 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
2679
2680 APInt KnownZero, KnownOne;
2681 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2682 !DCI.isBeforeLegalizeOps());
2683 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2684 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
2685 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
2686 DCI.CommitTargetLoweringOpt(TLO);
2687 }
2688
2689 break;
2690 }
2691
2692 case ISD::UINT_TO_FP: {
2693 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002694 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002695 case ISD::FADD: {
2696 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2697 break;
2698
2699 EVT VT = N->getValueType(0);
2700 if (VT != MVT::f32)
2701 break;
2702
Matt Arsenault8d630032015-02-20 22:10:41 +00002703 // Only do this if we are not trying to support denormals. v_mad_f32 does
2704 // not support denormals ever.
2705 if (Subtarget->hasFP32Denormals())
2706 break;
2707
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002708 SDValue LHS = N->getOperand(0);
2709 SDValue RHS = N->getOperand(1);
2710
2711 // These should really be instruction patterns, but writing patterns with
2712 // source modiifiers is a pain.
2713
2714 // fadd (fadd (a, a), b) -> mad 2.0, a, b
2715 if (LHS.getOpcode() == ISD::FADD) {
2716 SDValue A = LHS.getOperand(0);
2717 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002718 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002719 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002720 }
2721 }
2722
2723 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
2724 if (RHS.getOpcode() == ISD::FADD) {
2725 SDValue A = RHS.getOperand(0);
2726 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002727 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002728 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002729 }
2730 }
2731
Matt Arsenault8d630032015-02-20 22:10:41 +00002732 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002733 }
Matt Arsenault8675db12014-08-29 16:01:14 +00002734 case ISD::FSUB: {
2735 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2736 break;
2737
2738 EVT VT = N->getValueType(0);
2739
2740 // Try to get the fneg to fold into the source modifier. This undoes generic
2741 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00002742 //
2743 // Only do this if we are not trying to support denormals. v_mad_f32 does
2744 // not support denormals ever.
2745 if (VT == MVT::f32 &&
2746 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00002747 SDValue LHS = N->getOperand(0);
2748 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002749 if (LHS.getOpcode() == ISD::FADD) {
2750 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
2751
2752 SDValue A = LHS.getOperand(0);
2753 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002754 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002755 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
2756
Matt Arsenault8d630032015-02-20 22:10:41 +00002757 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002758 }
2759 }
2760
2761 if (RHS.getOpcode() == ISD::FADD) {
2762 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
2763
2764 SDValue A = RHS.getOperand(0);
2765 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002766 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002767 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002768 }
2769 }
Matt Arsenault8d630032015-02-20 22:10:41 +00002770
2771 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00002772 }
2773
2774 break;
2775 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002776 case ISD::LOAD:
2777 case ISD::STORE:
2778 case ISD::ATOMIC_LOAD:
2779 case ISD::ATOMIC_STORE:
2780 case ISD::ATOMIC_CMP_SWAP:
2781 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2782 case ISD::ATOMIC_SWAP:
2783 case ISD::ATOMIC_LOAD_ADD:
2784 case ISD::ATOMIC_LOAD_SUB:
2785 case ISD::ATOMIC_LOAD_AND:
2786 case ISD::ATOMIC_LOAD_OR:
2787 case ISD::ATOMIC_LOAD_XOR:
2788 case ISD::ATOMIC_LOAD_NAND:
2789 case ISD::ATOMIC_LOAD_MIN:
2790 case ISD::ATOMIC_LOAD_MAX:
2791 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002792 case ISD::ATOMIC_LOAD_UMAX:
2793 case AMDGPUISD::ATOMIC_INC:
2794 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002795 if (DCI.isBeforeLegalize())
2796 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002797
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002798 MemSDNode *MemNode = cast<MemSDNode>(N);
2799 SDValue Ptr = MemNode->getBasePtr();
2800
2801 // TODO: We could also do this for multiplies.
2802 unsigned AS = MemNode->getAddressSpace();
2803 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
2804 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
2805 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002806 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002807
2808 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
2809 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
2810 }
2811 }
2812 break;
2813 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002814 case ISD::AND:
2815 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002816 case ISD::OR:
2817 return performOrCombine(N, DCI);
2818 case AMDGPUISD::FP_CLASS:
2819 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00002820 case ISD::FCANONICALIZE:
2821 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002822 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002823 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00002824}
Christian Konigd910b7d2013-02-26 17:52:16 +00002825
Christian Konigf82901a2013-02-26 17:52:23 +00002826/// \brief Analyze the possible immediate value Op
2827///
2828/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
2829/// and the immediate value if it's a literal immediate
2830int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
2831
Eric Christopher7792e322015-01-30 23:24:40 +00002832 const SIInstrInfo *TII =
2833 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00002834
Tom Stellardedbf1eb2013-04-05 23:31:20 +00002835 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00002836 if (TII->isInlineConstant(Node->getAPIntValue()))
2837 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00002838
Matt Arsenault11a4d672015-02-13 19:05:03 +00002839 uint64_t Val = Node->getZExtValue();
2840 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00002841 }
2842
2843 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
2844 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
2845 return 0;
2846
2847 if (Node->getValueType(0) == MVT::f32)
2848 return FloatToBits(Node->getValueAPF().convertToFloat());
2849
2850 return -1;
2851 }
2852
2853 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00002854}
2855
Christian Konig8e06e2a2013-04-10 08:39:08 +00002856/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00002857static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00002858 switch (Idx) {
2859 default: return 0;
2860 case AMDGPU::sub0: return 0;
2861 case AMDGPU::sub1: return 1;
2862 case AMDGPU::sub2: return 2;
2863 case AMDGPU::sub3: return 3;
2864 }
2865}
2866
2867/// \brief Adjust the writemask of MIMG instructions
2868void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
2869 SelectionDAG &DAG) const {
2870 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00002871 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002872 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
2873 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00002874 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002875
2876 // Try to figure out the used register components
2877 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
2878 I != E; ++I) {
2879
2880 // Abort if we can't understand the usage
2881 if (!I->isMachineOpcode() ||
2882 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
2883 return;
2884
Tom Stellard54774e52013-10-23 02:53:47 +00002885 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
2886 // Note that subregs are packed, i.e. Lane==0 is the first bit set
2887 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
2888 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00002889 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00002890
Tom Stellard54774e52013-10-23 02:53:47 +00002891 // Set which texture component corresponds to the lane.
2892 unsigned Comp;
2893 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
2894 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00002895 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00002896 Dmask &= ~(1 << Comp);
2897 }
2898
Christian Konig8e06e2a2013-04-10 08:39:08 +00002899 // Abort if we have more than one user per component
2900 if (Users[Lane])
2901 return;
2902
2903 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00002904 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002905 }
2906
Tom Stellard54774e52013-10-23 02:53:47 +00002907 // Abort if there's no change
2908 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00002909 return;
2910
2911 // Adjust the writemask in the node
2912 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002913 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002914 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002915 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00002916 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002917
Christian Konig8b1ed282013-04-10 08:39:16 +00002918 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00002919 // (if NewDmask has only one bit set...)
2920 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002921 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
2922 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00002923 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002924 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00002925 SDValue(Node, 0), RC);
2926 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
2927 return;
2928 }
2929
Christian Konig8e06e2a2013-04-10 08:39:08 +00002930 // Update the users of the node with the new indices
2931 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
2932
2933 SDNode *User = Users[i];
2934 if (!User)
2935 continue;
2936
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002937 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002938 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
2939
2940 switch (Idx) {
2941 default: break;
2942 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
2943 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
2944 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
2945 }
2946 }
2947}
2948
Tom Stellardc98ee202015-07-16 19:40:07 +00002949static bool isFrameIndexOp(SDValue Op) {
2950 if (Op.getOpcode() == ISD::AssertZext)
2951 Op = Op.getOperand(0);
2952
2953 return isa<FrameIndexSDNode>(Op);
2954}
2955
Tom Stellard3457a842014-10-09 19:06:00 +00002956/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
2957/// with frame index operands.
2958/// LLVM assumes that inputs are to these instructions are registers.
2959void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
2960 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002961
2962 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00002963 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00002964 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00002965 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002966 continue;
2967 }
2968
Tom Stellard3457a842014-10-09 19:06:00 +00002969 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002970 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00002971 Node->getOperand(i).getValueType(),
2972 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002973 }
2974
Tom Stellard3457a842014-10-09 19:06:00 +00002975 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002976}
2977
Matt Arsenault08d84942014-06-03 23:06:13 +00002978/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00002979SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
2980 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002981 const SIInstrInfo *TII =
2982 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00002983 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00002984
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00002985 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore())
Christian Konig8e06e2a2013-04-10 08:39:08 +00002986 adjustWritemask(Node, DAG);
2987
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00002988 if (Opcode == AMDGPU::INSERT_SUBREG ||
2989 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002990 legalizeTargetIndependentNode(Node, DAG);
2991 return Node;
2992 }
Tom Stellard654d6692015-01-08 15:08:17 +00002993 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002994}
Christian Konig8b1ed282013-04-10 08:39:16 +00002995
2996/// \brief Assign the register class depending on the number of
2997/// bits set in the writemask
2998void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
2999 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00003000 const SIInstrInfo *TII =
3001 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003002
Tom Stellarda99ada52014-11-21 22:31:44 +00003003 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00003004
3005 if (TII->isVOP3(MI->getOpcode())) {
3006 // Make sure constant bus requirements are respected.
3007 TII->legalizeOperandsVOP3(MRI, MI);
3008 return;
3009 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00003010
Matt Arsenault3add6432015-10-20 04:35:43 +00003011 if (TII->isMIMG(*MI)) {
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003012 unsigned VReg = MI->getOperand(0).getReg();
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003013 unsigned DmaskIdx = MI->getNumOperands() == 12 ? 3 : 4;
3014 unsigned Writemask = MI->getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003015 unsigned BitsSet = 0;
3016 for (unsigned i = 0; i < 4; ++i)
3017 BitsSet += Writemask & (1 << i) ? 1 : 0;
3018
3019 const TargetRegisterClass *RC;
3020 switch (BitsSet) {
3021 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00003022 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003023 case 2: RC = &AMDGPU::VReg_64RegClass; break;
3024 case 3: RC = &AMDGPU::VReg_96RegClass; break;
3025 }
3026
3027 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
3028 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003029 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00003030 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00003031 }
3032
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003033 // Replace unused atomics with the no return version.
3034 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
3035 if (NoRetAtomicOp != -1) {
3036 if (!Node->hasAnyUseOfValue(0)) {
3037 MI->setDesc(TII->get(NoRetAtomicOp));
3038 MI->RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003039 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003040 }
3041
Tom Stellard354a43c2016-04-01 18:27:37 +00003042 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
3043 // instruction, because the return type of these instructions is a vec2 of
3044 // the memory type, so it can be tied to the input operand.
3045 // This means these instructions always have a use, so we need to add a
3046 // special case to check if the atomic has only one extract_subreg use,
3047 // which itself has no uses.
3048 if ((Node->hasNUsesOfValue(1, 0) &&
3049 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
3050 !Node->use_begin()->hasAnyUseOfValue(0))) {
3051 unsigned Def = MI->getOperand(0).getReg();
3052
3053 // Change this into a noret atomic.
3054 MI->setDesc(TII->get(NoRetAtomicOp));
3055 MI->RemoveOperand(0);
3056
3057 // If we only remove the def operand from the atomic instruction, the
3058 // extract_subreg will be left with a use of a vreg without a def.
3059 // So we need to insert an implicit_def to avoid machine verifier
3060 // errors.
3061 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
3062 TII->get(AMDGPU::IMPLICIT_DEF), Def);
3063 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003064 return;
3065 }
Christian Konig8b1ed282013-04-10 08:39:16 +00003066}
Tom Stellard0518ff82013-06-03 17:39:58 +00003067
Matt Arsenault485defe2014-11-05 19:01:17 +00003068static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003069 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00003070 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
3071}
3072
3073MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
3074 SDLoc DL,
3075 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00003076 const SIInstrInfo *TII =
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003077 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00003078
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003079 // Build the half of the subregister with the constants before building the
3080 // full 128-bit register. If we are building multiple resource descriptors,
3081 // this will allow CSEing of the 2-component register.
3082 const SDValue Ops0[] = {
3083 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
3084 buildSMovImm32(DAG, DL, 0),
3085 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
3086 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
3087 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
3088 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003089
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003090 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
3091 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00003092
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003093 // Combine the constants and the pointer.
3094 const SDValue Ops1[] = {
3095 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
3096 Ptr,
3097 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
3098 SubRegHi,
3099 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
3100 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003101
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003102 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00003103}
3104
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003105/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003106/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
3107/// of the resource descriptor) to create an offset, which is added to
3108/// the resource pointer.
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003109MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
3110 SDLoc DL,
3111 SDValue Ptr,
3112 uint32_t RsrcDword1,
3113 uint64_t RsrcDword2And3) const {
3114 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
3115 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
3116 if (RsrcDword1) {
3117 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003118 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
3119 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003120 }
3121
3122 SDValue DataLo = buildSMovImm32(DAG, DL,
3123 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
3124 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
3125
3126 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003127 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003128 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003129 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003130 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003131 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003132 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003133 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003134 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003135 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003136 };
3137
3138 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
3139}
3140
Tom Stellard94593ee2013-06-03 17:40:18 +00003141SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3142 const TargetRegisterClass *RC,
3143 unsigned Reg, EVT VT) const {
3144 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
3145
3146 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
3147 cast<RegisterSDNode>(VReg)->getReg(), VT);
3148}
Tom Stellardd7e6f132015-04-08 01:09:26 +00003149
3150//===----------------------------------------------------------------------===//
3151// SI Inline Assembly Support
3152//===----------------------------------------------------------------------===//
3153
3154std::pair<unsigned, const TargetRegisterClass *>
3155SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003156 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00003157 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003158
3159 if (Constraint.size() == 1) {
3160 switch (Constraint[0]) {
3161 case 's':
3162 case 'r':
3163 switch (VT.getSizeInBits()) {
3164 default:
3165 return std::make_pair(0U, nullptr);
3166 case 32:
Tom Stellardd7e6f132015-04-08 01:09:26 +00003167 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003168 case 64:
3169 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
3170 case 128:
3171 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
3172 case 256:
3173 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
3174 }
3175
3176 case 'v':
3177 switch (VT.getSizeInBits()) {
3178 default:
3179 return std::make_pair(0U, nullptr);
3180 case 32:
3181 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
3182 case 64:
3183 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
3184 case 96:
3185 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
3186 case 128:
3187 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
3188 case 256:
3189 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
3190 case 512:
3191 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
3192 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00003193 }
3194 }
3195
3196 if (Constraint.size() > 1) {
3197 const TargetRegisterClass *RC = nullptr;
3198 if (Constraint[1] == 'v') {
3199 RC = &AMDGPU::VGPR_32RegClass;
3200 } else if (Constraint[1] == 's') {
3201 RC = &AMDGPU::SGPR_32RegClass;
3202 }
3203
3204 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00003205 uint32_t Idx;
3206 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
3207 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00003208 return std::make_pair(RC->getRegister(Idx), RC);
3209 }
3210 }
3211 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
3212}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003213
3214SITargetLowering::ConstraintType
3215SITargetLowering::getConstraintType(StringRef Constraint) const {
3216 if (Constraint.size() == 1) {
3217 switch (Constraint[0]) {
3218 default: break;
3219 case 's':
3220 case 'v':
3221 return C_RegisterClass;
3222 }
3223 }
3224 return TargetLowering::getConstraintType(Constraint);
3225}