blob: ee7ad3293d9cabc13949d248dc38b667835e10bd [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
Tom Stellard75aadc22012-12-11 21:25:42 +00001106MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
1107 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +00001108
Tom Stellard75aadc22012-12-11 21:25:42 +00001109 switch (MI->getOpcode()) {
Matt Arsenault20711b72015-02-20 22:10:45 +00001110 case AMDGPU::BRANCH:
1111 return BB;
Changpeng Fang01f60622016-03-15 17:28:44 +00001112 case AMDGPU::GET_GROUPSTATICSIZE: {
1113 const SIInstrInfo *TII =
1114 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
1115 MachineFunction *MF = BB->getParent();
1116 SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1117 DebugLoc DL = MI->getDebugLoc();
1118 BuildMI (*BB, MI, DL, TII->get(AMDGPU::S_MOVK_I32))
1119 .addOperand(MI->getOperand(0))
1120 .addImm(MFI->LDSSize);
1121 MI->eraseFromParent();
1122 return BB;
1123 }
1124 default:
1125 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Tom Stellard75aadc22012-12-11 21:25:42 +00001126 }
1127 return BB;
1128}
1129
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001130bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1131 // This currently forces unfolding various combinations of fsub into fma with
1132 // free fneg'd operands. As long as we have fast FMA (controlled by
1133 // isFMAFasterThanFMulAndFAdd), we should perform these.
1134
1135 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1136 // most of these combines appear to be cycle neutral but save on instruction
1137 // count / code size.
1138 return true;
1139}
1140
Mehdi Amini44ede332015-07-09 02:09:04 +00001141EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1142 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001143 if (!VT.isVector()) {
1144 return MVT::i1;
1145 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001146 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001147}
1148
Mehdi Aminieaabc512015-07-09 15:12:23 +00001149MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001150 return MVT::i32;
1151}
1152
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001153// Answering this is somewhat tricky and depends on the specific device which
1154// have different rates for fma or all f64 operations.
1155//
1156// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1157// regardless of which device (although the number of cycles differs between
1158// devices), so it is always profitable for f64.
1159//
1160// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1161// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1162// which we can always do even without fused FP ops since it returns the same
1163// result as the separate operations and since it is always full
1164// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1165// however does not support denormals, so we do report fma as faster if we have
1166// a fast fma device and require denormals.
1167//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001168bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1169 VT = VT.getScalarType();
1170
1171 if (!VT.isSimple())
1172 return false;
1173
1174 switch (VT.getSimpleVT().SimpleTy) {
1175 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001176 // This is as fast on some subtargets. However, we always have full rate f32
1177 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001178 // which we should prefer over fma. We can't use this if we want to support
1179 // denormals, so only report this in these cases.
1180 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001181 case MVT::f64:
1182 return true;
1183 default:
1184 break;
1185 }
1186
1187 return false;
1188}
1189
Tom Stellard75aadc22012-12-11 21:25:42 +00001190//===----------------------------------------------------------------------===//
1191// Custom DAG Lowering Operations
1192//===----------------------------------------------------------------------===//
1193
1194SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1195 switch (Op.getOpcode()) {
1196 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001197 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001198 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001199 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001200 SDValue Result = LowerLOAD(Op, DAG);
1201 assert((!Result.getNode() ||
1202 Result.getNode()->getNumValues() == 2) &&
1203 "Load should return a value and a chain");
1204 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001205 }
Tom Stellardaf775432013-10-23 00:44:32 +00001206
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001207 case ISD::FSIN:
1208 case ISD::FCOS:
1209 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001210 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001211 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard354a43c2016-04-01 18:27:37 +00001212 case ISD::ATOMIC_CMP_SWAP: return LowerATOMIC_CMP_SWAP(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001213 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001214 case ISD::GlobalAddress: {
1215 MachineFunction &MF = DAG.getMachineFunction();
1216 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1217 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001218 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001219 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001220 case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001221 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001222 }
1223 return SDValue();
1224}
1225
Tom Stellardf8794352012-12-19 22:10:31 +00001226/// \brief Helper function for LowerBRCOND
1227static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001228
Tom Stellardf8794352012-12-19 22:10:31 +00001229 SDNode *Parent = Value.getNode();
1230 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1231 I != E; ++I) {
1232
1233 if (I.getUse().get() != Value)
1234 continue;
1235
1236 if (I->getOpcode() == Opcode)
1237 return *I;
1238 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001239 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001240}
1241
Tom Stellardb02094e2014-07-21 15:45:01 +00001242SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
1243
Tom Stellardc98ee202015-07-16 19:40:07 +00001244 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +00001245 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
1246 unsigned FrameIndex = FINode->getIndex();
1247
Matt Arsenault3a619852016-02-27 20:26:57 +00001248 // A FrameIndex node represents a 32-bit offset into scratch memory. If the
1249 // high bit of a frame index offset were to be set, this would mean that it
1250 // represented an offset of ~2GB * 64 = ~128GB from the start of the scratch
1251 // buffer, with 64 being the number of threads per wave.
Tom Stellardc98ee202015-07-16 19:40:07 +00001252 //
Matt Arsenault3a619852016-02-27 20:26:57 +00001253 // The maximum private allocation for the entire GPU is 4G, and we are
1254 // concerned with the largest the index could ever be for an individual
1255 // workitem. This will occur with the minmum dispatch size. If a program
1256 // requires more, the dispatch size will be reduced.
1257 //
1258 // With this limit, we can mark the high bit of the FrameIndex node as known
1259 // zero, which is important, because it means in most situations we can prove
1260 // that values derived from FrameIndex nodes are non-negative. This enables us
1261 // to take advantage of more addressing modes when accessing scratch buffers,
1262 // since for scratch reads/writes, the register offset must always be
1263 // positive.
1264
1265 uint64_t MaxGPUAlloc = UINT64_C(4) * 1024 * 1024 * 1024;
1266
1267 // XXX - It is unclear if partial dispatch works. Assume it works at half wave
1268 // granularity. It is probably a full wave.
1269 uint64_t MinGranularity = 32;
1270
1271 unsigned KnownBits = Log2_64(MaxGPUAlloc / MinGranularity);
1272 EVT ExtVT = EVT::getIntegerVT(*DAG.getContext(), KnownBits);
Tom Stellardc98ee202015-07-16 19:40:07 +00001273
1274 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
Tom Stellardc98ee202015-07-16 19:40:07 +00001275 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
Matt Arsenault3a619852016-02-27 20:26:57 +00001276 DAG.getValueType(ExtVT));
Tom Stellardb02094e2014-07-21 15:45:01 +00001277}
1278
Tom Stellardbc4497b2016-02-12 23:45:29 +00001279bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault16f48d72016-02-13 00:36:10 +00001280 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN)
Tom Stellardbc4497b2016-02-12 23:45:29 +00001281 return false;
1282
1283 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1284 default: return false;
1285 case AMDGPUIntrinsic::amdgcn_if:
1286 case AMDGPUIntrinsic::amdgcn_else:
1287 case AMDGPUIntrinsic::amdgcn_break:
1288 case AMDGPUIntrinsic::amdgcn_if_break:
1289 case AMDGPUIntrinsic::amdgcn_else_break:
1290 case AMDGPUIntrinsic::amdgcn_loop:
1291 case AMDGPUIntrinsic::amdgcn_end_cf:
1292 return true;
1293 }
1294}
1295
Tom Stellardf8794352012-12-19 22:10:31 +00001296/// This transforms the control flow intrinsics to get the branch destination as
1297/// last parameter, also switches branch target with BR if the need arise
1298SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1299 SelectionDAG &DAG) const {
1300
Andrew Trickef9de2a2013-05-25 02:42:55 +00001301 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001302
1303 SDNode *Intr = BRCOND.getOperand(1).getNode();
1304 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001305 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001306 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001307
1308 if (Intr->getOpcode() == ISD::SETCC) {
1309 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001310 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001311 Intr = SetCC->getOperand(0).getNode();
1312
1313 } else {
1314 // Get the target from BR if we don't negate the condition
1315 BR = findUser(BRCOND, ISD::BR);
1316 Target = BR->getOperand(1);
1317 }
1318
Tom Stellardbc4497b2016-02-12 23:45:29 +00001319 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN) {
1320 // This is a uniform branch so we don't need to legalize.
1321 return BRCOND;
1322 }
1323
1324 assert(!SetCC ||
1325 (SetCC->getConstantOperandVal(1) == 1 &&
1326 isCFIntrinsic(Intr) &&
1327 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1328 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001329
1330 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001331 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001332
1333 // operands of the new intrinsic call
1334 SmallVector<SDValue, 4> Ops;
1335 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001336 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001337 Ops.push_back(Target);
1338
1339 // build the new intrinsic call
1340 SDNode *Result = DAG.getNode(
1341 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001342 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001343
1344 if (BR) {
1345 // Give the branch instruction our target
1346 SDValue Ops[] = {
1347 BR->getOperand(0),
1348 BRCOND.getOperand(2)
1349 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001350 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1351 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1352 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001353 }
1354
1355 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1356
1357 // Copy the intrinsic results to registers
1358 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1359 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1360 if (!CopyToReg)
1361 continue;
1362
1363 Chain = DAG.getCopyToReg(
1364 Chain, DL,
1365 CopyToReg->getOperand(1),
1366 SDValue(Result, i - 1),
1367 SDValue());
1368
1369 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1370 }
1371
1372 // Remove the old intrinsic from the chain
1373 DAG.ReplaceAllUsesOfValueWith(
1374 SDValue(Intr, Intr->getNumValues() - 1),
1375 Intr->getOperand(0));
1376
1377 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001378}
1379
Tom Stellard067c8152014-07-21 14:01:14 +00001380SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1381 SDValue Op,
1382 SelectionDAG &DAG) const {
1383 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1384
1385 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
1386 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1387
1388 SDLoc DL(GSD);
1389 const GlobalValue *GV = GSD->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001390 MVT PtrVT = getPointerTy(DAG.getDataLayout(), GSD->getAddressSpace());
Tom Stellard067c8152014-07-21 14:01:14 +00001391
Tom Stellard067c8152014-07-21 14:01:14 +00001392 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
Tom Stellardc93fc112015-12-10 02:13:01 +00001393 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT, GA);
Tom Stellard067c8152014-07-21 14:01:14 +00001394}
1395
Tom Stellardfc92e772015-05-12 14:18:14 +00001396SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL,
1397 SDValue V) const {
1398 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1399 // so we will end up with redundant moves to m0.
1400 //
1401 // We can't use S_MOV_B32, because there is no way to specify m0 as the
1402 // destination register.
1403 //
1404 // We have to use them both. Machine cse will combine all the S_MOV_B32
1405 // instructions and the register coalescer eliminate the extra copies.
1406 SDNode *M0 = DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, V.getValueType(), V);
1407 return DAG.getCopyToReg(Chain, DL, DAG.getRegister(AMDGPU::M0, MVT::i32),
1408 SDValue(M0, 0), SDValue()); // Glue
1409 // A Null SDValue creates
1410 // a glue result.
1411}
1412
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001413SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1414 SDValue Op,
1415 MVT VT,
1416 unsigned Offset) const {
1417 SDLoc SL(Op);
1418 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1419 DAG.getEntryNode(), Offset, false);
1420 // The local size values will have the hi 16-bits as zero.
1421 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1422 DAG.getValueType(VT));
1423}
1424
Matt Arsenaulte0132462016-01-30 05:19:45 +00001425static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, EVT VT) {
1426 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
1427 "non-hsa intrinsic with hsa target");
1428 DAG.getContext()->diagnose(BadIntrin);
1429 return DAG.getUNDEF(VT);
1430}
1431
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001432SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1433 SelectionDAG &DAG) const {
1434 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001435 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardec2e43c2014-09-22 15:35:29 +00001436 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +00001437 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001438
1439 EVT VT = Op.getValueType();
1440 SDLoc DL(Op);
1441 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1442
Sanjay Patela2607012015-09-16 16:31:21 +00001443 // TODO: Should this propagate fast-math-flags?
1444
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001445 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001446 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault800fecf2016-01-11 21:18:33 +00001447 if (!Subtarget->isAmdHsaOS()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00001448 DiagnosticInfoUnsupported BadIntrin(
1449 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
1450 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00001451 DAG.getContext()->diagnose(BadIntrin);
1452 return DAG.getUNDEF(VT);
1453 }
1454
Tom Stellard48f29f22015-11-26 00:43:29 +00001455 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
1456 TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_PTR), VT);
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001457 case Intrinsic::amdgcn_rcp:
1458 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
1459 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001460 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001461 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenault79963e82016-02-13 01:03:00 +00001462 case Intrinsic::amdgcn_rsq_clamp:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001463 case AMDGPUIntrinsic::AMDGPU_rsq_clamped: { // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001464 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00001465 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00001466
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001467 Type *Type = VT.getTypeForEVT(*DAG.getContext());
1468 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
1469 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
1470
1471 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
1472 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
1473 DAG.getConstantFP(Max, DL, VT));
1474 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
1475 DAG.getConstantFP(Min, DL, VT));
1476 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001477 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001478 if (Subtarget->isAmdHsaOS())
1479 return emitNonHSAIntrinsicError(DAG, VT);
1480
Tom Stellardec2e43c2014-09-22 15:35:29 +00001481 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1482 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001483 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001484 if (Subtarget->isAmdHsaOS())
1485 return emitNonHSAIntrinsicError(DAG, VT);
1486
Tom Stellardec2e43c2014-09-22 15:35:29 +00001487 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1488 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001489 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001490 if (Subtarget->isAmdHsaOS())
1491 return emitNonHSAIntrinsicError(DAG, VT);
1492
Tom Stellardec2e43c2014-09-22 15:35:29 +00001493 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1494 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001495 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001496 if (Subtarget->isAmdHsaOS())
1497 return emitNonHSAIntrinsicError(DAG, VT);
1498
Tom Stellardec2e43c2014-09-22 15:35:29 +00001499 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1500 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001501 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001502 if (Subtarget->isAmdHsaOS())
1503 return emitNonHSAIntrinsicError(DAG, VT);
1504
Tom Stellardec2e43c2014-09-22 15:35:29 +00001505 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1506 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001507 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001508 if (Subtarget->isAmdHsaOS())
1509 return emitNonHSAIntrinsicError(DAG, VT);
1510
Tom Stellardec2e43c2014-09-22 15:35:29 +00001511 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1512 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001513 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001514 if (Subtarget->isAmdHsaOS())
1515 return emitNonHSAIntrinsicError(DAG, VT);
1516
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001517 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1518 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001519 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001520 if (Subtarget->isAmdHsaOS())
1521 return emitNonHSAIntrinsicError(DAG, VT);
1522
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001523 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1524 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001525 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001526 if (Subtarget->isAmdHsaOS())
1527 return emitNonHSAIntrinsicError(DAG, VT);
1528
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001529 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1530 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenaultbef34e22016-01-22 21:30:34 +00001531 case Intrinsic::amdgcn_read_workdim:
1532 case AMDGPUIntrinsic::AMDGPU_read_workdim: // Legacy name.
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001533 // Really only 2 bits.
1534 return lowerImplicitZextParam(DAG, Op, MVT::i8,
1535 getImplicitParameterOffset(MFI, GRID_DIM));
Matt Arsenault43976df2016-01-30 04:25:19 +00001536 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001537 case Intrinsic::r600_read_tgid_x:
1538 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001539 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001540 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001541 case Intrinsic::r600_read_tgid_y:
1542 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001543 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001544 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001545 case Intrinsic::r600_read_tgid_z:
1546 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001547 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001548 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001549 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001550 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001551 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001552 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001553 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001554 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001555 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001556 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001557 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001558 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001559 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001560 case AMDGPUIntrinsic::SI_load_const: {
1561 SDValue Ops[] = {
1562 Op.getOperand(1),
1563 Op.getOperand(2)
1564 };
1565
1566 MachineMemOperand *MMO = MF.getMachineMemOperand(
1567 MachinePointerInfo(),
1568 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
1569 VT.getStoreSize(), 4);
1570 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
1571 Op->getVTList(), Ops, VT, MMO);
1572 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001573 case AMDGPUIntrinsic::SI_vs_load_input:
1574 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1575 Op.getOperand(1),
1576 Op.getOperand(2),
1577 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00001578
Tom Stellard2a9d9472015-05-12 15:00:46 +00001579 case AMDGPUIntrinsic::SI_fs_constant: {
1580 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1581 SDValue Glue = M0.getValue(1);
1582 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
1583 DAG.getConstant(2, DL, MVT::i32), // P0
1584 Op.getOperand(1), Op.getOperand(2), Glue);
1585 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00001586 case AMDGPUIntrinsic::SI_packf16:
1587 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
1588 return DAG.getUNDEF(MVT::i32);
1589 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00001590 case AMDGPUIntrinsic::SI_fs_interp: {
1591 SDValue IJ = Op.getOperand(4);
1592 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1593 DAG.getConstant(0, DL, MVT::i32));
1594 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1595 DAG.getConstant(1, DL, MVT::i32));
1596 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1597 SDValue Glue = M0.getValue(1);
1598 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
1599 DAG.getVTList(MVT::f32, MVT::Glue),
1600 I, Op.getOperand(1), Op.getOperand(2), Glue);
1601 Glue = SDValue(P1.getNode(), 1);
1602 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
1603 Op.getOperand(1), Op.getOperand(2), Glue);
1604 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00001605 case Intrinsic::amdgcn_interp_p1: {
1606 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
1607 SDValue Glue = M0.getValue(1);
1608 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
1609 Op.getOperand(2), Op.getOperand(3), Glue);
1610 }
1611 case Intrinsic::amdgcn_interp_p2: {
1612 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
1613 SDValue Glue = SDValue(M0.getNode(), 1);
1614 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
1615 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
1616 Glue);
1617 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00001618 case Intrinsic::amdgcn_sin:
1619 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
1620
1621 case Intrinsic::amdgcn_cos:
1622 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
1623
1624 case Intrinsic::amdgcn_log_clamp: {
1625 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
1626 return SDValue();
1627
1628 DiagnosticInfoUnsupported BadIntrin(
1629 *MF.getFunction(), "intrinsic not supported on subtarget",
1630 DL.getDebugLoc());
1631 DAG.getContext()->diagnose(BadIntrin);
1632 return DAG.getUNDEF(VT);
1633 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001634 case Intrinsic::amdgcn_ldexp:
1635 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
1636 Op.getOperand(1), Op.getOperand(2));
1637 case Intrinsic::amdgcn_class:
1638 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
1639 Op.getOperand(1), Op.getOperand(2));
1640 case Intrinsic::amdgcn_div_fmas:
1641 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
1642 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
1643 Op.getOperand(4));
1644
1645 case Intrinsic::amdgcn_div_fixup:
1646 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
1647 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
1648
1649 case Intrinsic::amdgcn_trig_preop:
1650 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
1651 Op.getOperand(1), Op.getOperand(2));
1652 case Intrinsic::amdgcn_div_scale: {
1653 // 3rd parameter required to be a constant.
1654 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1655 if (!Param)
1656 return DAG.getUNDEF(VT);
1657
1658 // Translate to the operands expected by the machine instruction. The
1659 // first parameter must be the same as the first instruction.
1660 SDValue Numerator = Op.getOperand(1);
1661 SDValue Denominator = Op.getOperand(2);
1662
1663 // Note this order is opposite of the machine instruction's operations,
1664 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
1665 // intrinsic has the numerator as the first operand to match a normal
1666 // division operation.
1667
1668 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
1669
1670 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
1671 Denominator, Numerator);
1672 }
1673 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte0:
1674 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Op.getOperand(1));
1675 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte1:
1676 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE1, DL, VT, Op.getOperand(1));
1677 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte2:
1678 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE2, DL, VT, Op.getOperand(1));
1679 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte3:
1680 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE3, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001681 default:
1682 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1683 }
1684}
1685
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00001686SDValue SITargetLowering::LowerINTRINSIC_W_CHAIN(SDValue Op,
1687 SelectionDAG &DAG) const {
1688 unsigned IntrID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1689 switch (IntrID) {
1690 case Intrinsic::amdgcn_atomic_inc:
1691 case Intrinsic::amdgcn_atomic_dec: {
1692 MemSDNode *M = cast<MemSDNode>(Op);
1693 unsigned Opc = (IntrID == Intrinsic::amdgcn_atomic_inc) ?
1694 AMDGPUISD::ATOMIC_INC : AMDGPUISD::ATOMIC_DEC;
1695 SDValue Ops[] = {
1696 M->getOperand(0), // Chain
1697 M->getOperand(2), // Ptr
1698 M->getOperand(3) // Value
1699 };
1700
1701 return DAG.getMemIntrinsicNode(Opc, SDLoc(Op), M->getVTList(), Ops,
1702 M->getMemoryVT(), M->getMemOperand());
1703 }
1704 default:
1705 return SDValue();
1706 }
1707}
1708
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001709SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1710 SelectionDAG &DAG) const {
1711 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00001712 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001713 SDValue Chain = Op.getOperand(0);
1714 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1715
1716 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00001717 case AMDGPUIntrinsic::SI_sendmsg: {
1718 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1719 SDValue Glue = Chain.getValue(1);
1720 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1721 Op.getOperand(2), Glue);
1722 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001723 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001724 SDValue Ops[] = {
1725 Chain,
1726 Op.getOperand(2),
1727 Op.getOperand(3),
1728 Op.getOperand(4),
1729 Op.getOperand(5),
1730 Op.getOperand(6),
1731 Op.getOperand(7),
1732 Op.getOperand(8),
1733 Op.getOperand(9),
1734 Op.getOperand(10),
1735 Op.getOperand(11),
1736 Op.getOperand(12),
1737 Op.getOperand(13),
1738 Op.getOperand(14)
1739 };
1740
1741 EVT VT = Op.getOperand(3).getValueType();
1742
1743 MachineMemOperand *MMO = MF.getMachineMemOperand(
1744 MachinePointerInfo(),
1745 MachineMemOperand::MOStore,
1746 VT.getStoreSize(), 4);
1747 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1748 Op->getVTList(), Ops, VT, MMO);
1749 }
1750 default:
1751 return SDValue();
1752 }
1753}
1754
Tom Stellard81d871d2013-11-13 23:36:50 +00001755SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1756 SDLoc DL(Op);
1757 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001758 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00001759 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00001760
Matt Arsenaulta1436412016-02-10 18:21:45 +00001761 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
1762 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected");
Matt Arsenault6dfda962016-02-10 18:21:39 +00001763 // FIXME: Copied from PPC
1764 // First, load into 32 bits, then truncate to 1 bit.
1765
1766 SDValue Chain = Load->getChain();
1767 SDValue BasePtr = Load->getBasePtr();
1768 MachineMemOperand *MMO = Load->getMemOperand();
1769
1770 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
1771 BasePtr, MVT::i8, MMO);
1772
1773 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00001774 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00001775 NewLD.getValue(1)
1776 };
1777
1778 return DAG.getMergeValues(Ops, DL);
1779 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001780
Matt Arsenaulta1436412016-02-10 18:21:45 +00001781 if (!MemVT.isVector())
1782 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001783
Matt Arsenaulta1436412016-02-10 18:21:45 +00001784 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1785 "Custom lowering for non-i32 vectors hasn't been implemented.");
1786 unsigned NumElements = MemVT.getVectorNumElements();
1787 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001788
Matt Arsenaulta1436412016-02-10 18:21:45 +00001789 switch (Load->getAddressSpace()) {
1790 case AMDGPUAS::CONSTANT_ADDRESS:
1791 if (isMemOpUniform(Load))
1792 return SDValue();
1793 // Non-uniform loads will be selected to MUBUF instructions, so they
1794 // have the same legalization requires ments as global and private
1795 // loads.
1796 //
1797 // Fall-through
1798 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001799 case AMDGPUAS::FLAT_ADDRESS:
1800 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00001801 return SplitVectorLoad(Op, DAG);
1802 // v4 loads are supported for private and global memory.
1803 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001804 case AMDGPUAS::PRIVATE_ADDRESS: {
1805 // Depending on the setting of the private_element_size field in the
1806 // resource descriptor, we can only make private accesses up to a certain
1807 // size.
1808 switch (Subtarget->getMaxPrivateElementSize()) {
1809 case 4:
1810 return ScalarizeVectorLoad(Op, DAG);
1811 case 8:
1812 if (NumElements > 2)
1813 return SplitVectorLoad(Op, DAG);
1814 return SDValue();
1815 case 16:
1816 // Same as global/flat
1817 if (NumElements > 4)
1818 return SplitVectorLoad(Op, DAG);
1819 return SDValue();
1820 default:
1821 llvm_unreachable("unsupported private_element_size");
1822 }
1823 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00001824 case AMDGPUAS::LOCAL_ADDRESS:
1825 // If properly aligned, if we split we might be able to use ds_read_b64.
1826 return SplitVectorLoad(Op, DAG);
1827 default:
1828 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00001829 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001830}
1831
Tom Stellard0ec134f2014-02-04 17:18:40 +00001832SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1833 if (Op.getValueType() != MVT::i64)
1834 return SDValue();
1835
1836 SDLoc DL(Op);
1837 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001838
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001839 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1840 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001841
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001842 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1843 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1844
1845 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1846 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001847
1848 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1849
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001850 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1851 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001852
1853 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1854
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001855 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1856 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001857}
1858
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001859// Catch division cases where we can use shortcuts with rcp and rsq
1860// instructions.
1861SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001862 SDLoc SL(Op);
1863 SDValue LHS = Op.getOperand(0);
1864 SDValue RHS = Op.getOperand(1);
1865 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001866 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001867
1868 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001869 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1870 CLHS->isExactlyValue(1.0)) {
1871 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1872 // the CI documentation has a worst case error of 1 ulp.
1873 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1874 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001875
1876 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001877 //
1878 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1879 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001880 if (RHS.getOpcode() == ISD::FSQRT)
1881 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1882
1883 // 1.0 / x -> rcp(x)
1884 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1885 }
1886 }
1887
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001888 if (Unsafe) {
1889 // Turn into multiply by the reciprocal.
1890 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00001891 SDNodeFlags Flags;
1892 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001893 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00001894 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001895 }
1896
1897 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001898}
1899
1900SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001901 if (SDValue FastLowered = LowerFastFDIV(Op, DAG))
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001902 return FastLowered;
1903
1904 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1905 // selection error for now rather than do something incorrect.
1906 if (Subtarget->hasFP32Denormals())
1907 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001908
1909 SDLoc SL(Op);
1910 SDValue LHS = Op.getOperand(0);
1911 SDValue RHS = Op.getOperand(1);
1912
1913 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1914
1915 const APFloat K0Val(BitsToFloat(0x6f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001916 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001917
1918 const APFloat K1Val(BitsToFloat(0x2f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001919 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001920
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001921 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001922
Mehdi Amini44ede332015-07-09 02:09:04 +00001923 EVT SetCCVT =
1924 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001925
1926 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1927
1928 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1929
Sanjay Patela2607012015-09-16 16:31:21 +00001930 // TODO: Should this propagate fast-math-flags?
1931
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001932 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1933
1934 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1935
1936 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1937
1938 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1939}
1940
1941SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001942 if (DAG.getTarget().Options.UnsafeFPMath)
1943 return LowerFastFDIV(Op, DAG);
1944
1945 SDLoc SL(Op);
1946 SDValue X = Op.getOperand(0);
1947 SDValue Y = Op.getOperand(1);
1948
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001949 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001950
1951 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
1952
1953 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
1954
1955 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
1956
1957 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
1958
1959 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
1960
1961 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
1962
1963 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
1964
1965 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
1966
1967 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
1968 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
1969
1970 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
1971 NegDivScale0, Mul, DivScale1);
1972
1973 SDValue Scale;
1974
1975 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1976 // Workaround a hardware bug on SI where the condition output from div_scale
1977 // is not usable.
1978
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001979 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001980
1981 // Figure out if the scale to use for div_fmas.
1982 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1983 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
1984 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
1985 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
1986
1987 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
1988 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
1989
1990 SDValue Scale0Hi
1991 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
1992 SDValue Scale1Hi
1993 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
1994
1995 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
1996 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
1997 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
1998 } else {
1999 Scale = DivScale1.getValue(1);
2000 }
2001
2002 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
2003 Fma4, Fma3, Mul, Scale);
2004
2005 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00002006}
2007
2008SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
2009 EVT VT = Op.getValueType();
2010
2011 if (VT == MVT::f32)
2012 return LowerFDIV32(Op, DAG);
2013
2014 if (VT == MVT::f64)
2015 return LowerFDIV64(Op, DAG);
2016
2017 llvm_unreachable("Unexpected type for fdiv");
2018}
2019
Tom Stellard81d871d2013-11-13 23:36:50 +00002020SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2021 SDLoc DL(Op);
2022 StoreSDNode *Store = cast<StoreSDNode>(Op);
2023 EVT VT = Store->getMemoryVT();
2024
Matt Arsenault95245662016-02-11 05:32:46 +00002025 if (VT == MVT::i1) {
2026 return DAG.getTruncStore(Store->getChain(), DL,
2027 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
2028 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00002029 }
2030
Matt Arsenault95245662016-02-11 05:32:46 +00002031 assert(Store->getValue().getValueType().getScalarType() == MVT::i32);
Tom Stellard81d871d2013-11-13 23:36:50 +00002032
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002033 unsigned NumElements = VT.getVectorNumElements();
2034 switch (Store->getAddressSpace()) {
2035 case AMDGPUAS::GLOBAL_ADDRESS:
2036 case AMDGPUAS::FLAT_ADDRESS:
2037 if (NumElements > 4)
2038 return SplitVectorStore(Op, DAG);
2039 return SDValue();
2040 case AMDGPUAS::PRIVATE_ADDRESS: {
2041 switch (Subtarget->getMaxPrivateElementSize()) {
2042 case 4:
2043 return ScalarizeVectorStore(Op, DAG);
2044 case 8:
2045 if (NumElements > 2)
2046 return SplitVectorStore(Op, DAG);
2047 return SDValue();
2048 case 16:
2049 if (NumElements > 4)
2050 return SplitVectorStore(Op, DAG);
2051 return SDValue();
2052 default:
2053 llvm_unreachable("unsupported private_element_size");
2054 }
2055 }
2056 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenault95245662016-02-11 05:32:46 +00002057 // If properly aligned, if we split we might be able to use ds_write_b64.
2058 return SplitVectorStore(Op, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00002059 default:
2060 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00002061 }
Tom Stellard81d871d2013-11-13 23:36:50 +00002062}
2063
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002064SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002065 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002066 EVT VT = Op.getValueType();
2067 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00002068 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002069 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
2070 DAG.getNode(ISD::FMUL, DL, VT, Arg,
2071 DAG.getConstantFP(0.5/M_PI, DL,
2072 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00002073
2074 switch (Op.getOpcode()) {
2075 case ISD::FCOS:
2076 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
2077 case ISD::FSIN:
2078 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
2079 default:
2080 llvm_unreachable("Wrong trig opcode");
2081 }
2082}
2083
Tom Stellard354a43c2016-04-01 18:27:37 +00002084SDValue SITargetLowering::LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const {
2085 AtomicSDNode *AtomicNode = cast<AtomicSDNode>(Op);
2086 assert(AtomicNode->isCompareAndSwap());
2087 unsigned AS = AtomicNode->getAddressSpace();
2088
2089 // No custom lowering required for local address space
2090 if (!isFlatGlobalAddrSpace(AS))
2091 return Op;
2092
2093 // Non-local address space requires custom lowering for atomic compare
2094 // and swap; cmp and swap should be in a v2i32 or v2i64 in case of _X2
2095 SDLoc DL(Op);
2096 SDValue ChainIn = Op.getOperand(0);
2097 SDValue Addr = Op.getOperand(1);
2098 SDValue Old = Op.getOperand(2);
2099 SDValue New = Op.getOperand(3);
2100 EVT VT = Op.getValueType();
2101 MVT SimpleVT = VT.getSimpleVT();
2102 MVT VecType = MVT::getVectorVT(SimpleVT, 2);
2103
2104 SDValue NewOld = DAG.getNode(ISD::BUILD_VECTOR, DL, VecType,
2105 New, Old);
2106 SDValue Ops[] = { ChainIn, Addr, NewOld };
2107 SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2108 return DAG.getMemIntrinsicNode(AMDGPUISD::ATOMIC_CMP_SWAP, DL,
2109 VTList, Ops, VT, AtomicNode->getMemOperand());
2110}
2111
Tom Stellard75aadc22012-12-11 21:25:42 +00002112//===----------------------------------------------------------------------===//
2113// Custom DAG optimizations
2114//===----------------------------------------------------------------------===//
2115
Matt Arsenault364a6742014-06-11 17:50:44 +00002116SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00002117 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00002118 EVT VT = N->getValueType(0);
2119 EVT ScalarVT = VT.getScalarType();
2120 if (ScalarVT != MVT::f32)
2121 return SDValue();
2122
2123 SelectionDAG &DAG = DCI.DAG;
2124 SDLoc DL(N);
2125
2126 SDValue Src = N->getOperand(0);
2127 EVT SrcVT = Src.getValueType();
2128
2129 // TODO: We could try to match extracting the higher bytes, which would be
2130 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
2131 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
2132 // about in practice.
2133 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
2134 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
2135 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
2136 DCI.AddToWorklist(Cvt.getNode());
2137 return Cvt;
2138 }
2139 }
2140
2141 // We are primarily trying to catch operations on illegal vector types
2142 // before they are expanded.
2143 // For scalars, we can use the more flexible method of checking masked bits
2144 // after legalization.
2145 if (!DCI.isBeforeLegalize() ||
2146 !SrcVT.isVector() ||
2147 SrcVT.getVectorElementType() != MVT::i8) {
2148 return SDValue();
2149 }
2150
2151 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
2152
2153 // Weird sized vectors are a pain to handle, but we know 3 is really the same
2154 // size as 4.
2155 unsigned NElts = SrcVT.getVectorNumElements();
2156 if (!SrcVT.isSimple() && NElts != 3)
2157 return SDValue();
2158
2159 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
2160 // prevent a mess from expanding to v4i32 and repacking.
2161 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
2162 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
2163 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
2164 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00002165 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00002166
2167 unsigned AS = Load->getAddressSpace();
2168 unsigned Align = Load->getAlignment();
2169 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +00002170 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
Matt Arsenaulte6986632015-01-14 01:35:22 +00002171
2172 // Don't try to replace the load if we have to expand it due to alignment
2173 // problems. Otherwise we will end up scalarizing the load, and trying to
2174 // repack into the vector for no real reason.
2175 if (Align < ABIAlignment &&
2176 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
2177 return SDValue();
2178 }
2179
Matt Arsenault364a6742014-06-11 17:50:44 +00002180 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
2181 Load->getChain(),
2182 Load->getBasePtr(),
2183 LoadVT,
2184 Load->getMemOperand());
2185
2186 // Make sure successors of the original load stay after it by updating
2187 // them to use the new Chain.
2188 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
2189
2190 SmallVector<SDValue, 4> Elts;
2191 if (RegVT.isVector())
2192 DAG.ExtractVectorElements(NewLoad, Elts);
2193 else
2194 Elts.push_back(NewLoad);
2195
2196 SmallVector<SDValue, 4> Ops;
2197
2198 unsigned EltIdx = 0;
2199 for (SDValue Elt : Elts) {
2200 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
2201 for (unsigned I = 0; I < ComponentsInElt; ++I) {
2202 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
2203 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
2204 DCI.AddToWorklist(Cvt.getNode());
2205 Ops.push_back(Cvt);
2206 }
2207
2208 ++EltIdx;
2209 }
2210
2211 assert(Ops.size() == NElts);
2212
2213 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
2214 }
2215
2216 return SDValue();
2217}
2218
Eric Christopher6c5b5112015-03-11 18:43:21 +00002219/// \brief Return true if the given offset Size in bytes can be folded into
2220/// the immediate offsets of a memory instruction for the given address space.
2221static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
2222 const AMDGPUSubtarget &STI) {
2223 switch (AS) {
2224 case AMDGPUAS::GLOBAL_ADDRESS: {
2225 // MUBUF instructions a 12-bit offset in bytes.
2226 return isUInt<12>(OffsetSize);
2227 }
2228 case AMDGPUAS::CONSTANT_ADDRESS: {
2229 // SMRD instructions have an 8-bit offset in dwords on SI and
2230 // a 20-bit offset in bytes on VI.
2231 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
2232 return isUInt<20>(OffsetSize);
2233 else
2234 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
2235 }
2236 case AMDGPUAS::LOCAL_ADDRESS:
2237 case AMDGPUAS::REGION_ADDRESS: {
2238 // The single offset versions have a 16-bit offset in bytes.
2239 return isUInt<16>(OffsetSize);
2240 }
2241 case AMDGPUAS::PRIVATE_ADDRESS:
2242 // Indirect register addressing does not use any offsets.
2243 default:
2244 return 0;
2245 }
2246}
2247
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002248// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
2249
2250// This is a variant of
2251// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
2252//
2253// The normal DAG combiner will do this, but only if the add has one use since
2254// that would increase the number of instructions.
2255//
2256// This prevents us from seeing a constant offset that can be folded into a
2257// memory instruction's addressing mode. If we know the resulting add offset of
2258// a pointer can be folded into an addressing offset, we can replace the pointer
2259// operand with the add of new constant offset. This eliminates one of the uses,
2260// and may allow the remaining use to also be simplified.
2261//
2262SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
2263 unsigned AddrSpace,
2264 DAGCombinerInfo &DCI) const {
2265 SDValue N0 = N->getOperand(0);
2266 SDValue N1 = N->getOperand(1);
2267
2268 if (N0.getOpcode() != ISD::ADD)
2269 return SDValue();
2270
2271 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
2272 if (!CN1)
2273 return SDValue();
2274
2275 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2276 if (!CAdd)
2277 return SDValue();
2278
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002279 // If the resulting offset is too large, we can't fold it into the addressing
2280 // mode offset.
2281 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Eric Christopher6c5b5112015-03-11 18:43:21 +00002282 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002283 return SDValue();
2284
2285 SelectionDAG &DAG = DCI.DAG;
2286 SDLoc SL(N);
2287 EVT VT = N->getValueType(0);
2288
2289 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002290 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002291
2292 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
2293}
2294
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002295SDValue SITargetLowering::performAndCombine(SDNode *N,
2296 DAGCombinerInfo &DCI) const {
2297 if (DCI.isBeforeLegalize())
2298 return SDValue();
2299
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002300 if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI))
2301 return Base;
2302
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002303 SelectionDAG &DAG = DCI.DAG;
2304
2305 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
2306 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
2307 SDValue LHS = N->getOperand(0);
2308 SDValue RHS = N->getOperand(1);
2309
2310 if (LHS.getOpcode() == ISD::SETCC &&
2311 RHS.getOpcode() == ISD::SETCC) {
2312 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2313 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2314
2315 SDValue X = LHS.getOperand(0);
2316 SDValue Y = RHS.getOperand(0);
2317 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2318 return SDValue();
2319
2320 if (LCC == ISD::SETO) {
2321 if (X != LHS.getOperand(1))
2322 return SDValue();
2323
2324 if (RCC == ISD::SETUNE) {
2325 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2326 if (!C1 || !C1->isInfinity() || C1->isNegative())
2327 return SDValue();
2328
2329 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2330 SIInstrFlags::N_SUBNORMAL |
2331 SIInstrFlags::N_ZERO |
2332 SIInstrFlags::P_ZERO |
2333 SIInstrFlags::P_SUBNORMAL |
2334 SIInstrFlags::P_NORMAL;
2335
2336 static_assert(((~(SIInstrFlags::S_NAN |
2337 SIInstrFlags::Q_NAN |
2338 SIInstrFlags::N_INFINITY |
2339 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2340 "mask not equal");
2341
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002342 SDLoc DL(N);
2343 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2344 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002345 }
2346 }
2347 }
2348
2349 return SDValue();
2350}
2351
Matt Arsenaultf2290332015-01-06 23:00:39 +00002352SDValue SITargetLowering::performOrCombine(SDNode *N,
2353 DAGCombinerInfo &DCI) const {
2354 SelectionDAG &DAG = DCI.DAG;
2355 SDValue LHS = N->getOperand(0);
2356 SDValue RHS = N->getOperand(1);
2357
Matt Arsenault3b082382016-04-12 18:24:38 +00002358 EVT VT = N->getValueType(0);
2359 if (VT == MVT::i64) {
2360 // TODO: This could be a generic combine with a predicate for extracting the
2361 // high half of an integer being free.
2362
2363 // (or i64:x, (zero_extend i32:y)) ->
2364 // i64 (bitcast (v2i32 build_vector (or i32:y, lo_32(x)), hi_32(x)))
2365 if (LHS.getOpcode() == ISD::ZERO_EXTEND &&
2366 RHS.getOpcode() != ISD::ZERO_EXTEND)
2367 std::swap(LHS, RHS);
2368
2369 if (RHS.getOpcode() == ISD::ZERO_EXTEND) {
2370 SDValue ExtSrc = RHS.getOperand(0);
2371 EVT SrcVT = ExtSrc.getValueType();
2372 if (SrcVT == MVT::i32) {
2373 SDLoc SL(N);
2374 SDValue LowLHS, HiBits;
2375 std::tie(LowLHS, HiBits) = split64BitValue(LHS, DAG);
2376 SDValue LowOr = DAG.getNode(ISD::OR, SL, MVT::i32, LowLHS, ExtSrc);
2377
2378 DCI.AddToWorklist(LowOr.getNode());
2379 DCI.AddToWorklist(HiBits.getNode());
2380
2381 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
2382 LowOr, HiBits);
2383 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2384 }
2385 }
2386 }
2387
Matt Arsenaultf2290332015-01-06 23:00:39 +00002388 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
2389 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
2390 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
2391 SDValue Src = LHS.getOperand(0);
2392 if (Src != RHS.getOperand(0))
2393 return SDValue();
2394
2395 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
2396 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
2397 if (!CLHS || !CRHS)
2398 return SDValue();
2399
2400 // Only 10 bits are used.
2401 static const uint32_t MaxMask = 0x3ff;
2402
2403 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002404 SDLoc DL(N);
2405 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2406 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00002407 }
2408
2409 return SDValue();
2410}
2411
2412SDValue SITargetLowering::performClassCombine(SDNode *N,
2413 DAGCombinerInfo &DCI) const {
2414 SelectionDAG &DAG = DCI.DAG;
2415 SDValue Mask = N->getOperand(1);
2416
2417 // fp_class x, 0 -> false
2418 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
2419 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002420 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002421 }
2422
2423 return SDValue();
2424}
2425
Matt Arsenault9cd90712016-04-14 01:42:16 +00002426// Constant fold canonicalize.
2427SDValue SITargetLowering::performFCanonicalizeCombine(
2428 SDNode *N,
2429 DAGCombinerInfo &DCI) const {
2430 ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
2431 if (!CFP)
2432 return SDValue();
2433
2434 SelectionDAG &DAG = DCI.DAG;
2435 const APFloat &C = CFP->getValueAPF();
2436
2437 // Flush denormals to 0 if not enabled.
2438 if (C.isDenormal()) {
2439 EVT VT = N->getValueType(0);
2440 if (VT == MVT::f32 && !Subtarget->hasFP32Denormals())
2441 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2442
2443 if (VT == MVT::f64 && !Subtarget->hasFP64Denormals())
2444 return DAG.getConstantFP(0.0, SDLoc(N), VT);
2445 }
2446
2447 if (C.isNaN()) {
2448 EVT VT = N->getValueType(0);
2449 APFloat CanonicalQNaN = APFloat::getQNaN(C.getSemantics());
2450 if (C.isSignaling()) {
2451 // Quiet a signaling NaN.
2452 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
2453 }
2454
2455 // Make sure it is the canonical NaN bitpattern.
2456 //
2457 // TODO: Can we use -1 as the canonical NaN value since it's an inline
2458 // immediate?
2459 if (C.bitcastToAPInt() != CanonicalQNaN.bitcastToAPInt())
2460 return DAG.getConstantFP(CanonicalQNaN, SDLoc(N), VT);
2461 }
2462
2463 return SDValue(CFP, 0);
2464}
2465
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002466static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
2467 switch (Opc) {
2468 case ISD::FMAXNUM:
2469 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002470 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002471 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002472 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002473 return AMDGPUISD::UMAX3;
2474 case ISD::FMINNUM:
2475 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002476 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002477 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002478 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002479 return AMDGPUISD::UMIN3;
2480 default:
2481 llvm_unreachable("Not a min/max opcode");
2482 }
2483}
2484
Matt Arsenaultf639c322016-01-28 20:53:42 +00002485static SDValue performIntMed3ImmCombine(SelectionDAG &DAG,
2486 SDLoc SL,
2487 SDValue Op0,
2488 SDValue Op1,
2489 bool Signed) {
2490 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
2491 if (!K1)
2492 return SDValue();
2493
2494 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
2495 if (!K0)
2496 return SDValue();
2497
2498
2499 if (Signed) {
2500 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
2501 return SDValue();
2502 } else {
2503 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
2504 return SDValue();
2505 }
2506
2507 EVT VT = K0->getValueType(0);
2508 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
2509 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
2510}
2511
2512static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
2513 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
2514 return true;
2515
2516 return DAG.isKnownNeverNaN(Op);
2517}
2518
2519static SDValue performFPMed3ImmCombine(SelectionDAG &DAG,
2520 SDLoc SL,
2521 SDValue Op0,
2522 SDValue Op1) {
2523 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
2524 if (!K1)
2525 return SDValue();
2526
2527 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
2528 if (!K0)
2529 return SDValue();
2530
2531 // Ordered >= (although NaN inputs should have folded away by now).
2532 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
2533 if (Cmp == APFloat::cmpGreaterThan)
2534 return SDValue();
2535
2536 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
2537 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
2538 // give the other result, which is different from med3 with a NaN input.
2539 SDValue Var = Op0.getOperand(0);
2540 if (!isKnownNeverSNan(DAG, Var))
2541 return SDValue();
2542
2543 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
2544 Var, SDValue(K0, 0), SDValue(K1, 0));
2545}
2546
2547SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
2548 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002549 SelectionDAG &DAG = DCI.DAG;
2550
2551 unsigned Opc = N->getOpcode();
2552 SDValue Op0 = N->getOperand(0);
2553 SDValue Op1 = N->getOperand(1);
2554
2555 // Only do this if the inner op has one use since this will just increases
2556 // register pressure for no benefit.
2557
Matt Arsenault5b39b342016-01-28 20:53:48 +00002558 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
2559 // max(max(a, b), c) -> max3(a, b, c)
2560 // min(min(a, b), c) -> min3(a, b, c)
2561 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
2562 SDLoc DL(N);
2563 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2564 DL,
2565 N->getValueType(0),
2566 Op0.getOperand(0),
2567 Op0.getOperand(1),
2568 Op1);
2569 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002570
Matt Arsenault5b39b342016-01-28 20:53:48 +00002571 // Try commuted.
2572 // max(a, max(b, c)) -> max3(a, b, c)
2573 // min(a, min(b, c)) -> min3(a, b, c)
2574 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
2575 SDLoc DL(N);
2576 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2577 DL,
2578 N->getValueType(0),
2579 Op0,
2580 Op1.getOperand(0),
2581 Op1.getOperand(1));
2582 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002583 }
2584
Matt Arsenaultf639c322016-01-28 20:53:42 +00002585 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
2586 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
2587 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
2588 return Med3;
2589 }
2590
2591 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
2592 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
2593 return Med3;
2594 }
2595
2596 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00002597 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
2598 (Opc == AMDGPUISD::FMIN_LEGACY &&
2599 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00002600 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
2601 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
2602 return Res;
2603 }
2604
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002605 return SDValue();
2606}
2607
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002608SDValue SITargetLowering::performSetCCCombine(SDNode *N,
2609 DAGCombinerInfo &DCI) const {
2610 SelectionDAG &DAG = DCI.DAG;
2611 SDLoc SL(N);
2612
2613 SDValue LHS = N->getOperand(0);
2614 SDValue RHS = N->getOperand(1);
2615 EVT VT = LHS.getValueType();
2616
2617 if (VT != MVT::f32 && VT != MVT::f64)
2618 return SDValue();
2619
2620 // Match isinf pattern
2621 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
2622 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
2623 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
2624 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2625 if (!CRHS)
2626 return SDValue();
2627
2628 const APFloat &APF = CRHS->getValueAPF();
2629 if (APF.isInfinity() && !APF.isNegative()) {
2630 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002631 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
2632 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002633 }
2634 }
2635
2636 return SDValue();
2637}
2638
Tom Stellard75aadc22012-12-11 21:25:42 +00002639SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
2640 DAGCombinerInfo &DCI) const {
2641 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002642 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00002643
2644 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00002645 default:
2646 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002647 case ISD::SETCC:
2648 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00002649 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002650 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002651 case ISD::SMAX:
2652 case ISD::SMIN:
2653 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00002654 case ISD::UMIN:
2655 case AMDGPUISD::FMIN_LEGACY:
2656 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002657 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00002658 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002659 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00002660 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002661 break;
2662 }
Matt Arsenault364a6742014-06-11 17:50:44 +00002663
2664 case AMDGPUISD::CVT_F32_UBYTE0:
2665 case AMDGPUISD::CVT_F32_UBYTE1:
2666 case AMDGPUISD::CVT_F32_UBYTE2:
2667 case AMDGPUISD::CVT_F32_UBYTE3: {
2668 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
2669
2670 SDValue Src = N->getOperand(0);
2671 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
2672
2673 APInt KnownZero, KnownOne;
2674 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2675 !DCI.isBeforeLegalizeOps());
2676 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2677 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
2678 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
2679 DCI.CommitTargetLoweringOpt(TLO);
2680 }
2681
2682 break;
2683 }
2684
2685 case ISD::UINT_TO_FP: {
2686 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002687 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002688 case ISD::FADD: {
2689 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2690 break;
2691
2692 EVT VT = N->getValueType(0);
2693 if (VT != MVT::f32)
2694 break;
2695
Matt Arsenault8d630032015-02-20 22:10:41 +00002696 // Only do this if we are not trying to support denormals. v_mad_f32 does
2697 // not support denormals ever.
2698 if (Subtarget->hasFP32Denormals())
2699 break;
2700
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002701 SDValue LHS = N->getOperand(0);
2702 SDValue RHS = N->getOperand(1);
2703
2704 // These should really be instruction patterns, but writing patterns with
2705 // source modiifiers is a pain.
2706
2707 // fadd (fadd (a, a), b) -> mad 2.0, a, b
2708 if (LHS.getOpcode() == ISD::FADD) {
2709 SDValue A = LHS.getOperand(0);
2710 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002711 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002712 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002713 }
2714 }
2715
2716 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
2717 if (RHS.getOpcode() == ISD::FADD) {
2718 SDValue A = RHS.getOperand(0);
2719 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002720 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002721 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002722 }
2723 }
2724
Matt Arsenault8d630032015-02-20 22:10:41 +00002725 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002726 }
Matt Arsenault8675db12014-08-29 16:01:14 +00002727 case ISD::FSUB: {
2728 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2729 break;
2730
2731 EVT VT = N->getValueType(0);
2732
2733 // Try to get the fneg to fold into the source modifier. This undoes generic
2734 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00002735 //
2736 // Only do this if we are not trying to support denormals. v_mad_f32 does
2737 // not support denormals ever.
2738 if (VT == MVT::f32 &&
2739 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00002740 SDValue LHS = N->getOperand(0);
2741 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002742 if (LHS.getOpcode() == ISD::FADD) {
2743 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
2744
2745 SDValue A = LHS.getOperand(0);
2746 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002747 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002748 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
2749
Matt Arsenault8d630032015-02-20 22:10:41 +00002750 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002751 }
2752 }
2753
2754 if (RHS.getOpcode() == ISD::FADD) {
2755 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
2756
2757 SDValue A = RHS.getOperand(0);
2758 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002759 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002760 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002761 }
2762 }
Matt Arsenault8d630032015-02-20 22:10:41 +00002763
2764 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00002765 }
2766
2767 break;
2768 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002769 case ISD::LOAD:
2770 case ISD::STORE:
2771 case ISD::ATOMIC_LOAD:
2772 case ISD::ATOMIC_STORE:
2773 case ISD::ATOMIC_CMP_SWAP:
2774 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2775 case ISD::ATOMIC_SWAP:
2776 case ISD::ATOMIC_LOAD_ADD:
2777 case ISD::ATOMIC_LOAD_SUB:
2778 case ISD::ATOMIC_LOAD_AND:
2779 case ISD::ATOMIC_LOAD_OR:
2780 case ISD::ATOMIC_LOAD_XOR:
2781 case ISD::ATOMIC_LOAD_NAND:
2782 case ISD::ATOMIC_LOAD_MIN:
2783 case ISD::ATOMIC_LOAD_MAX:
2784 case ISD::ATOMIC_LOAD_UMIN:
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00002785 case ISD::ATOMIC_LOAD_UMAX:
2786 case AMDGPUISD::ATOMIC_INC:
2787 case AMDGPUISD::ATOMIC_DEC: { // TODO: Target mem intrinsics.
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002788 if (DCI.isBeforeLegalize())
2789 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002790
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002791 MemSDNode *MemNode = cast<MemSDNode>(N);
2792 SDValue Ptr = MemNode->getBasePtr();
2793
2794 // TODO: We could also do this for multiplies.
2795 unsigned AS = MemNode->getAddressSpace();
2796 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
2797 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
2798 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002799 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002800
2801 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
2802 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
2803 }
2804 }
2805 break;
2806 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002807 case ISD::AND:
2808 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002809 case ISD::OR:
2810 return performOrCombine(N, DCI);
2811 case AMDGPUISD::FP_CLASS:
2812 return performClassCombine(N, DCI);
Matt Arsenault9cd90712016-04-14 01:42:16 +00002813 case ISD::FCANONICALIZE:
2814 return performFCanonicalizeCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002815 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002816 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00002817}
Christian Konigd910b7d2013-02-26 17:52:16 +00002818
Christian Konigf82901a2013-02-26 17:52:23 +00002819/// \brief Analyze the possible immediate value Op
2820///
2821/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
2822/// and the immediate value if it's a literal immediate
2823int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
2824
Eric Christopher7792e322015-01-30 23:24:40 +00002825 const SIInstrInfo *TII =
2826 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00002827
Tom Stellardedbf1eb2013-04-05 23:31:20 +00002828 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00002829 if (TII->isInlineConstant(Node->getAPIntValue()))
2830 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00002831
Matt Arsenault11a4d672015-02-13 19:05:03 +00002832 uint64_t Val = Node->getZExtValue();
2833 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00002834 }
2835
2836 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
2837 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
2838 return 0;
2839
2840 if (Node->getValueType(0) == MVT::f32)
2841 return FloatToBits(Node->getValueAPF().convertToFloat());
2842
2843 return -1;
2844 }
2845
2846 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00002847}
2848
Christian Konig8e06e2a2013-04-10 08:39:08 +00002849/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00002850static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00002851 switch (Idx) {
2852 default: return 0;
2853 case AMDGPU::sub0: return 0;
2854 case AMDGPU::sub1: return 1;
2855 case AMDGPU::sub2: return 2;
2856 case AMDGPU::sub3: return 3;
2857 }
2858}
2859
2860/// \brief Adjust the writemask of MIMG instructions
2861void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
2862 SelectionDAG &DAG) const {
2863 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00002864 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002865 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
2866 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00002867 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002868
2869 // Try to figure out the used register components
2870 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
2871 I != E; ++I) {
2872
2873 // Abort if we can't understand the usage
2874 if (!I->isMachineOpcode() ||
2875 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
2876 return;
2877
Tom Stellard54774e52013-10-23 02:53:47 +00002878 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
2879 // Note that subregs are packed, i.e. Lane==0 is the first bit set
2880 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
2881 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00002882 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00002883
Tom Stellard54774e52013-10-23 02:53:47 +00002884 // Set which texture component corresponds to the lane.
2885 unsigned Comp;
2886 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
2887 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00002888 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00002889 Dmask &= ~(1 << Comp);
2890 }
2891
Christian Konig8e06e2a2013-04-10 08:39:08 +00002892 // Abort if we have more than one user per component
2893 if (Users[Lane])
2894 return;
2895
2896 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00002897 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002898 }
2899
Tom Stellard54774e52013-10-23 02:53:47 +00002900 // Abort if there's no change
2901 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00002902 return;
2903
2904 // Adjust the writemask in the node
2905 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002906 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002907 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002908 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00002909 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002910
Christian Konig8b1ed282013-04-10 08:39:16 +00002911 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00002912 // (if NewDmask has only one bit set...)
2913 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002914 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
2915 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00002916 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002917 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00002918 SDValue(Node, 0), RC);
2919 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
2920 return;
2921 }
2922
Christian Konig8e06e2a2013-04-10 08:39:08 +00002923 // Update the users of the node with the new indices
2924 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
2925
2926 SDNode *User = Users[i];
2927 if (!User)
2928 continue;
2929
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002930 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002931 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
2932
2933 switch (Idx) {
2934 default: break;
2935 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
2936 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
2937 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
2938 }
2939 }
2940}
2941
Tom Stellardc98ee202015-07-16 19:40:07 +00002942static bool isFrameIndexOp(SDValue Op) {
2943 if (Op.getOpcode() == ISD::AssertZext)
2944 Op = Op.getOperand(0);
2945
2946 return isa<FrameIndexSDNode>(Op);
2947}
2948
Tom Stellard3457a842014-10-09 19:06:00 +00002949/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
2950/// with frame index operands.
2951/// LLVM assumes that inputs are to these instructions are registers.
2952void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
2953 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002954
2955 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00002956 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00002957 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00002958 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002959 continue;
2960 }
2961
Tom Stellard3457a842014-10-09 19:06:00 +00002962 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002963 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00002964 Node->getOperand(i).getValueType(),
2965 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002966 }
2967
Tom Stellard3457a842014-10-09 19:06:00 +00002968 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002969}
2970
Matt Arsenault08d84942014-06-03 23:06:13 +00002971/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00002972SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
2973 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002974 const SIInstrInfo *TII =
2975 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00002976 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00002977
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00002978 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore())
Christian Konig8e06e2a2013-04-10 08:39:08 +00002979 adjustWritemask(Node, DAG);
2980
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00002981 if (Opcode == AMDGPU::INSERT_SUBREG ||
2982 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002983 legalizeTargetIndependentNode(Node, DAG);
2984 return Node;
2985 }
Tom Stellard654d6692015-01-08 15:08:17 +00002986 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002987}
Christian Konig8b1ed282013-04-10 08:39:16 +00002988
2989/// \brief Assign the register class depending on the number of
2990/// bits set in the writemask
2991void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
2992 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002993 const SIInstrInfo *TII =
2994 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002995
Tom Stellarda99ada52014-11-21 22:31:44 +00002996 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00002997
2998 if (TII->isVOP3(MI->getOpcode())) {
2999 // Make sure constant bus requirements are respected.
3000 TII->legalizeOperandsVOP3(MRI, MI);
3001 return;
3002 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00003003
Matt Arsenault3add6432015-10-20 04:35:43 +00003004 if (TII->isMIMG(*MI)) {
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003005 unsigned VReg = MI->getOperand(0).getReg();
Nikolay Haustov2f684f12016-02-26 09:51:05 +00003006 unsigned DmaskIdx = MI->getNumOperands() == 12 ? 3 : 4;
3007 unsigned Writemask = MI->getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003008 unsigned BitsSet = 0;
3009 for (unsigned i = 0; i < 4; ++i)
3010 BitsSet += Writemask & (1 << i) ? 1 : 0;
3011
3012 const TargetRegisterClass *RC;
3013 switch (BitsSet) {
3014 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00003015 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003016 case 2: RC = &AMDGPU::VReg_64RegClass; break;
3017 case 3: RC = &AMDGPU::VReg_96RegClass; break;
3018 }
3019
3020 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
3021 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003022 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00003023 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00003024 }
3025
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003026 // Replace unused atomics with the no return version.
3027 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
3028 if (NoRetAtomicOp != -1) {
3029 if (!Node->hasAnyUseOfValue(0)) {
3030 MI->setDesc(TII->get(NoRetAtomicOp));
3031 MI->RemoveOperand(0);
Tom Stellard354a43c2016-04-01 18:27:37 +00003032 return;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003033 }
3034
Tom Stellard354a43c2016-04-01 18:27:37 +00003035 // For mubuf_atomic_cmpswap, we need to have tablegen use an extract_subreg
3036 // instruction, because the return type of these instructions is a vec2 of
3037 // the memory type, so it can be tied to the input operand.
3038 // This means these instructions always have a use, so we need to add a
3039 // special case to check if the atomic has only one extract_subreg use,
3040 // which itself has no uses.
3041 if ((Node->hasNUsesOfValue(1, 0) &&
3042 Node->use_begin()->getMachineOpcode() == AMDGPU::EXTRACT_SUBREG &&
3043 !Node->use_begin()->hasAnyUseOfValue(0))) {
3044 unsigned Def = MI->getOperand(0).getReg();
3045
3046 // Change this into a noret atomic.
3047 MI->setDesc(TII->get(NoRetAtomicOp));
3048 MI->RemoveOperand(0);
3049
3050 // If we only remove the def operand from the atomic instruction, the
3051 // extract_subreg will be left with a use of a vreg without a def.
3052 // So we need to insert an implicit_def to avoid machine verifier
3053 // errors.
3054 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(),
3055 TII->get(AMDGPU::IMPLICIT_DEF), Def);
3056 }
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00003057 return;
3058 }
Christian Konig8b1ed282013-04-10 08:39:16 +00003059}
Tom Stellard0518ff82013-06-03 17:39:58 +00003060
Matt Arsenault485defe2014-11-05 19:01:17 +00003061static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003062 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00003063 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
3064}
3065
3066MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
3067 SDLoc DL,
3068 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00003069 const SIInstrInfo *TII =
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003070 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00003071
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003072 // Build the half of the subregister with the constants before building the
3073 // full 128-bit register. If we are building multiple resource descriptors,
3074 // this will allow CSEing of the 2-component register.
3075 const SDValue Ops0[] = {
3076 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
3077 buildSMovImm32(DAG, DL, 0),
3078 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
3079 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
3080 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
3081 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003082
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003083 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
3084 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00003085
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003086 // Combine the constants and the pointer.
3087 const SDValue Ops1[] = {
3088 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
3089 Ptr,
3090 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
3091 SubRegHi,
3092 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
3093 };
Matt Arsenault485defe2014-11-05 19:01:17 +00003094
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00003095 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00003096}
3097
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003098/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00003099/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
3100/// of the resource descriptor) to create an offset, which is added to
3101/// the resource pointer.
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003102MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
3103 SDLoc DL,
3104 SDValue Ptr,
3105 uint32_t RsrcDword1,
3106 uint64_t RsrcDword2And3) const {
3107 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
3108 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
3109 if (RsrcDword1) {
3110 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003111 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
3112 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003113 }
3114
3115 SDValue DataLo = buildSMovImm32(DAG, DL,
3116 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
3117 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
3118
3119 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003120 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003121 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003122 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003123 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003124 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003125 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003126 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003127 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003128 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00003129 };
3130
3131 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
3132}
3133
Tom Stellard94593ee2013-06-03 17:40:18 +00003134SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3135 const TargetRegisterClass *RC,
3136 unsigned Reg, EVT VT) const {
3137 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
3138
3139 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
3140 cast<RegisterSDNode>(VReg)->getReg(), VT);
3141}
Tom Stellardd7e6f132015-04-08 01:09:26 +00003142
3143//===----------------------------------------------------------------------===//
3144// SI Inline Assembly Support
3145//===----------------------------------------------------------------------===//
3146
3147std::pair<unsigned, const TargetRegisterClass *>
3148SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00003149 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00003150 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003151
3152 if (Constraint.size() == 1) {
3153 switch (Constraint[0]) {
3154 case 's':
3155 case 'r':
3156 switch (VT.getSizeInBits()) {
3157 default:
3158 return std::make_pair(0U, nullptr);
3159 case 32:
Tom Stellardd7e6f132015-04-08 01:09:26 +00003160 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003161 case 64:
3162 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
3163 case 128:
3164 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
3165 case 256:
3166 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
3167 }
3168
3169 case 'v':
3170 switch (VT.getSizeInBits()) {
3171 default:
3172 return std::make_pair(0U, nullptr);
3173 case 32:
3174 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
3175 case 64:
3176 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
3177 case 96:
3178 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
3179 case 128:
3180 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
3181 case 256:
3182 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
3183 case 512:
3184 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
3185 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00003186 }
3187 }
3188
3189 if (Constraint.size() > 1) {
3190 const TargetRegisterClass *RC = nullptr;
3191 if (Constraint[1] == 'v') {
3192 RC = &AMDGPU::VGPR_32RegClass;
3193 } else if (Constraint[1] == 's') {
3194 RC = &AMDGPU::SGPR_32RegClass;
3195 }
3196
3197 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00003198 uint32_t Idx;
3199 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
3200 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00003201 return std::make_pair(RC->getRegister(Idx), RC);
3202 }
3203 }
3204 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
3205}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00003206
3207SITargetLowering::ConstraintType
3208SITargetLowering::getConstraintType(StringRef Constraint) const {
3209 if (Constraint.size() == 1) {
3210 switch (Constraint[0]) {
3211 default: break;
3212 case 's':
3213 case 'v':
3214 return C_RegisterClass;
3215 }
3216 }
3217 return TargetLowering::getConstraintType(Constraint);
3218}