blob: 9c1ce70dec3e276e69b8ddd0e41d46adcc870a46 [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 Arsenaultd48da142015-11-02 23:23:02 +000023#include "AMDGPUDiagnosticInfoUnsupported.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000024#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000025#include "AMDGPUSubtarget.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000026#include "SIInstrInfo.h"
27#include "SIMachineFunctionInfo.h"
28#include "SIRegisterInfo.h"
Alexey Samsonova253bf92014-08-27 19:36:53 +000029#include "llvm/ADT/BitVector.h"
Matt Arsenault9a10cea2016-01-26 04:29:24 +000030#include "llvm/ADT/StringSwitch.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000031#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000032#include "llvm/CodeGen/MachineInstrBuilder.h"
33#include "llvm/CodeGen/MachineRegisterInfo.h"
34#include "llvm/CodeGen/SelectionDAG.h"
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
Eric Christopher7792e322015-01-30 23:24:40 +000040SITargetLowering::SITargetLowering(TargetMachine &TM,
41 const AMDGPUSubtarget &STI)
42 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000043 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000044 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000045
Christian Konig2214f142013-03-07 09:03:38 +000046 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
47 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
48
Tom Stellard334b29c2014-04-17 21:00:09 +000049 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000050 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000051
Tom Stellard436780b2014-05-15 14:41:57 +000052 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
53 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
54 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000055
Matt Arsenault61001bb2015-11-25 19:58:34 +000056 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
57 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
58
Tom Stellard436780b2014-05-15 14:41:57 +000059 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
60 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000061
Tom Stellardf0a21072014-11-18 20:39:39 +000062 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000063 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
64
Tom Stellardf0a21072014-11-18 20:39:39 +000065 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000066 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000067
Eric Christopher23a3a7c2015-02-26 00:00:24 +000068 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000069
Christian Konig2989ffc2013-03-18 11:34:16 +000070 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
71 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
72 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
73 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
74
Tom Stellard75aadc22012-12-11 21:25:42 +000075 setOperationAction(ISD::ADD, MVT::i32, Legal);
Matt Arsenaulte8d21462013-11-18 20:09:40 +000076 setOperationAction(ISD::ADDC, MVT::i32, Legal);
77 setOperationAction(ISD::ADDE, MVT::i32, Legal);
Matt Arsenaultb8b51532014-06-23 18:00:38 +000078 setOperationAction(ISD::SUBC, MVT::i32, Legal);
79 setOperationAction(ISD::SUBE, MVT::i32, Legal);
Aaron Watrydaabb202013-06-25 13:55:52 +000080
Matt Arsenaultad14ce82014-07-19 18:44:39 +000081 setOperationAction(ISD::FSIN, MVT::f32, Custom);
82 setOperationAction(ISD::FCOS, MVT::f32, Custom);
83
Matt Arsenault7c936902014-10-21 23:01:01 +000084 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
85 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
86
Tom Stellard35bb18c2013-08-26 15:06:04 +000087 // We need to custom lower vector stores from local memory
Tom Stellard35bb18c2013-08-26 15:06:04 +000088 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000089 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
90 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
91
92 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
93 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000094
Tom Stellard1c8788e2014-03-07 20:12:33 +000095 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000096 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
97
Tom Stellard0ec134f2014-02-04 17:18:40 +000098 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +000099 setOperationAction(ISD::SELECT, MVT::f64, Promote);
100 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000101
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000102 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
103 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
104 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
105 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000106
Tom Stellardd1efda82016-01-20 21:48:24 +0000107 setOperationAction(ISD::SETCC, MVT::i1, Promote);
Tom Stellard83747202013-07-18 21:43:53 +0000108 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
109 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
110
Matt Arsenaulte306a322014-10-21 16:25:08 +0000111 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
Matt Arsenaultd0792852015-12-14 17:25:38 +0000112 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000113
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000114 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000115 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
116 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
117
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
121
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
125
Matt Arsenault94812212014-11-14 18:18:16 +0000126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000127 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
128
Tom Stellard94593ee2013-06-03 17:40:18 +0000129 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000130 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
131 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
132 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000133
Tom Stellardafcf12f2013-09-12 02:55:14 +0000134 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000135 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000136
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000137 for (MVT VT : MVT::integer_valuetypes()) {
Matt Arsenaultbd223422015-01-14 01:35:17 +0000138 if (VT == MVT::i64)
139 continue;
140
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000141 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000142 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
143 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000144 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000145
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000146 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000147 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
148 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000149 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000150
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000151 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000152 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
153 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000154 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
155 }
156
157 for (MVT VT : MVT::integer_vector_valuetypes()) {
158 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
159 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
160 }
161
162 for (MVT VT : MVT::fp_valuetypes())
163 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000164
Matt Arsenault61001bb2015-11-25 19:58:34 +0000165 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
166 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
167
Matt Arsenault6f243792013-09-05 19:41:10 +0000168 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000169 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
Matt Arsenaulte1ce3442015-07-31 04:12:04 +0000170 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000171 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000172
Matt Arsenault61001bb2015-11-25 19:58:34 +0000173
174 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
175
176 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
177 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
178
Matt Arsenault470acd82014-04-15 22:28:39 +0000179 setOperationAction(ISD::LOAD, MVT::i1, Custom);
180
Matt Arsenault61001bb2015-11-25 19:58:34 +0000181 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
182 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
183
184 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
185 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
186
187 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
188
Tom Stellardfd155822013-08-26 15:05:36 +0000189 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000190 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000191 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000192
Tom Stellard5f337882014-04-29 23:12:43 +0000193 // These should use UDIVREM, so set them to expand
194 setOperationAction(ISD::UDIV, MVT::i64, Expand);
195 setOperationAction(ISD::UREM, MVT::i64, Expand);
196
Matt Arsenault0d89e842014-07-15 21:44:37 +0000197 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
198 setOperationAction(ISD::SELECT, MVT::i1, Promote);
199
Matt Arsenault61001bb2015-11-25 19:58:34 +0000200 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
201
202
203 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
204
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000205 // We only support LOAD/STORE and vector manipulation ops for vectors
206 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000207 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000208 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
209 switch(Op) {
210 case ISD::LOAD:
211 case ISD::STORE:
212 case ISD::BUILD_VECTOR:
213 case ISD::BITCAST:
214 case ISD::EXTRACT_VECTOR_ELT:
215 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000216 case ISD::INSERT_SUBVECTOR:
217 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000218 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000219 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000220 case ISD::CONCAT_VECTORS:
221 setOperationAction(Op, VT, Custom);
222 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000223 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000224 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000225 break;
226 }
227 }
228 }
229
Matt Arsenault61001bb2015-11-25 19:58:34 +0000230 // Most operations are naturally 32-bit vector operations. We only support
231 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
232 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
233 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
234 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
235
236 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
237 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
238
239 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
240 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
241
242 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
243 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
244 }
245
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000246 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
247 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
248 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000249 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000250 }
251
Marek Olsak7d777282015-03-24 13:40:15 +0000252 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000253 setOperationAction(ISD::FDIV, MVT::f32, Custom);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +0000254 setOperationAction(ISD::FDIV, MVT::f64, Custom);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000255
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000256 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000257 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000258 setTargetDAGCombine(ISD::FMINNUM);
259 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000260 setTargetDAGCombine(ISD::SMIN);
261 setTargetDAGCombine(ISD::SMAX);
262 setTargetDAGCombine(ISD::UMIN);
263 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000264 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000265 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000266 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000267 setTargetDAGCombine(ISD::UINT_TO_FP);
268
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000269 // All memory operations. Some folding on the pointer operand is done to help
270 // matching the constant offsets in the addressing modes.
271 setTargetDAGCombine(ISD::LOAD);
272 setTargetDAGCombine(ISD::STORE);
273 setTargetDAGCombine(ISD::ATOMIC_LOAD);
274 setTargetDAGCombine(ISD::ATOMIC_STORE);
275 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
276 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
277 setTargetDAGCombine(ISD::ATOMIC_SWAP);
278 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
279 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
280 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
281 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
282 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
283 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
284 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
285 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
286 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
287 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
288
Christian Konigeecebd02013-03-26 14:04:02 +0000289 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000290}
291
Tom Stellard0125f2a2013-06-25 02:39:35 +0000292//===----------------------------------------------------------------------===//
293// TargetLowering queries
294//===----------------------------------------------------------------------===//
295
Matt Arsenaulte306a322014-10-21 16:25:08 +0000296bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
297 EVT) const {
298 // SI has some legal vector types, but no legal vector operations. Say no
299 // shuffles are legal in order to prefer scalarizing some vector operations.
300 return false;
301}
302
Tom Stellard70580f82015-07-20 14:28:41 +0000303bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
304 // Flat instructions do not have offsets, and only have the register
305 // address.
306 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
307}
308
Matt Arsenault711b3902015-08-07 20:18:34 +0000309bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
310 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
311 // additionally can do r + r + i with addr64. 32-bit has more addressing
312 // mode options. Depending on the resource constant, it can also do
313 // (i64 r0) + (i32 r1) * (i14 i).
314 //
315 // Private arrays end up using a scratch buffer most of the time, so also
316 // assume those use MUBUF instructions. Scratch loads / stores are currently
317 // implemented as mubuf instructions with offen bit set, so slightly
318 // different than the normal addr64.
319 if (!isUInt<12>(AM.BaseOffs))
320 return false;
321
322 // FIXME: Since we can split immediate into soffset and immediate offset,
323 // would it make sense to allow any immediate?
324
325 switch (AM.Scale) {
326 case 0: // r + i or just i, depending on HasBaseReg.
327 return true;
328 case 1:
329 return true; // We have r + r or r + i.
330 case 2:
331 if (AM.HasBaseReg) {
332 // Reject 2 * r + r.
333 return false;
334 }
335
336 // Allow 2 * r as r + r
337 // Or 2 * r + i is allowed as r + r + i.
338 return true;
339 default: // Don't allow n * r
340 return false;
341 }
342}
343
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000344bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
345 const AddrMode &AM, Type *Ty,
346 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000347 // No global is ever allowed as a base.
348 if (AM.BaseGV)
349 return false;
350
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000351 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000352 case AMDGPUAS::GLOBAL_ADDRESS: {
Tom Stellard70580f82015-07-20 14:28:41 +0000353 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
354 // Assume the we will use FLAT for all global memory accesses
355 // on VI.
356 // FIXME: This assumption is currently wrong. On VI we still use
357 // MUBUF instructions for the r + i addressing mode. As currently
358 // implemented, the MUBUF instructions only work on buffer < 4GB.
359 // It may be possible to support > 4GB buffers with MUBUF instructions,
360 // by setting the stride value in the resource descriptor which would
361 // increase the size limit to (stride * 4GB). However, this is risky,
362 // because it has never been validated.
363 return isLegalFlatAddressingMode(AM);
364 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000365
Matt Arsenault711b3902015-08-07 20:18:34 +0000366 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000367 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000368 case AMDGPUAS::CONSTANT_ADDRESS: {
369 // If the offset isn't a multiple of 4, it probably isn't going to be
370 // correctly aligned.
371 if (AM.BaseOffs % 4 != 0)
372 return isLegalMUBUFAddressingMode(AM);
373
374 // There are no SMRD extloads, so if we have to do a small type access we
375 // will use a MUBUF load.
376 // FIXME?: We also need to do this if unaligned, but we don't know the
377 // alignment here.
378 if (DL.getTypeStoreSize(Ty) < 4)
379 return isLegalMUBUFAddressingMode(AM);
380
381 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
382 // SMRD instructions have an 8-bit, dword offset on SI.
383 if (!isUInt<8>(AM.BaseOffs / 4))
384 return false;
385 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) {
386 // On CI+, this can also be a 32-bit literal constant offset. If it fits
387 // in 8-bits, it can use a smaller encoding.
388 if (!isUInt<32>(AM.BaseOffs / 4))
389 return false;
390 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) {
391 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
392 if (!isUInt<20>(AM.BaseOffs))
393 return false;
394 } else
395 llvm_unreachable("unhandled generation");
396
397 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
398 return true;
399
400 if (AM.Scale == 1 && AM.HasBaseReg)
401 return true;
402
403 return false;
404 }
405
406 case AMDGPUAS::PRIVATE_ADDRESS:
407 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
408 return isLegalMUBUFAddressingMode(AM);
409
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000410 case AMDGPUAS::LOCAL_ADDRESS:
411 case AMDGPUAS::REGION_ADDRESS: {
412 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
413 // field.
414 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
415 // an 8-bit dword offset but we don't know the alignment here.
416 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000417 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000418
419 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
420 return true;
421
422 if (AM.Scale == 1 && AM.HasBaseReg)
423 return true;
424
Matt Arsenault5015a892014-08-15 17:17:07 +0000425 return false;
426 }
Tom Stellard70580f82015-07-20 14:28:41 +0000427 case AMDGPUAS::FLAT_ADDRESS:
428 return isLegalFlatAddressingMode(AM);
429
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000430 default:
431 llvm_unreachable("unhandled address space");
432 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000433}
434
Matt Arsenaulte6986632015-01-14 01:35:22 +0000435bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000436 unsigned AddrSpace,
437 unsigned Align,
438 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000439 if (IsFast)
440 *IsFast = false;
441
Matt Arsenault1018c892014-04-24 17:08:26 +0000442 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
443 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000444 if (!VT.isSimple() || VT == MVT::Other)
445 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000446
Tom Stellardc6b299c2015-02-02 18:02:28 +0000447 // TODO - CI+ supports unaligned memory accesses, but this requires driver
448 // support.
Matt Arsenault1018c892014-04-24 17:08:26 +0000449
Matt Arsenault1018c892014-04-24 17:08:26 +0000450 // XXX - The only mention I see of this in the ISA manual is for LDS direct
451 // reads the "byte address and must be dword aligned". Is it also true for the
452 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000453 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
454 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
455 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
456 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000457 bool AlignedBy4 = (Align % 4 == 0);
458 if (IsFast)
459 *IsFast = AlignedBy4;
460 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000461 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000462
Tom Stellard33e64c62015-02-04 20:49:52 +0000463 // Smaller than dword value must be aligned.
464 // FIXME: This should be allowed on CI+
465 if (VT.bitsLT(MVT::i32))
466 return false;
467
Matt Arsenault1018c892014-04-24 17:08:26 +0000468 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
469 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000470 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000471 if (IsFast)
472 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000473
474 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000475}
476
Matt Arsenault46645fa2014-07-28 17:49:26 +0000477EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
478 unsigned SrcAlign, bool IsMemset,
479 bool ZeroMemset,
480 bool MemcpyStrSrc,
481 MachineFunction &MF) const {
482 // FIXME: Should account for address space here.
483
484 // The default fallback uses the private pointer size as a guess for a type to
485 // use. Make sure we switch these to 64-bit accesses.
486
487 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
488 return MVT::v4i32;
489
490 if (Size >= 8 && DstAlign >= 4)
491 return MVT::v2i32;
492
493 // Use the default.
494 return MVT::Other;
495}
496
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000497static bool isFlatGlobalAddrSpace(unsigned AS) {
498 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
499 AS == AMDGPUAS::FLAT_ADDRESS ||
500 AS == AMDGPUAS::CONSTANT_ADDRESS;
501}
502
503bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
504 unsigned DestAS) const {
505 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
506}
507
Tom Stellarda6f24c62015-12-15 20:55:55 +0000508
509bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
510 const MemSDNode *MemNode = cast<MemSDNode>(N);
511 const Value *Ptr = MemNode->getMemOperand()->getValue();
512
513 // UndefValue means this is a load of a kernel input. These are uniform.
514 // Sometimes LDS instructions have constant pointers
515 if (isa<UndefValue>(Ptr) || isa<Argument>(Ptr) || isa<Constant>(Ptr) ||
516 isa<GlobalValue>(Ptr))
517 return true;
518
519 const Instruction *I = dyn_cast_or_null<Instruction>(Ptr);
520 return I && I->getMetadata("amdgpu.uniform");
521}
522
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000523TargetLoweringBase::LegalizeTypeAction
524SITargetLowering::getPreferredVectorAction(EVT VT) const {
525 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
526 return TypeSplitVector;
527
528 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000529}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000530
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000531bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
532 Type *Ty) const {
Eric Christopher7792e322015-01-30 23:24:40 +0000533 const SIInstrInfo *TII =
534 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000535 return TII->isInlineConstant(Imm);
536}
537
Tom Stellard2e045bb2016-01-20 00:13:22 +0000538bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
539
540 // SimplifySetCC uses this function to determine whether or not it should
541 // create setcc with i1 operands. We don't have instructions for i1 setcc.
542 if (VT == MVT::i1 && Op == ISD::SETCC)
543 return false;
544
545 return TargetLowering::isTypeDesirableForOp(Op, VT);
546}
547
Tom Stellardaf775432013-10-23 00:44:32 +0000548SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000549 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000550 unsigned Offset, bool Signed) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000551 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000552 MachineFunction &MF = DAG.getMachineFunction();
553 const SIRegisterInfo *TRI =
554 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
Matt Arsenaultac234b62015-11-30 21:15:57 +0000555 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000556
Matt Arsenault86033ca2014-07-28 17:31:39 +0000557 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
558
559 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000560 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000561 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000562 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
563 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
564 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
565 DAG.getConstant(Offset, SL, PtrVT));
Mehdi Amini44ede332015-07-09 02:09:04 +0000566 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000567 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
568
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000569 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000570
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000571 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000572 if (MemVT.isFloatingPoint())
573 ExtTy = ISD::EXTLOAD;
574
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000575 return DAG.getLoad(ISD::UNINDEXED, ExtTy,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000576 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
577 false, // isVolatile
578 true, // isNonTemporal
579 true, // isInvariant
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000580 Align); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000581}
582
Christian Konig2c8f6d52013-03-07 09:03:52 +0000583SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000584 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
585 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
586 SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardec2e43c2014-09-22 15:35:29 +0000587 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000588 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000589
590 MachineFunction &MF = DAG.getMachineFunction();
591 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000592 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000593 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000594
Matt Arsenaultd48da142015-11-02 23:23:02 +0000595 if (Subtarget->isAmdHsaOS() && Info->getShaderType() != ShaderType::COMPUTE) {
596 const Function *Fn = MF.getFunction();
597 DiagnosticInfoUnsupported NoGraphicsHSA(*Fn, "non-compute shaders with HSA");
598 DAG.getContext()->diagnose(NoGraphicsHSA);
599 return SDValue();
600 }
601
Tom Stellard0fbf8992015-10-06 21:16:34 +0000602 // FIXME: We currently assume all calling conventions are kernels.
Christian Konig2c8f6d52013-03-07 09:03:52 +0000603
604 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000605 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000606
607 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000608 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000609
610 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000611 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000612 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000613
Marek Olsakfccabaf2016-01-13 11:45:36 +0000614 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000615 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000616 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000617 ++PSInputNum;
618 continue;
619 }
620
Marek Olsakfccabaf2016-01-13 11:45:36 +0000621 Info->markPSInputAllocated(PSInputNum);
622 if (Arg.Used)
623 Info->PSInputEna |= 1 << PSInputNum;
624
625 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000626 }
627
628 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000629 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000630 ISD::InputArg NewArg = Arg;
631 NewArg.Flags.setSplit();
632 NewArg.VT = Arg.VT.getVectorElementType();
633
634 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
635 // three or five element vertex only needs three or five registers,
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000636 // NOT four or eight.
Andrew Trick05938a52015-02-16 18:10:47 +0000637 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000638 unsigned NumElements = ParamType->getVectorNumElements();
639
640 for (unsigned j = 0; j != NumElements; ++j) {
641 Splits.push_back(NewArg);
642 NewArg.PartOffset += NewArg.VT.getStoreSize();
643 }
644
Matt Arsenault762af962014-07-13 03:06:39 +0000645 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000646 Splits.push_back(Arg);
647 }
648 }
649
650 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000651 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
652 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000653
Christian Konig99ee0f42013-03-07 09:04:14 +0000654 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000655 //
656 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
657 // PSInputAddr, the user wants to enable some bits after the compilation
658 // based on run-time states. Since we can't know what the final PSInputEna
659 // will look like, so we shouldn't do anything here and the user should take
660 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000661 //
662 // Otherwise, the following restrictions apply:
663 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
664 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
665 // enabled too.
Matt Arsenault762af962014-07-13 03:06:39 +0000666 if (Info->getShaderType() == ShaderType::PIXEL &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000667 ((Info->getPSInputAddr() & 0x7F) == 0 ||
668 ((Info->getPSInputAddr() & 0xF) == 0 &&
669 Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000670 CCInfo.AllocateReg(AMDGPU::VGPR0);
671 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000672 Info->markPSInputAllocated(0);
673 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000674 }
675
Matt Arsenault762af962014-07-13 03:06:39 +0000676 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000677 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
678 Splits);
679 }
680
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000681 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
682 if (Info->hasPrivateSegmentBuffer()) {
683 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
684 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
685 CCInfo.AllocateReg(PrivateSegmentBufferReg);
686 }
687
688 if (Info->hasDispatchPtr()) {
689 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
690 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
691 CCInfo.AllocateReg(DispatchPtrReg);
692 }
693
694 if (Info->hasKernargSegmentPtr()) {
695 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
696 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
697 CCInfo.AllocateReg(InputPtrReg);
698 }
699
Christian Konig2c8f6d52013-03-07 09:03:52 +0000700 AnalyzeFormalArguments(CCInfo, Splits);
701
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000702 SmallVector<SDValue, 16> Chains;
703
Christian Konig2c8f6d52013-03-07 09:03:52 +0000704 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
705
Christian Konigb7be72d2013-05-17 09:46:48 +0000706 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000707 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000708 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000709 continue;
710 }
711
Christian Konig2c8f6d52013-03-07 09:03:52 +0000712 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000713 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000714
715 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000716 VT = Ins[i].VT;
717 EVT MemVT = Splits[i].VT;
Tom Stellardb5798b02015-06-26 21:15:03 +0000718 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
719 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000720 // The first 36 bytes of the input buffer contains information about
721 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000722 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000723 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000724 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000725
Craig Toppere3dcce92015-08-01 22:20:21 +0000726 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000727 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000728 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
729 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
730 // On SI local pointers are just offsets into LDS, so they are always
731 // less than 16-bits. On CI and newer they could potentially be
732 // real pointers, so we can't guarantee their size.
733 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
734 DAG.getValueType(MVT::i16));
735 }
736
Tom Stellarded882c22013-06-03 17:40:11 +0000737 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000738 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000739 continue;
740 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000741 assert(VA.isRegLoc() && "Parameter must be in a register!");
742
743 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000744
745 if (VT == MVT::i64) {
746 // For now assume it is a pointer
747 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
748 &AMDGPU::SReg_64RegClass);
749 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000750 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
751 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000752 continue;
753 }
754
755 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
756
757 Reg = MF.addLiveIn(Reg, RC);
758 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
759
Christian Konig2c8f6d52013-03-07 09:03:52 +0000760 if (Arg.VT.isVector()) {
761
762 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000763 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000764 unsigned NumElements = ParamType->getVectorNumElements();
765
766 SmallVector<SDValue, 4> Regs;
767 Regs.push_back(Val);
768 for (unsigned j = 1; j != NumElements; ++j) {
769 Reg = ArgLocs[ArgIdx++].getLocReg();
770 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000771
772 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
773 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000774 }
775
776 // Fill up the missing vector elements
777 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000778 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000779
Craig Topper48d114b2014-04-26 18:35:24 +0000780 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000781 continue;
782 }
783
784 InVals.push_back(Val);
785 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000786
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000787 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
788 // these from the dispatch pointer.
789
790 // Start adding system SGPRs.
791 if (Info->hasWorkGroupIDX()) {
792 unsigned Reg = Info->addWorkGroupIDX();
793 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
794 CCInfo.AllocateReg(Reg);
795 } else
796 llvm_unreachable("work group id x is always enabled");
797
798 if (Info->hasWorkGroupIDY()) {
799 unsigned Reg = Info->addWorkGroupIDY();
800 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
801 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000802 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000803
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000804 if (Info->hasWorkGroupIDZ()) {
805 unsigned Reg = Info->addWorkGroupIDZ();
806 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
807 CCInfo.AllocateReg(Reg);
808 }
809
810 if (Info->hasWorkGroupInfo()) {
811 unsigned Reg = Info->addWorkGroupInfo();
812 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
813 CCInfo.AllocateReg(Reg);
814 }
815
816 if (Info->hasPrivateSegmentWaveByteOffset()) {
817 // Scratch wave offset passed in system SGPR.
818 unsigned PrivateSegmentWaveByteOffsetReg
819 = Info->addPrivateSegmentWaveByteOffset();
820
821 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
822 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
823 }
824
825 // Now that we've figured out where the scratch register inputs are, see if
826 // should reserve the arguments and use them directly.
827
828 bool HasStackObjects = MF.getFrameInfo()->hasStackObjects();
829
830 if (ST.isAmdHsaOS()) {
831 // TODO: Assume we will spill without optimizations.
832 if (HasStackObjects) {
833 // If we have stack objects, we unquestionably need the private buffer
834 // resource. For the HSA ABI, this will be the first 4 user SGPR
835 // inputs. We can reserve those and use them directly.
836
837 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
838 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
839 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
840
841 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
842 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
843 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
844 } else {
845 unsigned ReservedBufferReg
846 = TRI->reservedPrivateSegmentBufferReg(MF);
847 unsigned ReservedOffsetReg
848 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
849
850 // We tentatively reserve the last registers (skipping the last two
851 // which may contain VCC). After register allocation, we'll replace
852 // these with the ones immediately after those which were really
853 // allocated. In the prologue copies will be inserted from the argument
854 // to these reserved registers.
855 Info->setScratchRSrcReg(ReservedBufferReg);
856 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
857 }
858 } else {
859 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
860
861 // Without HSA, relocations are used for the scratch pointer and the
862 // buffer resource setup is always inserted in the prologue. Scratch wave
863 // offset is still in an input SGPR.
864 Info->setScratchRSrcReg(ReservedBufferReg);
865
866 if (HasStackObjects) {
867 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
868 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
869 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
870 } else {
871 unsigned ReservedOffsetReg
872 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
873 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
874 }
875 }
876
877 if (Info->hasWorkItemIDX()) {
878 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
879 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
880 CCInfo.AllocateReg(Reg);
881 } else
882 llvm_unreachable("workitem id x should always be enabled");
883
884 if (Info->hasWorkItemIDY()) {
885 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
886 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
887 CCInfo.AllocateReg(Reg);
888 }
889
890 if (Info->hasWorkItemIDZ()) {
891 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
892 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
893 CCInfo.AllocateReg(Reg);
894 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000895
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000896 if (Chains.empty())
897 return Chain;
898
899 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000900}
901
Marek Olsak8a0f3352016-01-13 17:23:04 +0000902SDValue SITargetLowering::LowerReturn(SDValue Chain,
903 CallingConv::ID CallConv,
904 bool isVarArg,
905 const SmallVectorImpl<ISD::OutputArg> &Outs,
906 const SmallVectorImpl<SDValue> &OutVals,
907 SDLoc DL, SelectionDAG &DAG) const {
908 MachineFunction &MF = DAG.getMachineFunction();
909 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
910
911 if (Info->getShaderType() == ShaderType::COMPUTE)
912 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
913 OutVals, DL, DAG);
914
Marek Olsak8e9cc632016-01-13 17:23:09 +0000915 Info->setIfReturnsVoid(Outs.size() == 0);
916
Marek Olsak8a0f3352016-01-13 17:23:04 +0000917 SmallVector<ISD::OutputArg, 48> Splits;
918 SmallVector<SDValue, 48> SplitVals;
919
920 // Split vectors into their elements.
921 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
922 const ISD::OutputArg &Out = Outs[i];
923
924 if (Out.VT.isVector()) {
925 MVT VT = Out.VT.getVectorElementType();
926 ISD::OutputArg NewOut = Out;
927 NewOut.Flags.setSplit();
928 NewOut.VT = VT;
929
930 // We want the original number of vector elements here, e.g.
931 // three or five, not four or eight.
932 unsigned NumElements = Out.ArgVT.getVectorNumElements();
933
934 for (unsigned j = 0; j != NumElements; ++j) {
935 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
936 DAG.getConstant(j, DL, MVT::i32));
937 SplitVals.push_back(Elem);
938 Splits.push_back(NewOut);
939 NewOut.PartOffset += NewOut.VT.getStoreSize();
940 }
941 } else {
942 SplitVals.push_back(OutVals[i]);
943 Splits.push_back(Out);
944 }
945 }
946
947 // CCValAssign - represent the assignment of the return value to a location.
948 SmallVector<CCValAssign, 48> RVLocs;
949
950 // CCState - Info about the registers and stack slots.
951 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
952 *DAG.getContext());
953
954 // Analyze outgoing return values.
955 AnalyzeReturn(CCInfo, Splits);
956
957 SDValue Flag;
958 SmallVector<SDValue, 48> RetOps;
959 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
960
961 // Copy the result values into the output registers.
962 for (unsigned i = 0, realRVLocIdx = 0;
963 i != RVLocs.size();
964 ++i, ++realRVLocIdx) {
965 CCValAssign &VA = RVLocs[i];
966 assert(VA.isRegLoc() && "Can only return in registers!");
967
968 SDValue Arg = SplitVals[realRVLocIdx];
969
970 // Copied from other backends.
971 switch (VA.getLocInfo()) {
972 default: llvm_unreachable("Unknown loc info!");
973 case CCValAssign::Full:
974 break;
975 case CCValAssign::BCvt:
976 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
977 break;
978 }
979
980 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
981 Flag = Chain.getValue(1);
982 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
983 }
984
985 // Update chain and glue.
986 RetOps[0] = Chain;
987 if (Flag.getNode())
988 RetOps.push_back(Flag);
989
990 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, RetOps);
991}
992
Matt Arsenault9a10cea2016-01-26 04:29:24 +0000993unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
994 SelectionDAG &DAG) const {
995 unsigned Reg = StringSwitch<unsigned>(RegName)
996 .Case("m0", AMDGPU::M0)
997 .Case("exec", AMDGPU::EXEC)
998 .Case("exec_lo", AMDGPU::EXEC_LO)
999 .Case("exec_hi", AMDGPU::EXEC_HI)
1000 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1001 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1002 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1003 .Default(AMDGPU::NoRegister);
1004
1005 if (Reg == AMDGPU::NoRegister) {
1006 report_fatal_error(Twine("invalid register name \""
1007 + StringRef(RegName) + "\"."));
1008
1009 }
1010
1011 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
1012 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1013 report_fatal_error(Twine("invalid register \""
1014 + StringRef(RegName) + "\" for subtarget."));
1015 }
1016
1017 switch (Reg) {
1018 case AMDGPU::M0:
1019 case AMDGPU::EXEC_LO:
1020 case AMDGPU::EXEC_HI:
1021 case AMDGPU::FLAT_SCR_LO:
1022 case AMDGPU::FLAT_SCR_HI:
1023 if (VT.getSizeInBits() == 32)
1024 return Reg;
1025 break;
1026 case AMDGPU::EXEC:
1027 case AMDGPU::FLAT_SCR:
1028 if (VT.getSizeInBits() == 64)
1029 return Reg;
1030 break;
1031 default:
1032 llvm_unreachable("missing register type checking");
1033 }
1034
1035 report_fatal_error(Twine("invalid type for register \""
1036 + StringRef(RegName) + "\"."));
1037}
1038
Tom Stellard75aadc22012-12-11 21:25:42 +00001039MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
1040 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +00001041
Tom Stellard75aadc22012-12-11 21:25:42 +00001042 switch (MI->getOpcode()) {
1043 default:
1044 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Matt Arsenault20711b72015-02-20 22:10:45 +00001045 case AMDGPU::BRANCH:
1046 return BB;
Tom Stellard75aadc22012-12-11 21:25:42 +00001047 }
1048 return BB;
1049}
1050
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001051bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1052 // This currently forces unfolding various combinations of fsub into fma with
1053 // free fneg'd operands. As long as we have fast FMA (controlled by
1054 // isFMAFasterThanFMulAndFAdd), we should perform these.
1055
1056 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1057 // most of these combines appear to be cycle neutral but save on instruction
1058 // count / code size.
1059 return true;
1060}
1061
Mehdi Amini44ede332015-07-09 02:09:04 +00001062EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1063 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001064 if (!VT.isVector()) {
1065 return MVT::i1;
1066 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001067 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001068}
1069
Mehdi Aminieaabc512015-07-09 15:12:23 +00001070MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001071 return MVT::i32;
1072}
1073
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001074// Answering this is somewhat tricky and depends on the specific device which
1075// have different rates for fma or all f64 operations.
1076//
1077// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1078// regardless of which device (although the number of cycles differs between
1079// devices), so it is always profitable for f64.
1080//
1081// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1082// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1083// which we can always do even without fused FP ops since it returns the same
1084// result as the separate operations and since it is always full
1085// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1086// however does not support denormals, so we do report fma as faster if we have
1087// a fast fma device and require denormals.
1088//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001089bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1090 VT = VT.getScalarType();
1091
1092 if (!VT.isSimple())
1093 return false;
1094
1095 switch (VT.getSimpleVT().SimpleTy) {
1096 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001097 // This is as fast on some subtargets. However, we always have full rate f32
1098 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001099 // which we should prefer over fma. We can't use this if we want to support
1100 // denormals, so only report this in these cases.
1101 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001102 case MVT::f64:
1103 return true;
1104 default:
1105 break;
1106 }
1107
1108 return false;
1109}
1110
Tom Stellard75aadc22012-12-11 21:25:42 +00001111//===----------------------------------------------------------------------===//
1112// Custom DAG Lowering Operations
1113//===----------------------------------------------------------------------===//
1114
1115SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1116 switch (Op.getOpcode()) {
1117 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001118 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001119 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001120 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001121 SDValue Result = LowerLOAD(Op, DAG);
1122 assert((!Result.getNode() ||
1123 Result.getNode()->getNumValues() == 2) &&
1124 "Load should return a value and a chain");
1125 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001126 }
Tom Stellardaf775432013-10-23 00:44:32 +00001127
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001128 case ISD::FSIN:
1129 case ISD::FCOS:
1130 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001131 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001132 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001133 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001134 case ISD::GlobalAddress: {
1135 MachineFunction &MF = DAG.getMachineFunction();
1136 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1137 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001138 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001139 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
1140 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001141 }
1142 return SDValue();
1143}
1144
Tom Stellardf8794352012-12-19 22:10:31 +00001145/// \brief Helper function for LowerBRCOND
1146static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001147
Tom Stellardf8794352012-12-19 22:10:31 +00001148 SDNode *Parent = Value.getNode();
1149 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1150 I != E; ++I) {
1151
1152 if (I.getUse().get() != Value)
1153 continue;
1154
1155 if (I->getOpcode() == Opcode)
1156 return *I;
1157 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001158 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001159}
1160
Tom Stellardb02094e2014-07-21 15:45:01 +00001161SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
1162
Tom Stellardc98ee202015-07-16 19:40:07 +00001163 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +00001164 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
1165 unsigned FrameIndex = FINode->getIndex();
1166
Tom Stellardc98ee202015-07-16 19:40:07 +00001167 // A FrameIndex node represents a 32-bit offset into scratch memory. If
1168 // the high bit of a frame index offset were to be set, this would mean
1169 // that it represented an offset of ~2GB * 64 = ~128GB from the start of the
1170 // scratch buffer, with 64 being the number of threads per wave.
1171 //
1172 // If we know the machine uses less than 128GB of scratch, then we can
1173 // amrk the high bit of the FrameIndex node as known zero,
1174 // which is important, because it means in most situations we can
1175 // prove that values derived from FrameIndex nodes are non-negative.
1176 // This enables us to take advantage of more addressing modes when
1177 // accessing scratch buffers, since for scratch reads/writes, the register
1178 // offset must always be positive.
1179
1180 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
1181 if (Subtarget->enableHugeScratchBuffer())
1182 return TFI;
1183
1184 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
1185 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), 31)));
Tom Stellardb02094e2014-07-21 15:45:01 +00001186}
1187
Tom Stellardf8794352012-12-19 22:10:31 +00001188/// This transforms the control flow intrinsics to get the branch destination as
1189/// last parameter, also switches branch target with BR if the need arise
1190SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1191 SelectionDAG &DAG) const {
1192
Andrew Trickef9de2a2013-05-25 02:42:55 +00001193 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001194
1195 SDNode *Intr = BRCOND.getOperand(1).getNode();
1196 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001197 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001198
1199 if (Intr->getOpcode() == ISD::SETCC) {
1200 // As long as we negate the condition everything is fine
1201 SDNode *SetCC = Intr;
1202 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +00001203 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1204 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +00001205 Intr = SetCC->getOperand(0).getNode();
1206
1207 } else {
1208 // Get the target from BR if we don't negate the condition
1209 BR = findUser(BRCOND, ISD::BR);
1210 Target = BR->getOperand(1);
1211 }
1212
1213 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
1214
1215 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001216 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001217
1218 // operands of the new intrinsic call
1219 SmallVector<SDValue, 4> Ops;
1220 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001221 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001222 Ops.push_back(Target);
1223
1224 // build the new intrinsic call
1225 SDNode *Result = DAG.getNode(
1226 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001227 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001228
1229 if (BR) {
1230 // Give the branch instruction our target
1231 SDValue Ops[] = {
1232 BR->getOperand(0),
1233 BRCOND.getOperand(2)
1234 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001235 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1236 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1237 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001238 }
1239
1240 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1241
1242 // Copy the intrinsic results to registers
1243 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1244 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1245 if (!CopyToReg)
1246 continue;
1247
1248 Chain = DAG.getCopyToReg(
1249 Chain, DL,
1250 CopyToReg->getOperand(1),
1251 SDValue(Result, i - 1),
1252 SDValue());
1253
1254 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1255 }
1256
1257 // Remove the old intrinsic from the chain
1258 DAG.ReplaceAllUsesOfValueWith(
1259 SDValue(Intr, Intr->getNumValues() - 1),
1260 Intr->getOperand(0));
1261
1262 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001263}
1264
Tom Stellard067c8152014-07-21 14:01:14 +00001265SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1266 SDValue Op,
1267 SelectionDAG &DAG) const {
1268 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1269
1270 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
1271 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1272
1273 SDLoc DL(GSD);
1274 const GlobalValue *GV = GSD->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001275 MVT PtrVT = getPointerTy(DAG.getDataLayout(), GSD->getAddressSpace());
Tom Stellard067c8152014-07-21 14:01:14 +00001276
Tom Stellard067c8152014-07-21 14:01:14 +00001277 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
Tom Stellardc93fc112015-12-10 02:13:01 +00001278 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT, GA);
Tom Stellard067c8152014-07-21 14:01:14 +00001279}
1280
Tom Stellardfc92e772015-05-12 14:18:14 +00001281SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL,
1282 SDValue V) const {
1283 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1284 // so we will end up with redundant moves to m0.
1285 //
1286 // We can't use S_MOV_B32, because there is no way to specify m0 as the
1287 // destination register.
1288 //
1289 // We have to use them both. Machine cse will combine all the S_MOV_B32
1290 // instructions and the register coalescer eliminate the extra copies.
1291 SDNode *M0 = DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, V.getValueType(), V);
1292 return DAG.getCopyToReg(Chain, DL, DAG.getRegister(AMDGPU::M0, MVT::i32),
1293 SDValue(M0, 0), SDValue()); // Glue
1294 // A Null SDValue creates
1295 // a glue result.
1296}
1297
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001298SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1299 SDValue Op,
1300 MVT VT,
1301 unsigned Offset) const {
1302 SDLoc SL(Op);
1303 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1304 DAG.getEntryNode(), Offset, false);
1305 // The local size values will have the hi 16-bits as zero.
1306 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1307 DAG.getValueType(VT));
1308}
1309
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001310SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1311 SelectionDAG &DAG) const {
1312 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001313 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardec2e43c2014-09-22 15:35:29 +00001314 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +00001315 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001316
1317 EVT VT = Op.getValueType();
1318 SDLoc DL(Op);
1319 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1320
Sanjay Patela2607012015-09-16 16:31:21 +00001321 // TODO: Should this propagate fast-math-flags?
1322
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001323 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001324 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault800fecf2016-01-11 21:18:33 +00001325 if (!Subtarget->isAmdHsaOS()) {
1326 DiagnosticInfoUnsupported BadIntrin(*MF.getFunction(),
1327 "hsa intrinsic without hsa target");
1328 DAG.getContext()->diagnose(BadIntrin);
1329 return DAG.getUNDEF(VT);
1330 }
1331
Tom Stellard48f29f22015-11-26 00:43:29 +00001332 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
1333 TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_PTR), VT);
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001334 case Intrinsic::amdgcn_rcp:
1335 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
1336 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001337 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001338 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
1339 case Intrinsic::amdgcn_rsq_clamped:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001340 case AMDGPUIntrinsic::AMDGPU_rsq_clamped: { // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001341 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
1342 return DAG.getNode(AMDGPUISD::RSQ_CLAMPED, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00001343
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001344 Type *Type = VT.getTypeForEVT(*DAG.getContext());
1345 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
1346 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
1347
1348 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
1349 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
1350 DAG.getConstantFP(Max, DL, VT));
1351 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
1352 DAG.getConstantFP(Min, DL, VT));
1353 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001354 case Intrinsic::r600_read_ngroups_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001355 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1356 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001357 case Intrinsic::r600_read_ngroups_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001358 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1359 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001360 case Intrinsic::r600_read_ngroups_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001361 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1362 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001363 case Intrinsic::r600_read_global_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001364 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1365 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001366 case Intrinsic::r600_read_global_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001367 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1368 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001369 case Intrinsic::r600_read_global_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001370 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1371 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001372 case Intrinsic::r600_read_local_size_x:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001373 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1374 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001375 case Intrinsic::r600_read_local_size_y:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001376 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1377 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001378 case Intrinsic::r600_read_local_size_z:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001379 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1380 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenaultbef34e22016-01-22 21:30:34 +00001381 case Intrinsic::amdgcn_read_workdim:
1382 case AMDGPUIntrinsic::AMDGPU_read_workdim: // Legacy name.
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001383 // Really only 2 bits.
1384 return lowerImplicitZextParam(DAG, Op, MVT::i8,
1385 getImplicitParameterOffset(MFI, GRID_DIM));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001386 case Intrinsic::r600_read_tgid_x:
1387 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001388 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001389 case Intrinsic::r600_read_tgid_y:
1390 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001391 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001392 case Intrinsic::r600_read_tgid_z:
1393 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001394 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001395 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001396 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001397 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001398 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001399 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001400 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001401 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001402 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001403 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001404 case AMDGPUIntrinsic::SI_load_const: {
1405 SDValue Ops[] = {
1406 Op.getOperand(1),
1407 Op.getOperand(2)
1408 };
1409
1410 MachineMemOperand *MMO = MF.getMachineMemOperand(
1411 MachinePointerInfo(),
1412 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
1413 VT.getStoreSize(), 4);
1414 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
1415 Op->getVTList(), Ops, VT, MMO);
1416 }
1417 case AMDGPUIntrinsic::SI_sample:
1418 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
1419 case AMDGPUIntrinsic::SI_sampleb:
1420 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
1421 case AMDGPUIntrinsic::SI_sampled:
1422 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
1423 case AMDGPUIntrinsic::SI_samplel:
1424 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
1425 case AMDGPUIntrinsic::SI_vs_load_input:
1426 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1427 Op.getOperand(1),
1428 Op.getOperand(2),
1429 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00001430
Tom Stellard2a9d9472015-05-12 15:00:46 +00001431 case AMDGPUIntrinsic::SI_fs_constant: {
1432 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1433 SDValue Glue = M0.getValue(1);
1434 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
1435 DAG.getConstant(2, DL, MVT::i32), // P0
1436 Op.getOperand(1), Op.getOperand(2), Glue);
1437 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00001438 case AMDGPUIntrinsic::SI_packf16:
1439 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
1440 return DAG.getUNDEF(MVT::i32);
1441 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00001442 case AMDGPUIntrinsic::SI_fs_interp: {
1443 SDValue IJ = Op.getOperand(4);
1444 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1445 DAG.getConstant(0, DL, MVT::i32));
1446 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1447 DAG.getConstant(1, DL, MVT::i32));
1448 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1449 SDValue Glue = M0.getValue(1);
1450 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
1451 DAG.getVTList(MVT::f32, MVT::Glue),
1452 I, Op.getOperand(1), Op.getOperand(2), Glue);
1453 Glue = SDValue(P1.getNode(), 1);
1454 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
1455 Op.getOperand(1), Op.getOperand(2), Glue);
1456 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00001457 case Intrinsic::amdgcn_interp_p1: {
1458 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
1459 SDValue Glue = M0.getValue(1);
1460 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
1461 Op.getOperand(2), Op.getOperand(3), Glue);
1462 }
1463 case Intrinsic::amdgcn_interp_p2: {
1464 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
1465 SDValue Glue = SDValue(M0.getNode(), 1);
1466 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
1467 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
1468 Glue);
1469 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001470 case Intrinsic::amdgcn_ldexp:
1471 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
1472 Op.getOperand(1), Op.getOperand(2));
1473 case Intrinsic::amdgcn_class:
1474 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
1475 Op.getOperand(1), Op.getOperand(2));
1476 case Intrinsic::amdgcn_div_fmas:
1477 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
1478 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
1479 Op.getOperand(4));
1480
1481 case Intrinsic::amdgcn_div_fixup:
1482 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
1483 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
1484
1485 case Intrinsic::amdgcn_trig_preop:
1486 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
1487 Op.getOperand(1), Op.getOperand(2));
1488 case Intrinsic::amdgcn_div_scale: {
1489 // 3rd parameter required to be a constant.
1490 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1491 if (!Param)
1492 return DAG.getUNDEF(VT);
1493
1494 // Translate to the operands expected by the machine instruction. The
1495 // first parameter must be the same as the first instruction.
1496 SDValue Numerator = Op.getOperand(1);
1497 SDValue Denominator = Op.getOperand(2);
1498
1499 // Note this order is opposite of the machine instruction's operations,
1500 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
1501 // intrinsic has the numerator as the first operand to match a normal
1502 // division operation.
1503
1504 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
1505
1506 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
1507 Denominator, Numerator);
1508 }
1509 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte0:
1510 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Op.getOperand(1));
1511 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte1:
1512 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE1, DL, VT, Op.getOperand(1));
1513 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte2:
1514 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE2, DL, VT, Op.getOperand(1));
1515 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte3:
1516 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE3, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001517 default:
1518 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1519 }
1520}
1521
1522SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1523 SelectionDAG &DAG) const {
1524 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00001525 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001526 SDValue Chain = Op.getOperand(0);
1527 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1528
1529 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00001530 case AMDGPUIntrinsic::SI_sendmsg: {
1531 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1532 SDValue Glue = Chain.getValue(1);
1533 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1534 Op.getOperand(2), Glue);
1535 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001536 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001537 SDValue Ops[] = {
1538 Chain,
1539 Op.getOperand(2),
1540 Op.getOperand(3),
1541 Op.getOperand(4),
1542 Op.getOperand(5),
1543 Op.getOperand(6),
1544 Op.getOperand(7),
1545 Op.getOperand(8),
1546 Op.getOperand(9),
1547 Op.getOperand(10),
1548 Op.getOperand(11),
1549 Op.getOperand(12),
1550 Op.getOperand(13),
1551 Op.getOperand(14)
1552 };
1553
1554 EVT VT = Op.getOperand(3).getValueType();
1555
1556 MachineMemOperand *MMO = MF.getMachineMemOperand(
1557 MachinePointerInfo(),
1558 MachineMemOperand::MOStore,
1559 VT.getStoreSize(), 4);
1560 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1561 Op->getVTList(), Ops, VT, MMO);
1562 }
1563 default:
1564 return SDValue();
1565 }
1566}
1567
Tom Stellard81d871d2013-11-13 23:36:50 +00001568SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1569 SDLoc DL(Op);
1570 LoadSDNode *Load = cast<LoadSDNode>(Op);
1571
Tom Stellarde812f2f2014-07-21 15:45:06 +00001572 if (Op.getValueType().isVector()) {
1573 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1574 "Custom lowering for non-i32 vectors hasn't been implemented.");
1575 unsigned NumElements = Op.getValueType().getVectorNumElements();
1576 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001577
Tom Stellarde812f2f2014-07-21 15:45:06 +00001578 switch (Load->getAddressSpace()) {
1579 default: break;
Tom Stellarda6f24c62015-12-15 20:55:55 +00001580 case AMDGPUAS::CONSTANT_ADDRESS:
1581 if (isMemOpUniform(Load))
1582 break;
1583 // Non-uniform loads will be selected to MUBUF instructions, so they
1584 // have the same legalization requires ments as global and private
1585 // loads.
1586 //
1587 // Fall-through
Tom Stellarde812f2f2014-07-21 15:45:06 +00001588 case AMDGPUAS::GLOBAL_ADDRESS:
1589 case AMDGPUAS::PRIVATE_ADDRESS:
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001590 if (NumElements >= 8)
1591 return SplitVectorLoad(Op, DAG);
1592
Tom Stellarde812f2f2014-07-21 15:45:06 +00001593 // v4 loads are supported for private and global memory.
1594 if (NumElements <= 4)
1595 break;
1596 // fall-through
1597 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenaultff05da82015-11-24 12:18:54 +00001598 // If properly aligned, if we split we might be able to use ds_read_b64.
1599 return SplitVectorLoad(Op, DAG);
Tom Stellarde812f2f2014-07-21 15:45:06 +00001600 }
Tom Stellarde9373602014-01-22 19:24:14 +00001601 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001602
Tom Stellarde812f2f2014-07-21 15:45:06 +00001603 return AMDGPUTargetLowering::LowerLOAD(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001604}
1605
Tom Stellard9fa17912013-08-14 23:24:45 +00001606SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
1607 const SDValue &Op,
1608 SelectionDAG &DAG) const {
1609 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
1610 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +00001611 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +00001612 Op.getOperand(4));
1613}
1614
Tom Stellard0ec134f2014-02-04 17:18:40 +00001615SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1616 if (Op.getValueType() != MVT::i64)
1617 return SDValue();
1618
1619 SDLoc DL(Op);
1620 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001621
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001622 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1623 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001624
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001625 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1626 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1627
1628 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1629 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001630
1631 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1632
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001633 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1634 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001635
1636 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1637
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001638 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1639 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001640}
1641
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001642// Catch division cases where we can use shortcuts with rcp and rsq
1643// instructions.
1644SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001645 SDLoc SL(Op);
1646 SDValue LHS = Op.getOperand(0);
1647 SDValue RHS = Op.getOperand(1);
1648 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001649 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001650
1651 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001652 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1653 CLHS->isExactlyValue(1.0)) {
1654 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1655 // the CI documentation has a worst case error of 1 ulp.
1656 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1657 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001658
1659 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001660 //
1661 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1662 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001663 if (RHS.getOpcode() == ISD::FSQRT)
1664 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1665
1666 // 1.0 / x -> rcp(x)
1667 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1668 }
1669 }
1670
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001671 if (Unsafe) {
1672 // Turn into multiply by the reciprocal.
1673 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00001674 SDNodeFlags Flags;
1675 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001676 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00001677 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001678 }
1679
1680 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001681}
1682
1683SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001684 SDValue FastLowered = LowerFastFDIV(Op, DAG);
1685 if (FastLowered.getNode())
1686 return FastLowered;
1687
1688 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1689 // selection error for now rather than do something incorrect.
1690 if (Subtarget->hasFP32Denormals())
1691 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001692
1693 SDLoc SL(Op);
1694 SDValue LHS = Op.getOperand(0);
1695 SDValue RHS = Op.getOperand(1);
1696
1697 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1698
1699 const APFloat K0Val(BitsToFloat(0x6f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001700 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001701
1702 const APFloat K1Val(BitsToFloat(0x2f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001703 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001704
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001705 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001706
Mehdi Amini44ede332015-07-09 02:09:04 +00001707 EVT SetCCVT =
1708 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001709
1710 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1711
1712 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1713
Sanjay Patela2607012015-09-16 16:31:21 +00001714 // TODO: Should this propagate fast-math-flags?
1715
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001716 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1717
1718 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1719
1720 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1721
1722 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1723}
1724
1725SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001726 if (DAG.getTarget().Options.UnsafeFPMath)
1727 return LowerFastFDIV(Op, DAG);
1728
1729 SDLoc SL(Op);
1730 SDValue X = Op.getOperand(0);
1731 SDValue Y = Op.getOperand(1);
1732
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001733 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001734
1735 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
1736
1737 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
1738
1739 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
1740
1741 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
1742
1743 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
1744
1745 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
1746
1747 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
1748
1749 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
1750
1751 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
1752 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
1753
1754 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
1755 NegDivScale0, Mul, DivScale1);
1756
1757 SDValue Scale;
1758
1759 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1760 // Workaround a hardware bug on SI where the condition output from div_scale
1761 // is not usable.
1762
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001763 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001764
1765 // Figure out if the scale to use for div_fmas.
1766 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1767 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
1768 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
1769 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
1770
1771 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
1772 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
1773
1774 SDValue Scale0Hi
1775 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
1776 SDValue Scale1Hi
1777 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
1778
1779 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
1780 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
1781 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
1782 } else {
1783 Scale = DivScale1.getValue(1);
1784 }
1785
1786 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
1787 Fma4, Fma3, Mul, Scale);
1788
1789 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001790}
1791
1792SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1793 EVT VT = Op.getValueType();
1794
1795 if (VT == MVT::f32)
1796 return LowerFDIV32(Op, DAG);
1797
1798 if (VT == MVT::f64)
1799 return LowerFDIV64(Op, DAG);
1800
1801 llvm_unreachable("Unexpected type for fdiv");
1802}
1803
Tom Stellard81d871d2013-11-13 23:36:50 +00001804SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1805 SDLoc DL(Op);
1806 StoreSDNode *Store = cast<StoreSDNode>(Op);
1807 EVT VT = Store->getMemoryVT();
1808
Tom Stellard9b3816b2014-06-24 23:33:04 +00001809 // These stores are legal.
Tom Stellardb02094e2014-07-21 15:45:01 +00001810 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1811 if (VT.isVector() && VT.getVectorNumElements() > 4)
Matt Arsenault83e60582014-07-24 17:10:35 +00001812 return ScalarizeVectorStore(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001813 return SDValue();
1814 }
1815
Tom Stellard81d871d2013-11-13 23:36:50 +00001816 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1817 if (Ret.getNode())
1818 return Ret;
1819
1820 if (VT.isVector() && VT.getVectorNumElements() >= 8)
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001821 return SplitVectorStore(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001822
Tom Stellard1c8788e2014-03-07 20:12:33 +00001823 if (VT == MVT::i1)
1824 return DAG.getTruncStore(Store->getChain(), DL,
1825 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1826 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1827
Tom Stellarde812f2f2014-07-21 15:45:06 +00001828 return SDValue();
Tom Stellard81d871d2013-11-13 23:36:50 +00001829}
1830
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001831SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001832 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001833 EVT VT = Op.getValueType();
1834 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00001835 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001836 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
1837 DAG.getNode(ISD::FMUL, DL, VT, Arg,
1838 DAG.getConstantFP(0.5/M_PI, DL,
1839 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001840
1841 switch (Op.getOpcode()) {
1842 case ISD::FCOS:
1843 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1844 case ISD::FSIN:
1845 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1846 default:
1847 llvm_unreachable("Wrong trig opcode");
1848 }
1849}
1850
Tom Stellard75aadc22012-12-11 21:25:42 +00001851//===----------------------------------------------------------------------===//
1852// Custom DAG optimizations
1853//===----------------------------------------------------------------------===//
1854
Matt Arsenault364a6742014-06-11 17:50:44 +00001855SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00001856 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00001857 EVT VT = N->getValueType(0);
1858 EVT ScalarVT = VT.getScalarType();
1859 if (ScalarVT != MVT::f32)
1860 return SDValue();
1861
1862 SelectionDAG &DAG = DCI.DAG;
1863 SDLoc DL(N);
1864
1865 SDValue Src = N->getOperand(0);
1866 EVT SrcVT = Src.getValueType();
1867
1868 // TODO: We could try to match extracting the higher bytes, which would be
1869 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1870 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1871 // about in practice.
1872 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1873 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1874 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1875 DCI.AddToWorklist(Cvt.getNode());
1876 return Cvt;
1877 }
1878 }
1879
1880 // We are primarily trying to catch operations on illegal vector types
1881 // before they are expanded.
1882 // For scalars, we can use the more flexible method of checking masked bits
1883 // after legalization.
1884 if (!DCI.isBeforeLegalize() ||
1885 !SrcVT.isVector() ||
1886 SrcVT.getVectorElementType() != MVT::i8) {
1887 return SDValue();
1888 }
1889
1890 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1891
1892 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1893 // size as 4.
1894 unsigned NElts = SrcVT.getVectorNumElements();
1895 if (!SrcVT.isSimple() && NElts != 3)
1896 return SDValue();
1897
1898 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1899 // prevent a mess from expanding to v4i32 and repacking.
1900 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1901 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1902 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1903 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00001904 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001905
1906 unsigned AS = Load->getAddressSpace();
1907 unsigned Align = Load->getAlignment();
1908 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001909 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001910
1911 // Don't try to replace the load if we have to expand it due to alignment
1912 // problems. Otherwise we will end up scalarizing the load, and trying to
1913 // repack into the vector for no real reason.
1914 if (Align < ABIAlignment &&
1915 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
1916 return SDValue();
1917 }
1918
Matt Arsenault364a6742014-06-11 17:50:44 +00001919 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1920 Load->getChain(),
1921 Load->getBasePtr(),
1922 LoadVT,
1923 Load->getMemOperand());
1924
1925 // Make sure successors of the original load stay after it by updating
1926 // them to use the new Chain.
1927 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1928
1929 SmallVector<SDValue, 4> Elts;
1930 if (RegVT.isVector())
1931 DAG.ExtractVectorElements(NewLoad, Elts);
1932 else
1933 Elts.push_back(NewLoad);
1934
1935 SmallVector<SDValue, 4> Ops;
1936
1937 unsigned EltIdx = 0;
1938 for (SDValue Elt : Elts) {
1939 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1940 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1941 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1942 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1943 DCI.AddToWorklist(Cvt.getNode());
1944 Ops.push_back(Cvt);
1945 }
1946
1947 ++EltIdx;
1948 }
1949
1950 assert(Ops.size() == NElts);
1951
1952 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1953 }
1954
1955 return SDValue();
1956}
1957
Eric Christopher6c5b5112015-03-11 18:43:21 +00001958/// \brief Return true if the given offset Size in bytes can be folded into
1959/// the immediate offsets of a memory instruction for the given address space.
1960static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
1961 const AMDGPUSubtarget &STI) {
1962 switch (AS) {
1963 case AMDGPUAS::GLOBAL_ADDRESS: {
1964 // MUBUF instructions a 12-bit offset in bytes.
1965 return isUInt<12>(OffsetSize);
1966 }
1967 case AMDGPUAS::CONSTANT_ADDRESS: {
1968 // SMRD instructions have an 8-bit offset in dwords on SI and
1969 // a 20-bit offset in bytes on VI.
1970 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
1971 return isUInt<20>(OffsetSize);
1972 else
1973 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
1974 }
1975 case AMDGPUAS::LOCAL_ADDRESS:
1976 case AMDGPUAS::REGION_ADDRESS: {
1977 // The single offset versions have a 16-bit offset in bytes.
1978 return isUInt<16>(OffsetSize);
1979 }
1980 case AMDGPUAS::PRIVATE_ADDRESS:
1981 // Indirect register addressing does not use any offsets.
1982 default:
1983 return 0;
1984 }
1985}
1986
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001987// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
1988
1989// This is a variant of
1990// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
1991//
1992// The normal DAG combiner will do this, but only if the add has one use since
1993// that would increase the number of instructions.
1994//
1995// This prevents us from seeing a constant offset that can be folded into a
1996// memory instruction's addressing mode. If we know the resulting add offset of
1997// a pointer can be folded into an addressing offset, we can replace the pointer
1998// operand with the add of new constant offset. This eliminates one of the uses,
1999// and may allow the remaining use to also be simplified.
2000//
2001SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
2002 unsigned AddrSpace,
2003 DAGCombinerInfo &DCI) const {
2004 SDValue N0 = N->getOperand(0);
2005 SDValue N1 = N->getOperand(1);
2006
2007 if (N0.getOpcode() != ISD::ADD)
2008 return SDValue();
2009
2010 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
2011 if (!CN1)
2012 return SDValue();
2013
2014 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2015 if (!CAdd)
2016 return SDValue();
2017
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002018 // If the resulting offset is too large, we can't fold it into the addressing
2019 // mode offset.
2020 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Eric Christopher6c5b5112015-03-11 18:43:21 +00002021 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002022 return SDValue();
2023
2024 SelectionDAG &DAG = DCI.DAG;
2025 SDLoc SL(N);
2026 EVT VT = N->getValueType(0);
2027
2028 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002029 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002030
2031 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
2032}
2033
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002034SDValue SITargetLowering::performAndCombine(SDNode *N,
2035 DAGCombinerInfo &DCI) const {
2036 if (DCI.isBeforeLegalize())
2037 return SDValue();
2038
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002039 if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI))
2040 return Base;
2041
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002042 SelectionDAG &DAG = DCI.DAG;
2043
2044 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
2045 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
2046 SDValue LHS = N->getOperand(0);
2047 SDValue RHS = N->getOperand(1);
2048
2049 if (LHS.getOpcode() == ISD::SETCC &&
2050 RHS.getOpcode() == ISD::SETCC) {
2051 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2052 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2053
2054 SDValue X = LHS.getOperand(0);
2055 SDValue Y = RHS.getOperand(0);
2056 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2057 return SDValue();
2058
2059 if (LCC == ISD::SETO) {
2060 if (X != LHS.getOperand(1))
2061 return SDValue();
2062
2063 if (RCC == ISD::SETUNE) {
2064 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2065 if (!C1 || !C1->isInfinity() || C1->isNegative())
2066 return SDValue();
2067
2068 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2069 SIInstrFlags::N_SUBNORMAL |
2070 SIInstrFlags::N_ZERO |
2071 SIInstrFlags::P_ZERO |
2072 SIInstrFlags::P_SUBNORMAL |
2073 SIInstrFlags::P_NORMAL;
2074
2075 static_assert(((~(SIInstrFlags::S_NAN |
2076 SIInstrFlags::Q_NAN |
2077 SIInstrFlags::N_INFINITY |
2078 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2079 "mask not equal");
2080
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002081 SDLoc DL(N);
2082 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2083 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002084 }
2085 }
2086 }
2087
2088 return SDValue();
2089}
2090
Matt Arsenaultf2290332015-01-06 23:00:39 +00002091SDValue SITargetLowering::performOrCombine(SDNode *N,
2092 DAGCombinerInfo &DCI) const {
2093 SelectionDAG &DAG = DCI.DAG;
2094 SDValue LHS = N->getOperand(0);
2095 SDValue RHS = N->getOperand(1);
2096
2097 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
2098 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
2099 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
2100 SDValue Src = LHS.getOperand(0);
2101 if (Src != RHS.getOperand(0))
2102 return SDValue();
2103
2104 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
2105 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
2106 if (!CLHS || !CRHS)
2107 return SDValue();
2108
2109 // Only 10 bits are used.
2110 static const uint32_t MaxMask = 0x3ff;
2111
2112 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002113 SDLoc DL(N);
2114 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2115 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00002116 }
2117
2118 return SDValue();
2119}
2120
2121SDValue SITargetLowering::performClassCombine(SDNode *N,
2122 DAGCombinerInfo &DCI) const {
2123 SelectionDAG &DAG = DCI.DAG;
2124 SDValue Mask = N->getOperand(1);
2125
2126 // fp_class x, 0 -> false
2127 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
2128 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002129 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002130 }
2131
2132 return SDValue();
2133}
2134
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002135static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
2136 switch (Opc) {
2137 case ISD::FMAXNUM:
2138 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002139 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002140 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002141 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002142 return AMDGPUISD::UMAX3;
2143 case ISD::FMINNUM:
2144 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002145 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002146 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002147 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002148 return AMDGPUISD::UMIN3;
2149 default:
2150 llvm_unreachable("Not a min/max opcode");
2151 }
2152}
2153
2154SDValue SITargetLowering::performMin3Max3Combine(SDNode *N,
2155 DAGCombinerInfo &DCI) const {
2156 SelectionDAG &DAG = DCI.DAG;
2157
2158 unsigned Opc = N->getOpcode();
2159 SDValue Op0 = N->getOperand(0);
2160 SDValue Op1 = N->getOperand(1);
2161
2162 // Only do this if the inner op has one use since this will just increases
2163 // register pressure for no benefit.
2164
2165 // max(max(a, b), c)
2166 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
2167 SDLoc DL(N);
2168 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2169 DL,
2170 N->getValueType(0),
2171 Op0.getOperand(0),
2172 Op0.getOperand(1),
2173 Op1);
2174 }
2175
2176 // max(a, max(b, c))
2177 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
2178 SDLoc DL(N);
2179 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2180 DL,
2181 N->getValueType(0),
2182 Op0,
2183 Op1.getOperand(0),
2184 Op1.getOperand(1));
2185 }
2186
2187 return SDValue();
2188}
2189
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002190SDValue SITargetLowering::performSetCCCombine(SDNode *N,
2191 DAGCombinerInfo &DCI) const {
2192 SelectionDAG &DAG = DCI.DAG;
2193 SDLoc SL(N);
2194
2195 SDValue LHS = N->getOperand(0);
2196 SDValue RHS = N->getOperand(1);
2197 EVT VT = LHS.getValueType();
2198
2199 if (VT != MVT::f32 && VT != MVT::f64)
2200 return SDValue();
2201
2202 // Match isinf pattern
2203 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
2204 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
2205 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
2206 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2207 if (!CRHS)
2208 return SDValue();
2209
2210 const APFloat &APF = CRHS->getValueAPF();
2211 if (APF.isInfinity() && !APF.isNegative()) {
2212 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002213 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
2214 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002215 }
2216 }
2217
2218 return SDValue();
2219}
2220
Tom Stellard75aadc22012-12-11 21:25:42 +00002221SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
2222 DAGCombinerInfo &DCI) const {
2223 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002224 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00002225
2226 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00002227 default:
2228 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002229 case ISD::SETCC:
2230 return performSetCCCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002231 case ISD::FMAXNUM: // TODO: What about fmax_legacy?
2232 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002233 case ISD::SMAX:
2234 case ISD::SMIN:
2235 case ISD::UMAX:
2236 case ISD::UMIN: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002237 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00002238 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002239 getTargetMachine().getOptLevel() > CodeGenOpt::None)
2240 return performMin3Max3Combine(N, DCI);
2241 break;
2242 }
Matt Arsenault364a6742014-06-11 17:50:44 +00002243
2244 case AMDGPUISD::CVT_F32_UBYTE0:
2245 case AMDGPUISD::CVT_F32_UBYTE1:
2246 case AMDGPUISD::CVT_F32_UBYTE2:
2247 case AMDGPUISD::CVT_F32_UBYTE3: {
2248 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
2249
2250 SDValue Src = N->getOperand(0);
2251 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
2252
2253 APInt KnownZero, KnownOne;
2254 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2255 !DCI.isBeforeLegalizeOps());
2256 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2257 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
2258 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
2259 DCI.CommitTargetLoweringOpt(TLO);
2260 }
2261
2262 break;
2263 }
2264
2265 case ISD::UINT_TO_FP: {
2266 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002267 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002268 case ISD::FADD: {
2269 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2270 break;
2271
2272 EVT VT = N->getValueType(0);
2273 if (VT != MVT::f32)
2274 break;
2275
Matt Arsenault8d630032015-02-20 22:10:41 +00002276 // Only do this if we are not trying to support denormals. v_mad_f32 does
2277 // not support denormals ever.
2278 if (Subtarget->hasFP32Denormals())
2279 break;
2280
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002281 SDValue LHS = N->getOperand(0);
2282 SDValue RHS = N->getOperand(1);
2283
2284 // These should really be instruction patterns, but writing patterns with
2285 // source modiifiers is a pain.
2286
2287 // fadd (fadd (a, a), b) -> mad 2.0, a, b
2288 if (LHS.getOpcode() == ISD::FADD) {
2289 SDValue A = LHS.getOperand(0);
2290 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002291 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002292 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002293 }
2294 }
2295
2296 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
2297 if (RHS.getOpcode() == ISD::FADD) {
2298 SDValue A = RHS.getOperand(0);
2299 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002300 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002301 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002302 }
2303 }
2304
Matt Arsenault8d630032015-02-20 22:10:41 +00002305 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002306 }
Matt Arsenault8675db12014-08-29 16:01:14 +00002307 case ISD::FSUB: {
2308 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2309 break;
2310
2311 EVT VT = N->getValueType(0);
2312
2313 // Try to get the fneg to fold into the source modifier. This undoes generic
2314 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00002315 //
2316 // Only do this if we are not trying to support denormals. v_mad_f32 does
2317 // not support denormals ever.
2318 if (VT == MVT::f32 &&
2319 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00002320 SDValue LHS = N->getOperand(0);
2321 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002322 if (LHS.getOpcode() == ISD::FADD) {
2323 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
2324
2325 SDValue A = LHS.getOperand(0);
2326 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002327 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002328 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
2329
Matt Arsenault8d630032015-02-20 22:10:41 +00002330 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002331 }
2332 }
2333
2334 if (RHS.getOpcode() == ISD::FADD) {
2335 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
2336
2337 SDValue A = RHS.getOperand(0);
2338 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002339 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002340 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002341 }
2342 }
Matt Arsenault8d630032015-02-20 22:10:41 +00002343
2344 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00002345 }
2346
2347 break;
2348 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002349 case ISD::LOAD:
2350 case ISD::STORE:
2351 case ISD::ATOMIC_LOAD:
2352 case ISD::ATOMIC_STORE:
2353 case ISD::ATOMIC_CMP_SWAP:
2354 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2355 case ISD::ATOMIC_SWAP:
2356 case ISD::ATOMIC_LOAD_ADD:
2357 case ISD::ATOMIC_LOAD_SUB:
2358 case ISD::ATOMIC_LOAD_AND:
2359 case ISD::ATOMIC_LOAD_OR:
2360 case ISD::ATOMIC_LOAD_XOR:
2361 case ISD::ATOMIC_LOAD_NAND:
2362 case ISD::ATOMIC_LOAD_MIN:
2363 case ISD::ATOMIC_LOAD_MAX:
2364 case ISD::ATOMIC_LOAD_UMIN:
2365 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
2366 if (DCI.isBeforeLegalize())
2367 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002368
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002369 MemSDNode *MemNode = cast<MemSDNode>(N);
2370 SDValue Ptr = MemNode->getBasePtr();
2371
2372 // TODO: We could also do this for multiplies.
2373 unsigned AS = MemNode->getAddressSpace();
2374 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
2375 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
2376 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002377 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002378
2379 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
2380 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
2381 }
2382 }
2383 break;
2384 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002385 case ISD::AND:
2386 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002387 case ISD::OR:
2388 return performOrCombine(N, DCI);
2389 case AMDGPUISD::FP_CLASS:
2390 return performClassCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002391 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002392 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00002393}
Christian Konigd910b7d2013-02-26 17:52:16 +00002394
Christian Konigf82901a2013-02-26 17:52:23 +00002395/// \brief Analyze the possible immediate value Op
2396///
2397/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
2398/// and the immediate value if it's a literal immediate
2399int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
2400
Eric Christopher7792e322015-01-30 23:24:40 +00002401 const SIInstrInfo *TII =
2402 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00002403
Tom Stellardedbf1eb2013-04-05 23:31:20 +00002404 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00002405 if (TII->isInlineConstant(Node->getAPIntValue()))
2406 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00002407
Matt Arsenault11a4d672015-02-13 19:05:03 +00002408 uint64_t Val = Node->getZExtValue();
2409 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00002410 }
2411
2412 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
2413 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
2414 return 0;
2415
2416 if (Node->getValueType(0) == MVT::f32)
2417 return FloatToBits(Node->getValueAPF().convertToFloat());
2418
2419 return -1;
2420 }
2421
2422 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00002423}
2424
Christian Konig8e06e2a2013-04-10 08:39:08 +00002425/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00002426static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00002427 switch (Idx) {
2428 default: return 0;
2429 case AMDGPU::sub0: return 0;
2430 case AMDGPU::sub1: return 1;
2431 case AMDGPU::sub2: return 2;
2432 case AMDGPU::sub3: return 3;
2433 }
2434}
2435
2436/// \brief Adjust the writemask of MIMG instructions
2437void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
2438 SelectionDAG &DAG) const {
2439 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00002440 unsigned Lane = 0;
2441 unsigned OldDmask = Node->getConstantOperandVal(0);
2442 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002443
2444 // Try to figure out the used register components
2445 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
2446 I != E; ++I) {
2447
2448 // Abort if we can't understand the usage
2449 if (!I->isMachineOpcode() ||
2450 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
2451 return;
2452
Tom Stellard54774e52013-10-23 02:53:47 +00002453 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
2454 // Note that subregs are packed, i.e. Lane==0 is the first bit set
2455 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
2456 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00002457 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00002458
Tom Stellard54774e52013-10-23 02:53:47 +00002459 // Set which texture component corresponds to the lane.
2460 unsigned Comp;
2461 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
2462 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00002463 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00002464 Dmask &= ~(1 << Comp);
2465 }
2466
Christian Konig8e06e2a2013-04-10 08:39:08 +00002467 // Abort if we have more than one user per component
2468 if (Users[Lane])
2469 return;
2470
2471 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00002472 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002473 }
2474
Tom Stellard54774e52013-10-23 02:53:47 +00002475 // Abort if there's no change
2476 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00002477 return;
2478
2479 // Adjust the writemask in the node
2480 std::vector<SDValue> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002481 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002482 Ops.insert(Ops.end(), Node->op_begin() + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00002483 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002484
Christian Konig8b1ed282013-04-10 08:39:16 +00002485 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00002486 // (if NewDmask has only one bit set...)
2487 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002488 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
2489 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00002490 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002491 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00002492 SDValue(Node, 0), RC);
2493 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
2494 return;
2495 }
2496
Christian Konig8e06e2a2013-04-10 08:39:08 +00002497 // Update the users of the node with the new indices
2498 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
2499
2500 SDNode *User = Users[i];
2501 if (!User)
2502 continue;
2503
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002504 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002505 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
2506
2507 switch (Idx) {
2508 default: break;
2509 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
2510 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
2511 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
2512 }
2513 }
2514}
2515
Tom Stellardc98ee202015-07-16 19:40:07 +00002516static bool isFrameIndexOp(SDValue Op) {
2517 if (Op.getOpcode() == ISD::AssertZext)
2518 Op = Op.getOperand(0);
2519
2520 return isa<FrameIndexSDNode>(Op);
2521}
2522
Tom Stellard3457a842014-10-09 19:06:00 +00002523/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
2524/// with frame index operands.
2525/// LLVM assumes that inputs are to these instructions are registers.
2526void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
2527 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002528
2529 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00002530 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00002531 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00002532 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002533 continue;
2534 }
2535
Tom Stellard3457a842014-10-09 19:06:00 +00002536 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002537 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00002538 Node->getOperand(i).getValueType(),
2539 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002540 }
2541
Tom Stellard3457a842014-10-09 19:06:00 +00002542 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002543}
2544
Matt Arsenault08d84942014-06-03 23:06:13 +00002545/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00002546SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
2547 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002548 const SIInstrInfo *TII =
2549 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konig8e06e2a2013-04-10 08:39:08 +00002550
Tom Stellard16a9a202013-08-14 23:24:17 +00002551 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00002552 adjustWritemask(Node, DAG);
2553
Matt Arsenault7d858d82014-11-02 23:46:54 +00002554 if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG ||
2555 Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002556 legalizeTargetIndependentNode(Node, DAG);
2557 return Node;
2558 }
Tom Stellard654d6692015-01-08 15:08:17 +00002559 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002560}
Christian Konig8b1ed282013-04-10 08:39:16 +00002561
2562/// \brief Assign the register class depending on the number of
2563/// bits set in the writemask
2564void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
2565 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002566 const SIInstrInfo *TII =
2567 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002568
Tom Stellarda99ada52014-11-21 22:31:44 +00002569 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00002570
2571 if (TII->isVOP3(MI->getOpcode())) {
2572 // Make sure constant bus requirements are respected.
2573 TII->legalizeOperandsVOP3(MRI, MI);
2574 return;
2575 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00002576
Matt Arsenault3add6432015-10-20 04:35:43 +00002577 if (TII->isMIMG(*MI)) {
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002578 unsigned VReg = MI->getOperand(0).getReg();
2579 unsigned Writemask = MI->getOperand(1).getImm();
2580 unsigned BitsSet = 0;
2581 for (unsigned i = 0; i < 4; ++i)
2582 BitsSet += Writemask & (1 << i) ? 1 : 0;
2583
2584 const TargetRegisterClass *RC;
2585 switch (BitsSet) {
2586 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002587 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002588 case 2: RC = &AMDGPU::VReg_64RegClass; break;
2589 case 3: RC = &AMDGPU::VReg_96RegClass; break;
2590 }
2591
2592 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
2593 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002594 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00002595 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00002596 }
2597
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002598 // Replace unused atomics with the no return version.
2599 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
2600 if (NoRetAtomicOp != -1) {
2601 if (!Node->hasAnyUseOfValue(0)) {
2602 MI->setDesc(TII->get(NoRetAtomicOp));
2603 MI->RemoveOperand(0);
2604 }
2605
2606 return;
2607 }
Christian Konig8b1ed282013-04-10 08:39:16 +00002608}
Tom Stellard0518ff82013-06-03 17:39:58 +00002609
Matt Arsenault485defe2014-11-05 19:01:17 +00002610static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002611 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00002612 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
2613}
2614
2615MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
2616 SDLoc DL,
2617 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002618 const SIInstrInfo *TII =
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002619 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00002620
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002621 // Build the half of the subregister with the constants before building the
2622 // full 128-bit register. If we are building multiple resource descriptors,
2623 // this will allow CSEing of the 2-component register.
2624 const SDValue Ops0[] = {
2625 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
2626 buildSMovImm32(DAG, DL, 0),
2627 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
2628 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
2629 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
2630 };
Matt Arsenault485defe2014-11-05 19:01:17 +00002631
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002632 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
2633 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00002634
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002635 // Combine the constants and the pointer.
2636 const SDValue Ops1[] = {
2637 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
2638 Ptr,
2639 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
2640 SubRegHi,
2641 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
2642 };
Matt Arsenault485defe2014-11-05 19:01:17 +00002643
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002644 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00002645}
2646
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002647/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002648/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
2649/// of the resource descriptor) to create an offset, which is added to
2650/// the resource pointer.
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002651MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
2652 SDLoc DL,
2653 SDValue Ptr,
2654 uint32_t RsrcDword1,
2655 uint64_t RsrcDword2And3) const {
2656 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
2657 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
2658 if (RsrcDword1) {
2659 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002660 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
2661 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002662 }
2663
2664 SDValue DataLo = buildSMovImm32(DAG, DL,
2665 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
2666 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
2667
2668 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002669 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002670 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002671 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002672 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002673 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002674 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002675 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002676 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002677 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002678 };
2679
2680 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2681}
2682
Tom Stellard94593ee2013-06-03 17:40:18 +00002683SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2684 const TargetRegisterClass *RC,
2685 unsigned Reg, EVT VT) const {
2686 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2687
2688 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2689 cast<RegisterSDNode>(VReg)->getReg(), VT);
2690}
Tom Stellardd7e6f132015-04-08 01:09:26 +00002691
2692//===----------------------------------------------------------------------===//
2693// SI Inline Assembly Support
2694//===----------------------------------------------------------------------===//
2695
2696std::pair<unsigned, const TargetRegisterClass *>
2697SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002698 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00002699 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002700
2701 if (Constraint.size() == 1) {
2702 switch (Constraint[0]) {
2703 case 's':
2704 case 'r':
2705 switch (VT.getSizeInBits()) {
2706 default:
2707 return std::make_pair(0U, nullptr);
2708 case 32:
Tom Stellardd7e6f132015-04-08 01:09:26 +00002709 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002710 case 64:
2711 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
2712 case 128:
2713 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
2714 case 256:
2715 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
2716 }
2717
2718 case 'v':
2719 switch (VT.getSizeInBits()) {
2720 default:
2721 return std::make_pair(0U, nullptr);
2722 case 32:
2723 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
2724 case 64:
2725 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
2726 case 96:
2727 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
2728 case 128:
2729 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
2730 case 256:
2731 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
2732 case 512:
2733 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
2734 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00002735 }
2736 }
2737
2738 if (Constraint.size() > 1) {
2739 const TargetRegisterClass *RC = nullptr;
2740 if (Constraint[1] == 'v') {
2741 RC = &AMDGPU::VGPR_32RegClass;
2742 } else if (Constraint[1] == 's') {
2743 RC = &AMDGPU::SGPR_32RegClass;
2744 }
2745
2746 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00002747 uint32_t Idx;
2748 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
2749 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00002750 return std::make_pair(RC->getRegister(Idx), RC);
2751 }
2752 }
2753 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
2754}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002755
2756SITargetLowering::ConstraintType
2757SITargetLowering::getConstraintType(StringRef Constraint) const {
2758 if (Constraint.size() == 1) {
2759 switch (Constraint[0]) {
2760 default: break;
2761 case 's':
2762 case 'v':
2763 return C_RegisterClass;
2764 }
2765 }
2766 return TargetLowering::getConstraintType(Constraint);
2767}