blob: bb9a5a9205b4ead805f9dac5b702e271e3dbfaeb [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief Custom DAG lowering for SI
12//
13//===----------------------------------------------------------------------===//
14
NAKAMURA Takumi45e0a832014-07-20 11:15:07 +000015#ifdef _MSC_VER
16// Provide M_PI.
17#define _USE_MATH_DEFINES
18#include <cmath>
19#endif
20
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "SIISelLowering.h"
Christian Konig99ee0f42013-03-07 09:04:14 +000022#include "AMDGPU.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000023#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000024#include "AMDGPUSubtarget.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025#include "SIInstrInfo.h"
26#include "SIMachineFunctionInfo.h"
27#include "SIRegisterInfo.h"
Alexey Samsonova253bf92014-08-27 19:36:53 +000028#include "llvm/ADT/BitVector.h"
Matt Arsenault9a10cea2016-01-26 04:29:24 +000029#include "llvm/ADT/StringSwitch.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000030#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
32#include "llvm/CodeGen/MachineRegisterInfo.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Oliver Stannard7e7d9832016-02-02 13:52:43 +000034#include "llvm/IR/DiagnosticInfo.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000035#include "llvm/IR/Function.h"
Matt Arsenault364a6742014-06-11 17:50:44 +000036#include "llvm/ADT/SmallString.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000037
38using namespace llvm;
39
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
Tom Stellard334b29c2014-04-17 21:00:09 +000046 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000047 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000048
Tom Stellard436780b2014-05-15 14:41:57 +000049 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
50 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
51 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000052
Matt Arsenault61001bb2015-11-25 19:58:34 +000053 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
54 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
55
Tom Stellard436780b2014-05-15 14:41:57 +000056 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
57 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000058
Tom Stellardf0a21072014-11-18 20:39:39 +000059 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000060 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
61
Tom Stellardf0a21072014-11-18 20:39:39 +000062 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000063 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000064
Eric Christopher23a3a7c2015-02-26 00:00:24 +000065 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000066
Christian Konig2989ffc2013-03-18 11:34:16 +000067 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
68 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
69 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
70 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
71
Tom Stellard75aadc22012-12-11 21:25:42 +000072 setOperationAction(ISD::ADD, MVT::i32, Legal);
Matt Arsenaulte8d21462013-11-18 20:09:40 +000073 setOperationAction(ISD::ADDC, MVT::i32, Legal);
74 setOperationAction(ISD::ADDE, MVT::i32, Legal);
Matt Arsenaultb8b51532014-06-23 18:00:38 +000075 setOperationAction(ISD::SUBC, MVT::i32, Legal);
76 setOperationAction(ISD::SUBE, MVT::i32, Legal);
Aaron Watrydaabb202013-06-25 13:55:52 +000077
Matt Arsenaultad14ce82014-07-19 18:44:39 +000078 setOperationAction(ISD::FSIN, MVT::f32, Custom);
79 setOperationAction(ISD::FCOS, MVT::f32, Custom);
80
Matt Arsenault7c936902014-10-21 23:01:01 +000081 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
82 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
83
Tom Stellard35bb18c2013-08-26 15:06:04 +000084 // We need to custom lower vector stores from local memory
Tom Stellard35bb18c2013-08-26 15:06:04 +000085 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000086 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
87 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
88
89 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
90 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000091
Tom Stellard1c8788e2014-03-07 20:12:33 +000092 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000093 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
94
Tom Stellard0ec134f2014-02-04 17:18:40 +000095 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +000096 setOperationAction(ISD::SELECT, MVT::f64, Promote);
97 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +000098
Tom Stellard3ca1bfc2014-06-10 16:01:22 +000099 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
100 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
101 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
102 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000103
Tom Stellardd1efda82016-01-20 21:48:24 +0000104 setOperationAction(ISD::SETCC, MVT::i1, Promote);
Tom Stellard83747202013-07-18 21:43:53 +0000105 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
106 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
107
Matt Arsenaulte306a322014-10-21 16:25:08 +0000108 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
Matt Arsenaultd0792852015-12-14 17:25:38 +0000109 setOperationAction(ISD::BITREVERSE, MVT::i32, Legal);
Matt Arsenaulte306a322014-10-21 16:25:08 +0000110
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000111 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
114
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000115 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000116 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
118
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
122
Matt Arsenault94812212014-11-14 18:18:16 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
125
Tom Stellard94593ee2013-06-03 17:40:18 +0000126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000127 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
128 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
129 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000130
Tom Stellardafcf12f2013-09-12 02:55:14 +0000131 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000132 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellardbc4497b2016-02-12 23:45:29 +0000133 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
134 setOperationAction(ISD::BR_CC, MVT::i64, Expand);
135 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
136 setOperationAction(ISD::BR_CC, MVT::f64, Expand);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000137
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000138 for (MVT VT : MVT::integer_valuetypes()) {
Matt Arsenaultbd223422015-01-14 01:35:17 +0000139 if (VT == MVT::i64)
140 continue;
141
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000142 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000143 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
144 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000145 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000146
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000147 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000148 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
149 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000150 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000151
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000152 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000153 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
154 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000155 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
156 }
157
158 for (MVT VT : MVT::integer_vector_valuetypes()) {
159 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
160 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
161 }
162
163 for (MVT VT : MVT::fp_valuetypes())
164 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000165
Matt Arsenault61001bb2015-11-25 19:58:34 +0000166 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
167 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
168
Matt Arsenault6f243792013-09-05 19:41:10 +0000169 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000170 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
Matt Arsenaulte1ce3442015-07-31 04:12:04 +0000171 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000172 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000173
Matt Arsenault61001bb2015-11-25 19:58:34 +0000174
175 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
176
177 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
178 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
179
Matt Arsenault470acd82014-04-15 22:28:39 +0000180 setOperationAction(ISD::LOAD, MVT::i1, Custom);
181
Matt Arsenault61001bb2015-11-25 19:58:34 +0000182 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
183 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
184
185 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
186 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
187
188 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
189
Tom Stellardfd155822013-08-26 15:05:36 +0000190 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000191 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000192 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000193
Tom Stellard5f337882014-04-29 23:12:43 +0000194 // These should use UDIVREM, so set them to expand
195 setOperationAction(ISD::UDIV, MVT::i64, Expand);
196 setOperationAction(ISD::UREM, MVT::i64, Expand);
197
Matt Arsenault0d89e842014-07-15 21:44:37 +0000198 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
199 setOperationAction(ISD::SELECT, MVT::i1, Promote);
200
Matt Arsenault61001bb2015-11-25 19:58:34 +0000201 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
202
203
204 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
205
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000206 // We only support LOAD/STORE and vector manipulation ops for vectors
207 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000208 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000209 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
210 switch(Op) {
211 case ISD::LOAD:
212 case ISD::STORE:
213 case ISD::BUILD_VECTOR:
214 case ISD::BITCAST:
215 case ISD::EXTRACT_VECTOR_ELT:
216 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000217 case ISD::INSERT_SUBVECTOR:
218 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000219 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000220 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000221 case ISD::CONCAT_VECTORS:
222 setOperationAction(Op, VT, Custom);
223 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000224 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000225 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000226 break;
227 }
228 }
229 }
230
Matt Arsenault61001bb2015-11-25 19:58:34 +0000231 // Most operations are naturally 32-bit vector operations. We only support
232 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
233 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
234 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
235 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
236
237 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
238 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
239
240 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
241 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
242
243 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
244 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
245 }
246
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000247 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
248 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
249 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000250 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000251 }
252
Marek Olsak7d777282015-03-24 13:40:15 +0000253 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000254 setOperationAction(ISD::FDIV, MVT::f32, Custom);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +0000255 setOperationAction(ISD::FDIV, MVT::f64, Custom);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000256
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000257 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000258 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000259 setTargetDAGCombine(ISD::FMINNUM);
260 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000261 setTargetDAGCombine(ISD::SMIN);
262 setTargetDAGCombine(ISD::SMAX);
263 setTargetDAGCombine(ISD::UMIN);
264 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000265 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000266 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000267 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000268 setTargetDAGCombine(ISD::UINT_TO_FP);
269
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000270 // All memory operations. Some folding on the pointer operand is done to help
271 // matching the constant offsets in the addressing modes.
272 setTargetDAGCombine(ISD::LOAD);
273 setTargetDAGCombine(ISD::STORE);
274 setTargetDAGCombine(ISD::ATOMIC_LOAD);
275 setTargetDAGCombine(ISD::ATOMIC_STORE);
276 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
277 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
278 setTargetDAGCombine(ISD::ATOMIC_SWAP);
279 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
280 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
281 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
282 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
283 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
284 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
285 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
286 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
287 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
288 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
289
Christian Konigeecebd02013-03-26 14:04:02 +0000290 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000291}
292
Tom Stellard0125f2a2013-06-25 02:39:35 +0000293//===----------------------------------------------------------------------===//
294// TargetLowering queries
295//===----------------------------------------------------------------------===//
296
Matt Arsenaulte306a322014-10-21 16:25:08 +0000297bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
298 EVT) const {
299 // SI has some legal vector types, but no legal vector operations. Say no
300 // shuffles are legal in order to prefer scalarizing some vector operations.
301 return false;
302}
303
Tom Stellard70580f82015-07-20 14:28:41 +0000304bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
305 // Flat instructions do not have offsets, and only have the register
306 // address.
307 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
308}
309
Matt Arsenault711b3902015-08-07 20:18:34 +0000310bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
311 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
312 // additionally can do r + r + i with addr64. 32-bit has more addressing
313 // mode options. Depending on the resource constant, it can also do
314 // (i64 r0) + (i32 r1) * (i14 i).
315 //
316 // Private arrays end up using a scratch buffer most of the time, so also
317 // assume those use MUBUF instructions. Scratch loads / stores are currently
318 // implemented as mubuf instructions with offen bit set, so slightly
319 // different than the normal addr64.
320 if (!isUInt<12>(AM.BaseOffs))
321 return false;
322
323 // FIXME: Since we can split immediate into soffset and immediate offset,
324 // would it make sense to allow any immediate?
325
326 switch (AM.Scale) {
327 case 0: // r + i or just i, depending on HasBaseReg.
328 return true;
329 case 1:
330 return true; // We have r + r or r + i.
331 case 2:
332 if (AM.HasBaseReg) {
333 // Reject 2 * r + r.
334 return false;
335 }
336
337 // Allow 2 * r as r + r
338 // Or 2 * r + i is allowed as r + r + i.
339 return true;
340 default: // Don't allow n * r
341 return false;
342 }
343}
344
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000345bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
346 const AddrMode &AM, Type *Ty,
347 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000348 // No global is ever allowed as a base.
349 if (AM.BaseGV)
350 return false;
351
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000352 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000353 case AMDGPUAS::GLOBAL_ADDRESS: {
Tom Stellard70580f82015-07-20 14:28:41 +0000354 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
355 // Assume the we will use FLAT for all global memory accesses
356 // on VI.
357 // FIXME: This assumption is currently wrong. On VI we still use
358 // MUBUF instructions for the r + i addressing mode. As currently
359 // implemented, the MUBUF instructions only work on buffer < 4GB.
360 // It may be possible to support > 4GB buffers with MUBUF instructions,
361 // by setting the stride value in the resource descriptor which would
362 // increase the size limit to (stride * 4GB). However, this is risky,
363 // because it has never been validated.
364 return isLegalFlatAddressingMode(AM);
365 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000366
Matt Arsenault711b3902015-08-07 20:18:34 +0000367 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000368 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000369 case AMDGPUAS::CONSTANT_ADDRESS: {
370 // If the offset isn't a multiple of 4, it probably isn't going to be
371 // correctly aligned.
372 if (AM.BaseOffs % 4 != 0)
373 return isLegalMUBUFAddressingMode(AM);
374
375 // There are no SMRD extloads, so if we have to do a small type access we
376 // will use a MUBUF load.
377 // FIXME?: We also need to do this if unaligned, but we don't know the
378 // alignment here.
379 if (DL.getTypeStoreSize(Ty) < 4)
380 return isLegalMUBUFAddressingMode(AM);
381
382 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
383 // SMRD instructions have an 8-bit, dword offset on SI.
384 if (!isUInt<8>(AM.BaseOffs / 4))
385 return false;
386 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) {
387 // On CI+, this can also be a 32-bit literal constant offset. If it fits
388 // in 8-bits, it can use a smaller encoding.
389 if (!isUInt<32>(AM.BaseOffs / 4))
390 return false;
391 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) {
392 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
393 if (!isUInt<20>(AM.BaseOffs))
394 return false;
395 } else
396 llvm_unreachable("unhandled generation");
397
398 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
399 return true;
400
401 if (AM.Scale == 1 && AM.HasBaseReg)
402 return true;
403
404 return false;
405 }
406
407 case AMDGPUAS::PRIVATE_ADDRESS:
408 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
409 return isLegalMUBUFAddressingMode(AM);
410
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000411 case AMDGPUAS::LOCAL_ADDRESS:
412 case AMDGPUAS::REGION_ADDRESS: {
413 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
414 // field.
415 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
416 // an 8-bit dword offset but we don't know the alignment here.
417 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000418 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000419
420 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
421 return true;
422
423 if (AM.Scale == 1 && AM.HasBaseReg)
424 return true;
425
Matt Arsenault5015a892014-08-15 17:17:07 +0000426 return false;
427 }
Tom Stellard70580f82015-07-20 14:28:41 +0000428 case AMDGPUAS::FLAT_ADDRESS:
429 return isLegalFlatAddressingMode(AM);
430
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000431 default:
432 llvm_unreachable("unhandled address space");
433 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000434}
435
Matt Arsenaulte6986632015-01-14 01:35:22 +0000436bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000437 unsigned AddrSpace,
438 unsigned Align,
439 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000440 if (IsFast)
441 *IsFast = false;
442
Matt Arsenault1018c892014-04-24 17:08:26 +0000443 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
444 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000445 if (!VT.isSimple() || VT == MVT::Other)
446 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000447
Tom Stellardc6b299c2015-02-02 18:02:28 +0000448 // TODO - CI+ supports unaligned memory accesses, but this requires driver
449 // support.
Matt Arsenault1018c892014-04-24 17:08:26 +0000450
Matt Arsenault1018c892014-04-24 17:08:26 +0000451 // XXX - The only mention I see of this in the ISA manual is for LDS direct
452 // reads the "byte address and must be dword aligned". Is it also true for the
453 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000454 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
455 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
456 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
457 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000458 bool AlignedBy4 = (Align % 4 == 0);
459 if (IsFast)
460 *IsFast = AlignedBy4;
461 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000462 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000463
Tom Stellard33e64c62015-02-04 20:49:52 +0000464 // Smaller than dword value must be aligned.
465 // FIXME: This should be allowed on CI+
466 if (VT.bitsLT(MVT::i32))
467 return false;
468
Matt Arsenault1018c892014-04-24 17:08:26 +0000469 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
470 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000471 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000472 if (IsFast)
473 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000474
475 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000476}
477
Matt Arsenault46645fa2014-07-28 17:49:26 +0000478EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
479 unsigned SrcAlign, bool IsMemset,
480 bool ZeroMemset,
481 bool MemcpyStrSrc,
482 MachineFunction &MF) const {
483 // FIXME: Should account for address space here.
484
485 // The default fallback uses the private pointer size as a guess for a type to
486 // use. Make sure we switch these to 64-bit accesses.
487
488 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
489 return MVT::v4i32;
490
491 if (Size >= 8 && DstAlign >= 4)
492 return MVT::v2i32;
493
494 // Use the default.
495 return MVT::Other;
496}
497
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000498static bool isFlatGlobalAddrSpace(unsigned AS) {
499 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
500 AS == AMDGPUAS::FLAT_ADDRESS ||
501 AS == AMDGPUAS::CONSTANT_ADDRESS;
502}
503
504bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
505 unsigned DestAS) const {
506 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
507}
508
Tom Stellarda6f24c62015-12-15 20:55:55 +0000509
510bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
511 const MemSDNode *MemNode = cast<MemSDNode>(N);
512 const Value *Ptr = MemNode->getMemOperand()->getValue();
513
514 // UndefValue means this is a load of a kernel input. These are uniform.
515 // Sometimes LDS instructions have constant pointers
516 if (isa<UndefValue>(Ptr) || isa<Argument>(Ptr) || isa<Constant>(Ptr) ||
517 isa<GlobalValue>(Ptr))
518 return true;
519
520 const Instruction *I = dyn_cast_or_null<Instruction>(Ptr);
521 return I && I->getMetadata("amdgpu.uniform");
522}
523
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000524TargetLoweringBase::LegalizeTypeAction
525SITargetLowering::getPreferredVectorAction(EVT VT) const {
526 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
527 return TypeSplitVector;
528
529 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000530}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000531
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000532bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
533 Type *Ty) const {
Eric Christopher7792e322015-01-30 23:24:40 +0000534 const SIInstrInfo *TII =
535 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000536 return TII->isInlineConstant(Imm);
537}
538
Tom Stellard2e045bb2016-01-20 00:13:22 +0000539bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
540
541 // SimplifySetCC uses this function to determine whether or not it should
542 // create setcc with i1 operands. We don't have instructions for i1 setcc.
543 if (VT == MVT::i1 && Op == ISD::SETCC)
544 return false;
545
546 return TargetLowering::isTypeDesirableForOp(Op, VT);
547}
548
Tom Stellardaf775432013-10-23 00:44:32 +0000549SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000550 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000551 unsigned Offset, bool Signed) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000552 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000553 MachineFunction &MF = DAG.getMachineFunction();
554 const SIRegisterInfo *TRI =
555 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
Matt Arsenaultac234b62015-11-30 21:15:57 +0000556 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000557
Matt Arsenault86033ca2014-07-28 17:31:39 +0000558 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
559
560 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000561 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000562 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000563 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
564 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
565 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
566 DAG.getConstant(Offset, SL, PtrVT));
Mehdi Amini44ede332015-07-09 02:09:04 +0000567 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000568 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
569
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000570 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000571
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000572 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000573 if (MemVT.isFloatingPoint())
574 ExtTy = ISD::EXTLOAD;
575
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000576 return DAG.getLoad(ISD::UNINDEXED, ExtTy,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000577 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
578 false, // isVolatile
579 true, // isNonTemporal
580 true, // isInvariant
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000581 Align); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000582}
583
Christian Konig2c8f6d52013-03-07 09:03:52 +0000584SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000585 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
586 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
587 SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardec2e43c2014-09-22 15:35:29 +0000588 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000589 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000590
591 MachineFunction &MF = DAG.getMachineFunction();
592 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000593 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000594 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000595
Matt Arsenaultd48da142015-11-02 23:23:02 +0000596 if (Subtarget->isAmdHsaOS() && Info->getShaderType() != ShaderType::COMPUTE) {
597 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000598 DiagnosticInfoUnsupported NoGraphicsHSA(
599 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000600 DAG.getContext()->diagnose(NoGraphicsHSA);
601 return SDValue();
602 }
603
Tom Stellard0fbf8992015-10-06 21:16:34 +0000604 // FIXME: We currently assume all calling conventions are kernels.
Christian Konig2c8f6d52013-03-07 09:03:52 +0000605
606 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000607 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000608
609 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000610 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000611
612 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000613 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000614 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000615
Marek Olsakfccabaf2016-01-13 11:45:36 +0000616 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000617 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000618 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000619 ++PSInputNum;
620 continue;
621 }
622
Marek Olsakfccabaf2016-01-13 11:45:36 +0000623 Info->markPSInputAllocated(PSInputNum);
624 if (Arg.Used)
625 Info->PSInputEna |= 1 << PSInputNum;
626
627 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000628 }
629
630 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000631 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000632 ISD::InputArg NewArg = Arg;
633 NewArg.Flags.setSplit();
634 NewArg.VT = Arg.VT.getVectorElementType();
635
636 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
637 // three or five element vertex only needs three or five registers,
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000638 // NOT four or eight.
Andrew Trick05938a52015-02-16 18:10:47 +0000639 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000640 unsigned NumElements = ParamType->getVectorNumElements();
641
642 for (unsigned j = 0; j != NumElements; ++j) {
643 Splits.push_back(NewArg);
644 NewArg.PartOffset += NewArg.VT.getStoreSize();
645 }
646
Matt Arsenault762af962014-07-13 03:06:39 +0000647 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000648 Splits.push_back(Arg);
649 }
650 }
651
652 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000653 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
654 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000655
Christian Konig99ee0f42013-03-07 09:04:14 +0000656 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000657 //
658 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
659 // PSInputAddr, the user wants to enable some bits after the compilation
660 // based on run-time states. Since we can't know what the final PSInputEna
661 // will look like, so we shouldn't do anything here and the user should take
662 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000663 //
664 // Otherwise, the following restrictions apply:
665 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
666 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
667 // enabled too.
Matt Arsenault762af962014-07-13 03:06:39 +0000668 if (Info->getShaderType() == ShaderType::PIXEL &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000669 ((Info->getPSInputAddr() & 0x7F) == 0 ||
670 ((Info->getPSInputAddr() & 0xF) == 0 &&
671 Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000672 CCInfo.AllocateReg(AMDGPU::VGPR0);
673 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000674 Info->markPSInputAllocated(0);
675 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000676 }
677
Matt Arsenault762af962014-07-13 03:06:39 +0000678 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000679 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
680 Splits);
681 }
682
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000683 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
684 if (Info->hasPrivateSegmentBuffer()) {
685 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
686 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
687 CCInfo.AllocateReg(PrivateSegmentBufferReg);
688 }
689
690 if (Info->hasDispatchPtr()) {
691 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
692 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
693 CCInfo.AllocateReg(DispatchPtrReg);
694 }
695
696 if (Info->hasKernargSegmentPtr()) {
697 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
698 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
699 CCInfo.AllocateReg(InputPtrReg);
700 }
701
Matt Arsenault296b8492016-02-12 06:31:30 +0000702 if (Info->hasFlatScratchInit()) {
703 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
704 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass);
705 CCInfo.AllocateReg(FlatScratchInitReg);
706 }
707
Christian Konig2c8f6d52013-03-07 09:03:52 +0000708 AnalyzeFormalArguments(CCInfo, Splits);
709
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000710 SmallVector<SDValue, 16> Chains;
711
Christian Konig2c8f6d52013-03-07 09:03:52 +0000712 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
713
Christian Konigb7be72d2013-05-17 09:46:48 +0000714 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000715 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000716 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000717 continue;
718 }
719
Christian Konig2c8f6d52013-03-07 09:03:52 +0000720 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000721 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000722
723 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000724 VT = Ins[i].VT;
725 EVT MemVT = Splits[i].VT;
Tom Stellardb5798b02015-06-26 21:15:03 +0000726 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
727 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000728 // The first 36 bytes of the input buffer contains information about
729 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000730 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000731 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000732 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000733
Craig Toppere3dcce92015-08-01 22:20:21 +0000734 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000735 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000736 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
737 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
738 // On SI local pointers are just offsets into LDS, so they are always
739 // less than 16-bits. On CI and newer they could potentially be
740 // real pointers, so we can't guarantee their size.
741 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
742 DAG.getValueType(MVT::i16));
743 }
744
Tom Stellarded882c22013-06-03 17:40:11 +0000745 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000746 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000747 continue;
748 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000749 assert(VA.isRegLoc() && "Parameter must be in a register!");
750
751 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000752
753 if (VT == MVT::i64) {
754 // For now assume it is a pointer
755 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
756 &AMDGPU::SReg_64RegClass);
757 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000758 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
759 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000760 continue;
761 }
762
763 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
764
765 Reg = MF.addLiveIn(Reg, RC);
766 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
767
Christian Konig2c8f6d52013-03-07 09:03:52 +0000768 if (Arg.VT.isVector()) {
769
770 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000771 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000772 unsigned NumElements = ParamType->getVectorNumElements();
773
774 SmallVector<SDValue, 4> Regs;
775 Regs.push_back(Val);
776 for (unsigned j = 1; j != NumElements; ++j) {
777 Reg = ArgLocs[ArgIdx++].getLocReg();
778 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000779
780 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
781 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000782 }
783
784 // Fill up the missing vector elements
785 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000786 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000787
Craig Topper48d114b2014-04-26 18:35:24 +0000788 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000789 continue;
790 }
791
792 InVals.push_back(Val);
793 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000794
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000795 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
796 // these from the dispatch pointer.
797
798 // Start adding system SGPRs.
799 if (Info->hasWorkGroupIDX()) {
800 unsigned Reg = Info->addWorkGroupIDX();
801 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
802 CCInfo.AllocateReg(Reg);
803 } else
804 llvm_unreachable("work group id x is always enabled");
805
806 if (Info->hasWorkGroupIDY()) {
807 unsigned Reg = Info->addWorkGroupIDY();
808 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
809 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000810 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000811
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000812 if (Info->hasWorkGroupIDZ()) {
813 unsigned Reg = Info->addWorkGroupIDZ();
814 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
815 CCInfo.AllocateReg(Reg);
816 }
817
818 if (Info->hasWorkGroupInfo()) {
819 unsigned Reg = Info->addWorkGroupInfo();
820 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
821 CCInfo.AllocateReg(Reg);
822 }
823
824 if (Info->hasPrivateSegmentWaveByteOffset()) {
825 // Scratch wave offset passed in system SGPR.
826 unsigned PrivateSegmentWaveByteOffsetReg
827 = Info->addPrivateSegmentWaveByteOffset();
828
829 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
830 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
831 }
832
833 // Now that we've figured out where the scratch register inputs are, see if
834 // should reserve the arguments and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000835 bool HasStackObjects = MF.getFrameInfo()->hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +0000836 // Record that we know we have non-spill stack objects so we don't need to
837 // check all stack objects later.
838 if (HasStackObjects)
839 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000840
841 if (ST.isAmdHsaOS()) {
842 // TODO: Assume we will spill without optimizations.
843 if (HasStackObjects) {
844 // If we have stack objects, we unquestionably need the private buffer
845 // resource. For the HSA ABI, this will be the first 4 user SGPR
846 // inputs. We can reserve those and use them directly.
847
848 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
849 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
850 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
851
852 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
853 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
854 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
855 } else {
856 unsigned ReservedBufferReg
857 = TRI->reservedPrivateSegmentBufferReg(MF);
858 unsigned ReservedOffsetReg
859 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
860
861 // We tentatively reserve the last registers (skipping the last two
862 // which may contain VCC). After register allocation, we'll replace
863 // these with the ones immediately after those which were really
864 // allocated. In the prologue copies will be inserted from the argument
865 // to these reserved registers.
866 Info->setScratchRSrcReg(ReservedBufferReg);
867 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
868 }
869 } else {
870 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
871
872 // Without HSA, relocations are used for the scratch pointer and the
873 // buffer resource setup is always inserted in the prologue. Scratch wave
874 // offset is still in an input SGPR.
875 Info->setScratchRSrcReg(ReservedBufferReg);
876
877 if (HasStackObjects) {
878 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
879 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
880 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
881 } else {
882 unsigned ReservedOffsetReg
883 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
884 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
885 }
886 }
887
888 if (Info->hasWorkItemIDX()) {
889 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
890 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
891 CCInfo.AllocateReg(Reg);
892 } else
893 llvm_unreachable("workitem id x should always be enabled");
894
895 if (Info->hasWorkItemIDY()) {
896 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
897 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
898 CCInfo.AllocateReg(Reg);
899 }
900
901 if (Info->hasWorkItemIDZ()) {
902 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
903 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
904 CCInfo.AllocateReg(Reg);
905 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000906
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000907 if (Chains.empty())
908 return Chain;
909
910 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000911}
912
Marek Olsak8a0f3352016-01-13 17:23:04 +0000913SDValue SITargetLowering::LowerReturn(SDValue Chain,
914 CallingConv::ID CallConv,
915 bool isVarArg,
916 const SmallVectorImpl<ISD::OutputArg> &Outs,
917 const SmallVectorImpl<SDValue> &OutVals,
918 SDLoc DL, SelectionDAG &DAG) const {
919 MachineFunction &MF = DAG.getMachineFunction();
920 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
921
922 if (Info->getShaderType() == ShaderType::COMPUTE)
923 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
924 OutVals, DL, DAG);
925
Marek Olsak8e9cc632016-01-13 17:23:09 +0000926 Info->setIfReturnsVoid(Outs.size() == 0);
927
Marek Olsak8a0f3352016-01-13 17:23:04 +0000928 SmallVector<ISD::OutputArg, 48> Splits;
929 SmallVector<SDValue, 48> SplitVals;
930
931 // Split vectors into their elements.
932 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
933 const ISD::OutputArg &Out = Outs[i];
934
935 if (Out.VT.isVector()) {
936 MVT VT = Out.VT.getVectorElementType();
937 ISD::OutputArg NewOut = Out;
938 NewOut.Flags.setSplit();
939 NewOut.VT = VT;
940
941 // We want the original number of vector elements here, e.g.
942 // three or five, not four or eight.
943 unsigned NumElements = Out.ArgVT.getVectorNumElements();
944
945 for (unsigned j = 0; j != NumElements; ++j) {
946 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
947 DAG.getConstant(j, DL, MVT::i32));
948 SplitVals.push_back(Elem);
949 Splits.push_back(NewOut);
950 NewOut.PartOffset += NewOut.VT.getStoreSize();
951 }
952 } else {
953 SplitVals.push_back(OutVals[i]);
954 Splits.push_back(Out);
955 }
956 }
957
958 // CCValAssign - represent the assignment of the return value to a location.
959 SmallVector<CCValAssign, 48> RVLocs;
960
961 // CCState - Info about the registers and stack slots.
962 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
963 *DAG.getContext());
964
965 // Analyze outgoing return values.
966 AnalyzeReturn(CCInfo, Splits);
967
968 SDValue Flag;
969 SmallVector<SDValue, 48> RetOps;
970 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
971
972 // Copy the result values into the output registers.
973 for (unsigned i = 0, realRVLocIdx = 0;
974 i != RVLocs.size();
975 ++i, ++realRVLocIdx) {
976 CCValAssign &VA = RVLocs[i];
977 assert(VA.isRegLoc() && "Can only return in registers!");
978
979 SDValue Arg = SplitVals[realRVLocIdx];
980
981 // Copied from other backends.
982 switch (VA.getLocInfo()) {
983 default: llvm_unreachable("Unknown loc info!");
984 case CCValAssign::Full:
985 break;
986 case CCValAssign::BCvt:
987 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
988 break;
989 }
990
991 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
992 Flag = Chain.getValue(1);
993 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
994 }
995
996 // Update chain and glue.
997 RetOps[0] = Chain;
998 if (Flag.getNode())
999 RetOps.push_back(Flag);
1000
1001 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, RetOps);
1002}
1003
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001004unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1005 SelectionDAG &DAG) const {
1006 unsigned Reg = StringSwitch<unsigned>(RegName)
1007 .Case("m0", AMDGPU::M0)
1008 .Case("exec", AMDGPU::EXEC)
1009 .Case("exec_lo", AMDGPU::EXEC_LO)
1010 .Case("exec_hi", AMDGPU::EXEC_HI)
1011 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1012 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1013 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1014 .Default(AMDGPU::NoRegister);
1015
1016 if (Reg == AMDGPU::NoRegister) {
1017 report_fatal_error(Twine("invalid register name \""
1018 + StringRef(RegName) + "\"."));
1019
1020 }
1021
1022 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
1023 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1024 report_fatal_error(Twine("invalid register \""
1025 + StringRef(RegName) + "\" for subtarget."));
1026 }
1027
1028 switch (Reg) {
1029 case AMDGPU::M0:
1030 case AMDGPU::EXEC_LO:
1031 case AMDGPU::EXEC_HI:
1032 case AMDGPU::FLAT_SCR_LO:
1033 case AMDGPU::FLAT_SCR_HI:
1034 if (VT.getSizeInBits() == 32)
1035 return Reg;
1036 break;
1037 case AMDGPU::EXEC:
1038 case AMDGPU::FLAT_SCR:
1039 if (VT.getSizeInBits() == 64)
1040 return Reg;
1041 break;
1042 default:
1043 llvm_unreachable("missing register type checking");
1044 }
1045
1046 report_fatal_error(Twine("invalid type for register \""
1047 + StringRef(RegName) + "\"."));
1048}
1049
Tom Stellard75aadc22012-12-11 21:25:42 +00001050MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
1051 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +00001052
Tom Stellard75aadc22012-12-11 21:25:42 +00001053 switch (MI->getOpcode()) {
1054 default:
1055 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Matt Arsenault20711b72015-02-20 22:10:45 +00001056 case AMDGPU::BRANCH:
1057 return BB;
Tom Stellard75aadc22012-12-11 21:25:42 +00001058 }
1059 return BB;
1060}
1061
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001062bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1063 // This currently forces unfolding various combinations of fsub into fma with
1064 // free fneg'd operands. As long as we have fast FMA (controlled by
1065 // isFMAFasterThanFMulAndFAdd), we should perform these.
1066
1067 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1068 // most of these combines appear to be cycle neutral but save on instruction
1069 // count / code size.
1070 return true;
1071}
1072
Mehdi Amini44ede332015-07-09 02:09:04 +00001073EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1074 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001075 if (!VT.isVector()) {
1076 return MVT::i1;
1077 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001078 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001079}
1080
Mehdi Aminieaabc512015-07-09 15:12:23 +00001081MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001082 return MVT::i32;
1083}
1084
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001085// Answering this is somewhat tricky and depends on the specific device which
1086// have different rates for fma or all f64 operations.
1087//
1088// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1089// regardless of which device (although the number of cycles differs between
1090// devices), so it is always profitable for f64.
1091//
1092// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1093// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1094// which we can always do even without fused FP ops since it returns the same
1095// result as the separate operations and since it is always full
1096// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1097// however does not support denormals, so we do report fma as faster if we have
1098// a fast fma device and require denormals.
1099//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001100bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1101 VT = VT.getScalarType();
1102
1103 if (!VT.isSimple())
1104 return false;
1105
1106 switch (VT.getSimpleVT().SimpleTy) {
1107 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001108 // This is as fast on some subtargets. However, we always have full rate f32
1109 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001110 // which we should prefer over fma. We can't use this if we want to support
1111 // denormals, so only report this in these cases.
1112 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001113 case MVT::f64:
1114 return true;
1115 default:
1116 break;
1117 }
1118
1119 return false;
1120}
1121
Tom Stellard75aadc22012-12-11 21:25:42 +00001122//===----------------------------------------------------------------------===//
1123// Custom DAG Lowering Operations
1124//===----------------------------------------------------------------------===//
1125
1126SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1127 switch (Op.getOpcode()) {
1128 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001129 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001130 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001131 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001132 SDValue Result = LowerLOAD(Op, DAG);
1133 assert((!Result.getNode() ||
1134 Result.getNode()->getNumValues() == 2) &&
1135 "Load should return a value and a chain");
1136 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001137 }
Tom Stellardaf775432013-10-23 00:44:32 +00001138
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001139 case ISD::FSIN:
1140 case ISD::FCOS:
1141 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001142 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001143 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001144 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001145 case ISD::GlobalAddress: {
1146 MachineFunction &MF = DAG.getMachineFunction();
1147 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1148 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001149 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001150 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
1151 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001152 }
1153 return SDValue();
1154}
1155
Tom Stellardf8794352012-12-19 22:10:31 +00001156/// \brief Helper function for LowerBRCOND
1157static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001158
Tom Stellardf8794352012-12-19 22:10:31 +00001159 SDNode *Parent = Value.getNode();
1160 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1161 I != E; ++I) {
1162
1163 if (I.getUse().get() != Value)
1164 continue;
1165
1166 if (I->getOpcode() == Opcode)
1167 return *I;
1168 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001169 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001170}
1171
Tom Stellardb02094e2014-07-21 15:45:01 +00001172SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
1173
Tom Stellardc98ee202015-07-16 19:40:07 +00001174 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +00001175 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
1176 unsigned FrameIndex = FINode->getIndex();
1177
Tom Stellardc98ee202015-07-16 19:40:07 +00001178 // A FrameIndex node represents a 32-bit offset into scratch memory. If
1179 // the high bit of a frame index offset were to be set, this would mean
1180 // that it represented an offset of ~2GB * 64 = ~128GB from the start of the
1181 // scratch buffer, with 64 being the number of threads per wave.
1182 //
1183 // If we know the machine uses less than 128GB of scratch, then we can
1184 // amrk the high bit of the FrameIndex node as known zero,
1185 // which is important, because it means in most situations we can
1186 // prove that values derived from FrameIndex nodes are non-negative.
1187 // This enables us to take advantage of more addressing modes when
1188 // accessing scratch buffers, since for scratch reads/writes, the register
1189 // offset must always be positive.
1190
1191 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
1192 if (Subtarget->enableHugeScratchBuffer())
1193 return TFI;
1194
1195 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
1196 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), 31)));
Tom Stellardb02094e2014-07-21 15:45:01 +00001197}
1198
Tom Stellardbc4497b2016-02-12 23:45:29 +00001199bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault16f48d72016-02-13 00:36:10 +00001200 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN)
Tom Stellardbc4497b2016-02-12 23:45:29 +00001201 return false;
1202
1203 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1204 default: return false;
1205 case AMDGPUIntrinsic::amdgcn_if:
1206 case AMDGPUIntrinsic::amdgcn_else:
1207 case AMDGPUIntrinsic::amdgcn_break:
1208 case AMDGPUIntrinsic::amdgcn_if_break:
1209 case AMDGPUIntrinsic::amdgcn_else_break:
1210 case AMDGPUIntrinsic::amdgcn_loop:
1211 case AMDGPUIntrinsic::amdgcn_end_cf:
1212 return true;
1213 }
1214}
1215
Tom Stellardf8794352012-12-19 22:10:31 +00001216/// This transforms the control flow intrinsics to get the branch destination as
1217/// last parameter, also switches branch target with BR if the need arise
1218SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1219 SelectionDAG &DAG) const {
1220
Andrew Trickef9de2a2013-05-25 02:42:55 +00001221 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001222
1223 SDNode *Intr = BRCOND.getOperand(1).getNode();
1224 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001225 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001226 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001227
1228 if (Intr->getOpcode() == ISD::SETCC) {
1229 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001230 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001231 Intr = SetCC->getOperand(0).getNode();
1232
1233 } else {
1234 // Get the target from BR if we don't negate the condition
1235 BR = findUser(BRCOND, ISD::BR);
1236 Target = BR->getOperand(1);
1237 }
1238
Tom Stellardbc4497b2016-02-12 23:45:29 +00001239 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN) {
1240 // This is a uniform branch so we don't need to legalize.
1241 return BRCOND;
1242 }
1243
1244 assert(!SetCC ||
1245 (SetCC->getConstantOperandVal(1) == 1 &&
1246 isCFIntrinsic(Intr) &&
1247 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1248 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001249
1250 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001251 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001252
1253 // operands of the new intrinsic call
1254 SmallVector<SDValue, 4> Ops;
1255 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001256 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001257 Ops.push_back(Target);
1258
1259 // build the new intrinsic call
1260 SDNode *Result = DAG.getNode(
1261 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001262 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001263
1264 if (BR) {
1265 // Give the branch instruction our target
1266 SDValue Ops[] = {
1267 BR->getOperand(0),
1268 BRCOND.getOperand(2)
1269 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001270 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1271 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1272 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001273 }
1274
1275 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1276
1277 // Copy the intrinsic results to registers
1278 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1279 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1280 if (!CopyToReg)
1281 continue;
1282
1283 Chain = DAG.getCopyToReg(
1284 Chain, DL,
1285 CopyToReg->getOperand(1),
1286 SDValue(Result, i - 1),
1287 SDValue());
1288
1289 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1290 }
1291
1292 // Remove the old intrinsic from the chain
1293 DAG.ReplaceAllUsesOfValueWith(
1294 SDValue(Intr, Intr->getNumValues() - 1),
1295 Intr->getOperand(0));
1296
1297 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001298}
1299
Tom Stellard067c8152014-07-21 14:01:14 +00001300SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1301 SDValue Op,
1302 SelectionDAG &DAG) const {
1303 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1304
1305 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
1306 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1307
1308 SDLoc DL(GSD);
1309 const GlobalValue *GV = GSD->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001310 MVT PtrVT = getPointerTy(DAG.getDataLayout(), GSD->getAddressSpace());
Tom Stellard067c8152014-07-21 14:01:14 +00001311
Tom Stellard067c8152014-07-21 14:01:14 +00001312 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
Tom Stellardc93fc112015-12-10 02:13:01 +00001313 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT, GA);
Tom Stellard067c8152014-07-21 14:01:14 +00001314}
1315
Tom Stellardfc92e772015-05-12 14:18:14 +00001316SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL,
1317 SDValue V) const {
1318 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1319 // so we will end up with redundant moves to m0.
1320 //
1321 // We can't use S_MOV_B32, because there is no way to specify m0 as the
1322 // destination register.
1323 //
1324 // We have to use them both. Machine cse will combine all the S_MOV_B32
1325 // instructions and the register coalescer eliminate the extra copies.
1326 SDNode *M0 = DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, V.getValueType(), V);
1327 return DAG.getCopyToReg(Chain, DL, DAG.getRegister(AMDGPU::M0, MVT::i32),
1328 SDValue(M0, 0), SDValue()); // Glue
1329 // A Null SDValue creates
1330 // a glue result.
1331}
1332
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001333SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1334 SDValue Op,
1335 MVT VT,
1336 unsigned Offset) const {
1337 SDLoc SL(Op);
1338 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1339 DAG.getEntryNode(), Offset, false);
1340 // The local size values will have the hi 16-bits as zero.
1341 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1342 DAG.getValueType(VT));
1343}
1344
Matt Arsenaulte0132462016-01-30 05:19:45 +00001345static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, EVT VT) {
1346 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
1347 "non-hsa intrinsic with hsa target");
1348 DAG.getContext()->diagnose(BadIntrin);
1349 return DAG.getUNDEF(VT);
1350}
1351
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001352SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1353 SelectionDAG &DAG) const {
1354 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001355 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardec2e43c2014-09-22 15:35:29 +00001356 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +00001357 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001358
1359 EVT VT = Op.getValueType();
1360 SDLoc DL(Op);
1361 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1362
Sanjay Patela2607012015-09-16 16:31:21 +00001363 // TODO: Should this propagate fast-math-flags?
1364
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001365 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001366 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault800fecf2016-01-11 21:18:33 +00001367 if (!Subtarget->isAmdHsaOS()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00001368 DiagnosticInfoUnsupported BadIntrin(
1369 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
1370 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00001371 DAG.getContext()->diagnose(BadIntrin);
1372 return DAG.getUNDEF(VT);
1373 }
1374
Tom Stellard48f29f22015-11-26 00:43:29 +00001375 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
1376 TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_PTR), VT);
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001377 case Intrinsic::amdgcn_rcp:
1378 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
1379 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001380 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001381 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
1382 case Intrinsic::amdgcn_rsq_clamped:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001383 case AMDGPUIntrinsic::AMDGPU_rsq_clamped: { // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001384 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
1385 return DAG.getNode(AMDGPUISD::RSQ_CLAMPED, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00001386
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001387 Type *Type = VT.getTypeForEVT(*DAG.getContext());
1388 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
1389 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
1390
1391 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
1392 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
1393 DAG.getConstantFP(Max, DL, VT));
1394 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
1395 DAG.getConstantFP(Min, DL, VT));
1396 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001397 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001398 if (Subtarget->isAmdHsaOS())
1399 return emitNonHSAIntrinsicError(DAG, VT);
1400
Tom Stellardec2e43c2014-09-22 15:35:29 +00001401 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1402 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001403 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001404 if (Subtarget->isAmdHsaOS())
1405 return emitNonHSAIntrinsicError(DAG, VT);
1406
Tom Stellardec2e43c2014-09-22 15:35:29 +00001407 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1408 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001409 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001410 if (Subtarget->isAmdHsaOS())
1411 return emitNonHSAIntrinsicError(DAG, VT);
1412
Tom Stellardec2e43c2014-09-22 15:35:29 +00001413 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1414 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001415 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001416 if (Subtarget->isAmdHsaOS())
1417 return emitNonHSAIntrinsicError(DAG, VT);
1418
Tom Stellardec2e43c2014-09-22 15:35:29 +00001419 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1420 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001421 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001422 if (Subtarget->isAmdHsaOS())
1423 return emitNonHSAIntrinsicError(DAG, VT);
1424
Tom Stellardec2e43c2014-09-22 15:35:29 +00001425 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1426 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001427 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001428 if (Subtarget->isAmdHsaOS())
1429 return emitNonHSAIntrinsicError(DAG, VT);
1430
Tom Stellardec2e43c2014-09-22 15:35:29 +00001431 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1432 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001433 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001434 if (Subtarget->isAmdHsaOS())
1435 return emitNonHSAIntrinsicError(DAG, VT);
1436
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001437 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1438 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001439 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001440 if (Subtarget->isAmdHsaOS())
1441 return emitNonHSAIntrinsicError(DAG, VT);
1442
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001443 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1444 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001445 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001446 if (Subtarget->isAmdHsaOS())
1447 return emitNonHSAIntrinsicError(DAG, VT);
1448
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001449 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1450 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenaultbef34e22016-01-22 21:30:34 +00001451 case Intrinsic::amdgcn_read_workdim:
1452 case AMDGPUIntrinsic::AMDGPU_read_workdim: // Legacy name.
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001453 // Really only 2 bits.
1454 return lowerImplicitZextParam(DAG, Op, MVT::i8,
1455 getImplicitParameterOffset(MFI, GRID_DIM));
Matt Arsenault43976df2016-01-30 04:25:19 +00001456 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001457 case Intrinsic::r600_read_tgid_x:
1458 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001459 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001460 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001461 case Intrinsic::r600_read_tgid_y:
1462 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001463 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001464 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001465 case Intrinsic::r600_read_tgid_z:
1466 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001467 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001468 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001469 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001470 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001471 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001472 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001473 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001474 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001475 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001476 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001477 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001478 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001479 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001480 case AMDGPUIntrinsic::SI_load_const: {
1481 SDValue Ops[] = {
1482 Op.getOperand(1),
1483 Op.getOperand(2)
1484 };
1485
1486 MachineMemOperand *MMO = MF.getMachineMemOperand(
1487 MachinePointerInfo(),
1488 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
1489 VT.getStoreSize(), 4);
1490 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
1491 Op->getVTList(), Ops, VT, MMO);
1492 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001493 case AMDGPUIntrinsic::SI_vs_load_input:
1494 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1495 Op.getOperand(1),
1496 Op.getOperand(2),
1497 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00001498
Tom Stellard2a9d9472015-05-12 15:00:46 +00001499 case AMDGPUIntrinsic::SI_fs_constant: {
1500 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1501 SDValue Glue = M0.getValue(1);
1502 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
1503 DAG.getConstant(2, DL, MVT::i32), // P0
1504 Op.getOperand(1), Op.getOperand(2), Glue);
1505 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00001506 case AMDGPUIntrinsic::SI_packf16:
1507 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
1508 return DAG.getUNDEF(MVT::i32);
1509 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00001510 case AMDGPUIntrinsic::SI_fs_interp: {
1511 SDValue IJ = Op.getOperand(4);
1512 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1513 DAG.getConstant(0, DL, MVT::i32));
1514 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1515 DAG.getConstant(1, DL, MVT::i32));
1516 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1517 SDValue Glue = M0.getValue(1);
1518 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
1519 DAG.getVTList(MVT::f32, MVT::Glue),
1520 I, Op.getOperand(1), Op.getOperand(2), Glue);
1521 Glue = SDValue(P1.getNode(), 1);
1522 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
1523 Op.getOperand(1), Op.getOperand(2), Glue);
1524 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00001525 case Intrinsic::amdgcn_interp_p1: {
1526 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
1527 SDValue Glue = M0.getValue(1);
1528 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
1529 Op.getOperand(2), Op.getOperand(3), Glue);
1530 }
1531 case Intrinsic::amdgcn_interp_p2: {
1532 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
1533 SDValue Glue = SDValue(M0.getNode(), 1);
1534 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
1535 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
1536 Glue);
1537 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001538 case Intrinsic::amdgcn_ldexp:
1539 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
1540 Op.getOperand(1), Op.getOperand(2));
1541 case Intrinsic::amdgcn_class:
1542 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
1543 Op.getOperand(1), Op.getOperand(2));
1544 case Intrinsic::amdgcn_div_fmas:
1545 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
1546 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
1547 Op.getOperand(4));
1548
1549 case Intrinsic::amdgcn_div_fixup:
1550 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
1551 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
1552
1553 case Intrinsic::amdgcn_trig_preop:
1554 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
1555 Op.getOperand(1), Op.getOperand(2));
1556 case Intrinsic::amdgcn_div_scale: {
1557 // 3rd parameter required to be a constant.
1558 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1559 if (!Param)
1560 return DAG.getUNDEF(VT);
1561
1562 // Translate to the operands expected by the machine instruction. The
1563 // first parameter must be the same as the first instruction.
1564 SDValue Numerator = Op.getOperand(1);
1565 SDValue Denominator = Op.getOperand(2);
1566
1567 // Note this order is opposite of the machine instruction's operations,
1568 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
1569 // intrinsic has the numerator as the first operand to match a normal
1570 // division operation.
1571
1572 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
1573
1574 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
1575 Denominator, Numerator);
1576 }
1577 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte0:
1578 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Op.getOperand(1));
1579 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte1:
1580 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE1, DL, VT, Op.getOperand(1));
1581 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte2:
1582 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE2, DL, VT, Op.getOperand(1));
1583 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte3:
1584 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE3, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001585 default:
1586 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1587 }
1588}
1589
1590SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1591 SelectionDAG &DAG) const {
1592 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00001593 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001594 SDValue Chain = Op.getOperand(0);
1595 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1596
1597 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00001598 case AMDGPUIntrinsic::SI_sendmsg: {
1599 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1600 SDValue Glue = Chain.getValue(1);
1601 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1602 Op.getOperand(2), Glue);
1603 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001604 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001605 SDValue Ops[] = {
1606 Chain,
1607 Op.getOperand(2),
1608 Op.getOperand(3),
1609 Op.getOperand(4),
1610 Op.getOperand(5),
1611 Op.getOperand(6),
1612 Op.getOperand(7),
1613 Op.getOperand(8),
1614 Op.getOperand(9),
1615 Op.getOperand(10),
1616 Op.getOperand(11),
1617 Op.getOperand(12),
1618 Op.getOperand(13),
1619 Op.getOperand(14)
1620 };
1621
1622 EVT VT = Op.getOperand(3).getValueType();
1623
1624 MachineMemOperand *MMO = MF.getMachineMemOperand(
1625 MachinePointerInfo(),
1626 MachineMemOperand::MOStore,
1627 VT.getStoreSize(), 4);
1628 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1629 Op->getVTList(), Ops, VT, MMO);
1630 }
1631 default:
1632 return SDValue();
1633 }
1634}
1635
Tom Stellard81d871d2013-11-13 23:36:50 +00001636SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1637 SDLoc DL(Op);
1638 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001639 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00001640 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00001641
Matt Arsenaulta1436412016-02-10 18:21:45 +00001642 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
1643 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected");
Matt Arsenault6dfda962016-02-10 18:21:39 +00001644 // FIXME: Copied from PPC
1645 // First, load into 32 bits, then truncate to 1 bit.
1646
1647 SDValue Chain = Load->getChain();
1648 SDValue BasePtr = Load->getBasePtr();
1649 MachineMemOperand *MMO = Load->getMemOperand();
1650
1651 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
1652 BasePtr, MVT::i8, MMO);
1653
1654 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00001655 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00001656 NewLD.getValue(1)
1657 };
1658
1659 return DAG.getMergeValues(Ops, DL);
1660 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001661
Matt Arsenaulta1436412016-02-10 18:21:45 +00001662 if (!MemVT.isVector())
1663 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001664
Matt Arsenaulta1436412016-02-10 18:21:45 +00001665 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1666 "Custom lowering for non-i32 vectors hasn't been implemented.");
1667 unsigned NumElements = MemVT.getVectorNumElements();
1668 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001669
Matt Arsenaulta1436412016-02-10 18:21:45 +00001670 switch (Load->getAddressSpace()) {
1671 case AMDGPUAS::CONSTANT_ADDRESS:
1672 if (isMemOpUniform(Load))
1673 return SDValue();
1674 // Non-uniform loads will be selected to MUBUF instructions, so they
1675 // have the same legalization requires ments as global and private
1676 // loads.
1677 //
1678 // Fall-through
1679 case AMDGPUAS::GLOBAL_ADDRESS:
1680 case AMDGPUAS::PRIVATE_ADDRESS:
1681 if (NumElements >= 8)
1682 return SplitVectorLoad(Op, DAG);
1683 // v4 loads are supported for private and global memory.
1684 return SDValue();
1685 case AMDGPUAS::LOCAL_ADDRESS:
1686 // If properly aligned, if we split we might be able to use ds_read_b64.
1687 return SplitVectorLoad(Op, DAG);
1688 default:
1689 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00001690 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001691}
1692
Tom Stellard0ec134f2014-02-04 17:18:40 +00001693SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1694 if (Op.getValueType() != MVT::i64)
1695 return SDValue();
1696
1697 SDLoc DL(Op);
1698 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001699
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001700 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1701 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001702
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001703 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1704 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1705
1706 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1707 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001708
1709 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1710
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001711 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1712 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001713
1714 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1715
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001716 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1717 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001718}
1719
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001720// Catch division cases where we can use shortcuts with rcp and rsq
1721// instructions.
1722SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001723 SDLoc SL(Op);
1724 SDValue LHS = Op.getOperand(0);
1725 SDValue RHS = Op.getOperand(1);
1726 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001727 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001728
1729 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001730 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1731 CLHS->isExactlyValue(1.0)) {
1732 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1733 // the CI documentation has a worst case error of 1 ulp.
1734 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1735 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001736
1737 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001738 //
1739 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1740 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001741 if (RHS.getOpcode() == ISD::FSQRT)
1742 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1743
1744 // 1.0 / x -> rcp(x)
1745 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1746 }
1747 }
1748
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001749 if (Unsafe) {
1750 // Turn into multiply by the reciprocal.
1751 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00001752 SDNodeFlags Flags;
1753 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001754 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00001755 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001756 }
1757
1758 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001759}
1760
1761SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001762 if (SDValue FastLowered = LowerFastFDIV(Op, DAG))
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001763 return FastLowered;
1764
1765 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1766 // selection error for now rather than do something incorrect.
1767 if (Subtarget->hasFP32Denormals())
1768 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001769
1770 SDLoc SL(Op);
1771 SDValue LHS = Op.getOperand(0);
1772 SDValue RHS = Op.getOperand(1);
1773
1774 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1775
1776 const APFloat K0Val(BitsToFloat(0x6f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001777 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001778
1779 const APFloat K1Val(BitsToFloat(0x2f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001780 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001781
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001782 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001783
Mehdi Amini44ede332015-07-09 02:09:04 +00001784 EVT SetCCVT =
1785 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001786
1787 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1788
1789 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1790
Sanjay Patela2607012015-09-16 16:31:21 +00001791 // TODO: Should this propagate fast-math-flags?
1792
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001793 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1794
1795 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1796
1797 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1798
1799 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1800}
1801
1802SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001803 if (DAG.getTarget().Options.UnsafeFPMath)
1804 return LowerFastFDIV(Op, DAG);
1805
1806 SDLoc SL(Op);
1807 SDValue X = Op.getOperand(0);
1808 SDValue Y = Op.getOperand(1);
1809
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001810 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001811
1812 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
1813
1814 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
1815
1816 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
1817
1818 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
1819
1820 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
1821
1822 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
1823
1824 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
1825
1826 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
1827
1828 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
1829 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
1830
1831 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
1832 NegDivScale0, Mul, DivScale1);
1833
1834 SDValue Scale;
1835
1836 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1837 // Workaround a hardware bug on SI where the condition output from div_scale
1838 // is not usable.
1839
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001840 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001841
1842 // Figure out if the scale to use for div_fmas.
1843 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1844 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
1845 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
1846 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
1847
1848 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
1849 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
1850
1851 SDValue Scale0Hi
1852 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
1853 SDValue Scale1Hi
1854 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
1855
1856 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
1857 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
1858 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
1859 } else {
1860 Scale = DivScale1.getValue(1);
1861 }
1862
1863 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
1864 Fma4, Fma3, Mul, Scale);
1865
1866 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001867}
1868
1869SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1870 EVT VT = Op.getValueType();
1871
1872 if (VT == MVT::f32)
1873 return LowerFDIV32(Op, DAG);
1874
1875 if (VT == MVT::f64)
1876 return LowerFDIV64(Op, DAG);
1877
1878 llvm_unreachable("Unexpected type for fdiv");
1879}
1880
Tom Stellard81d871d2013-11-13 23:36:50 +00001881SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1882 SDLoc DL(Op);
1883 StoreSDNode *Store = cast<StoreSDNode>(Op);
1884 EVT VT = Store->getMemoryVT();
1885
Matt Arsenault95245662016-02-11 05:32:46 +00001886 if (VT == MVT::i1) {
1887 return DAG.getTruncStore(Store->getChain(), DL,
1888 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1889 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00001890 }
1891
Matt Arsenault95245662016-02-11 05:32:46 +00001892 assert(Store->getValue().getValueType().getScalarType() == MVT::i32);
Tom Stellard81d871d2013-11-13 23:36:50 +00001893
Matt Arsenault95245662016-02-11 05:32:46 +00001894 unsigned NElts = VT.getVectorNumElements();
1895 unsigned AS = Store->getAddressSpace();
1896 if (AS == AMDGPUAS::LOCAL_ADDRESS) {
1897 // If properly aligned, if we split we might be able to use ds_write_b64.
1898 return SplitVectorStore(Op, DAG);
1899 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001900
Matt Arsenault95245662016-02-11 05:32:46 +00001901 if (AS == AMDGPUAS::PRIVATE_ADDRESS && NElts > 4)
1902 return ScalarizeVectorStore(Op, DAG);
1903
1904 // These stores are legal. private, global and flat.
1905 if (NElts >= 8)
1906 return SplitVectorStore(Op, DAG);
Tom Stellard1c8788e2014-03-07 20:12:33 +00001907
Tom Stellarde812f2f2014-07-21 15:45:06 +00001908 return SDValue();
Tom Stellard81d871d2013-11-13 23:36:50 +00001909}
1910
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001911SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001912 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001913 EVT VT = Op.getValueType();
1914 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00001915 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001916 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
1917 DAG.getNode(ISD::FMUL, DL, VT, Arg,
1918 DAG.getConstantFP(0.5/M_PI, DL,
1919 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001920
1921 switch (Op.getOpcode()) {
1922 case ISD::FCOS:
1923 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1924 case ISD::FSIN:
1925 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1926 default:
1927 llvm_unreachable("Wrong trig opcode");
1928 }
1929}
1930
Tom Stellard75aadc22012-12-11 21:25:42 +00001931//===----------------------------------------------------------------------===//
1932// Custom DAG optimizations
1933//===----------------------------------------------------------------------===//
1934
Matt Arsenault364a6742014-06-11 17:50:44 +00001935SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00001936 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00001937 EVT VT = N->getValueType(0);
1938 EVT ScalarVT = VT.getScalarType();
1939 if (ScalarVT != MVT::f32)
1940 return SDValue();
1941
1942 SelectionDAG &DAG = DCI.DAG;
1943 SDLoc DL(N);
1944
1945 SDValue Src = N->getOperand(0);
1946 EVT SrcVT = Src.getValueType();
1947
1948 // TODO: We could try to match extracting the higher bytes, which would be
1949 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1950 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1951 // about in practice.
1952 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1953 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1954 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1955 DCI.AddToWorklist(Cvt.getNode());
1956 return Cvt;
1957 }
1958 }
1959
1960 // We are primarily trying to catch operations on illegal vector types
1961 // before they are expanded.
1962 // For scalars, we can use the more flexible method of checking masked bits
1963 // after legalization.
1964 if (!DCI.isBeforeLegalize() ||
1965 !SrcVT.isVector() ||
1966 SrcVT.getVectorElementType() != MVT::i8) {
1967 return SDValue();
1968 }
1969
1970 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1971
1972 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1973 // size as 4.
1974 unsigned NElts = SrcVT.getVectorNumElements();
1975 if (!SrcVT.isSimple() && NElts != 3)
1976 return SDValue();
1977
1978 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1979 // prevent a mess from expanding to v4i32 and repacking.
1980 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1981 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1982 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1983 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00001984 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001985
1986 unsigned AS = Load->getAddressSpace();
1987 unsigned Align = Load->getAlignment();
1988 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001989 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001990
1991 // Don't try to replace the load if we have to expand it due to alignment
1992 // problems. Otherwise we will end up scalarizing the load, and trying to
1993 // repack into the vector for no real reason.
1994 if (Align < ABIAlignment &&
1995 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
1996 return SDValue();
1997 }
1998
Matt Arsenault364a6742014-06-11 17:50:44 +00001999 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
2000 Load->getChain(),
2001 Load->getBasePtr(),
2002 LoadVT,
2003 Load->getMemOperand());
2004
2005 // Make sure successors of the original load stay after it by updating
2006 // them to use the new Chain.
2007 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
2008
2009 SmallVector<SDValue, 4> Elts;
2010 if (RegVT.isVector())
2011 DAG.ExtractVectorElements(NewLoad, Elts);
2012 else
2013 Elts.push_back(NewLoad);
2014
2015 SmallVector<SDValue, 4> Ops;
2016
2017 unsigned EltIdx = 0;
2018 for (SDValue Elt : Elts) {
2019 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
2020 for (unsigned I = 0; I < ComponentsInElt; ++I) {
2021 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
2022 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
2023 DCI.AddToWorklist(Cvt.getNode());
2024 Ops.push_back(Cvt);
2025 }
2026
2027 ++EltIdx;
2028 }
2029
2030 assert(Ops.size() == NElts);
2031
2032 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
2033 }
2034
2035 return SDValue();
2036}
2037
Eric Christopher6c5b5112015-03-11 18:43:21 +00002038/// \brief Return true if the given offset Size in bytes can be folded into
2039/// the immediate offsets of a memory instruction for the given address space.
2040static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
2041 const AMDGPUSubtarget &STI) {
2042 switch (AS) {
2043 case AMDGPUAS::GLOBAL_ADDRESS: {
2044 // MUBUF instructions a 12-bit offset in bytes.
2045 return isUInt<12>(OffsetSize);
2046 }
2047 case AMDGPUAS::CONSTANT_ADDRESS: {
2048 // SMRD instructions have an 8-bit offset in dwords on SI and
2049 // a 20-bit offset in bytes on VI.
2050 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
2051 return isUInt<20>(OffsetSize);
2052 else
2053 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
2054 }
2055 case AMDGPUAS::LOCAL_ADDRESS:
2056 case AMDGPUAS::REGION_ADDRESS: {
2057 // The single offset versions have a 16-bit offset in bytes.
2058 return isUInt<16>(OffsetSize);
2059 }
2060 case AMDGPUAS::PRIVATE_ADDRESS:
2061 // Indirect register addressing does not use any offsets.
2062 default:
2063 return 0;
2064 }
2065}
2066
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002067// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
2068
2069// This is a variant of
2070// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
2071//
2072// The normal DAG combiner will do this, but only if the add has one use since
2073// that would increase the number of instructions.
2074//
2075// This prevents us from seeing a constant offset that can be folded into a
2076// memory instruction's addressing mode. If we know the resulting add offset of
2077// a pointer can be folded into an addressing offset, we can replace the pointer
2078// operand with the add of new constant offset. This eliminates one of the uses,
2079// and may allow the remaining use to also be simplified.
2080//
2081SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
2082 unsigned AddrSpace,
2083 DAGCombinerInfo &DCI) const {
2084 SDValue N0 = N->getOperand(0);
2085 SDValue N1 = N->getOperand(1);
2086
2087 if (N0.getOpcode() != ISD::ADD)
2088 return SDValue();
2089
2090 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
2091 if (!CN1)
2092 return SDValue();
2093
2094 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2095 if (!CAdd)
2096 return SDValue();
2097
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002098 // If the resulting offset is too large, we can't fold it into the addressing
2099 // mode offset.
2100 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Eric Christopher6c5b5112015-03-11 18:43:21 +00002101 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002102 return SDValue();
2103
2104 SelectionDAG &DAG = DCI.DAG;
2105 SDLoc SL(N);
2106 EVT VT = N->getValueType(0);
2107
2108 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002109 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002110
2111 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
2112}
2113
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002114SDValue SITargetLowering::performAndCombine(SDNode *N,
2115 DAGCombinerInfo &DCI) const {
2116 if (DCI.isBeforeLegalize())
2117 return SDValue();
2118
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002119 if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI))
2120 return Base;
2121
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002122 SelectionDAG &DAG = DCI.DAG;
2123
2124 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
2125 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
2126 SDValue LHS = N->getOperand(0);
2127 SDValue RHS = N->getOperand(1);
2128
2129 if (LHS.getOpcode() == ISD::SETCC &&
2130 RHS.getOpcode() == ISD::SETCC) {
2131 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2132 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2133
2134 SDValue X = LHS.getOperand(0);
2135 SDValue Y = RHS.getOperand(0);
2136 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2137 return SDValue();
2138
2139 if (LCC == ISD::SETO) {
2140 if (X != LHS.getOperand(1))
2141 return SDValue();
2142
2143 if (RCC == ISD::SETUNE) {
2144 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2145 if (!C1 || !C1->isInfinity() || C1->isNegative())
2146 return SDValue();
2147
2148 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2149 SIInstrFlags::N_SUBNORMAL |
2150 SIInstrFlags::N_ZERO |
2151 SIInstrFlags::P_ZERO |
2152 SIInstrFlags::P_SUBNORMAL |
2153 SIInstrFlags::P_NORMAL;
2154
2155 static_assert(((~(SIInstrFlags::S_NAN |
2156 SIInstrFlags::Q_NAN |
2157 SIInstrFlags::N_INFINITY |
2158 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2159 "mask not equal");
2160
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002161 SDLoc DL(N);
2162 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2163 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002164 }
2165 }
2166 }
2167
2168 return SDValue();
2169}
2170
Matt Arsenaultf2290332015-01-06 23:00:39 +00002171SDValue SITargetLowering::performOrCombine(SDNode *N,
2172 DAGCombinerInfo &DCI) const {
2173 SelectionDAG &DAG = DCI.DAG;
2174 SDValue LHS = N->getOperand(0);
2175 SDValue RHS = N->getOperand(1);
2176
2177 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
2178 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
2179 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
2180 SDValue Src = LHS.getOperand(0);
2181 if (Src != RHS.getOperand(0))
2182 return SDValue();
2183
2184 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
2185 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
2186 if (!CLHS || !CRHS)
2187 return SDValue();
2188
2189 // Only 10 bits are used.
2190 static const uint32_t MaxMask = 0x3ff;
2191
2192 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002193 SDLoc DL(N);
2194 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2195 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00002196 }
2197
2198 return SDValue();
2199}
2200
2201SDValue SITargetLowering::performClassCombine(SDNode *N,
2202 DAGCombinerInfo &DCI) const {
2203 SelectionDAG &DAG = DCI.DAG;
2204 SDValue Mask = N->getOperand(1);
2205
2206 // fp_class x, 0 -> false
2207 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
2208 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002209 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002210 }
2211
2212 return SDValue();
2213}
2214
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002215static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
2216 switch (Opc) {
2217 case ISD::FMAXNUM:
2218 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002219 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002220 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002221 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002222 return AMDGPUISD::UMAX3;
2223 case ISD::FMINNUM:
2224 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002225 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002226 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002227 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002228 return AMDGPUISD::UMIN3;
2229 default:
2230 llvm_unreachable("Not a min/max opcode");
2231 }
2232}
2233
Matt Arsenaultf639c322016-01-28 20:53:42 +00002234static SDValue performIntMed3ImmCombine(SelectionDAG &DAG,
2235 SDLoc SL,
2236 SDValue Op0,
2237 SDValue Op1,
2238 bool Signed) {
2239 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
2240 if (!K1)
2241 return SDValue();
2242
2243 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
2244 if (!K0)
2245 return SDValue();
2246
2247
2248 if (Signed) {
2249 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
2250 return SDValue();
2251 } else {
2252 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
2253 return SDValue();
2254 }
2255
2256 EVT VT = K0->getValueType(0);
2257 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
2258 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
2259}
2260
2261static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
2262 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
2263 return true;
2264
2265 return DAG.isKnownNeverNaN(Op);
2266}
2267
2268static SDValue performFPMed3ImmCombine(SelectionDAG &DAG,
2269 SDLoc SL,
2270 SDValue Op0,
2271 SDValue Op1) {
2272 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
2273 if (!K1)
2274 return SDValue();
2275
2276 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
2277 if (!K0)
2278 return SDValue();
2279
2280 // Ordered >= (although NaN inputs should have folded away by now).
2281 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
2282 if (Cmp == APFloat::cmpGreaterThan)
2283 return SDValue();
2284
2285 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
2286 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
2287 // give the other result, which is different from med3 with a NaN input.
2288 SDValue Var = Op0.getOperand(0);
2289 if (!isKnownNeverSNan(DAG, Var))
2290 return SDValue();
2291
2292 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
2293 Var, SDValue(K0, 0), SDValue(K1, 0));
2294}
2295
2296SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
2297 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002298 SelectionDAG &DAG = DCI.DAG;
2299
2300 unsigned Opc = N->getOpcode();
2301 SDValue Op0 = N->getOperand(0);
2302 SDValue Op1 = N->getOperand(1);
2303
2304 // Only do this if the inner op has one use since this will just increases
2305 // register pressure for no benefit.
2306
Matt Arsenault5b39b342016-01-28 20:53:48 +00002307 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
2308 // max(max(a, b), c) -> max3(a, b, c)
2309 // min(min(a, b), c) -> min3(a, b, c)
2310 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
2311 SDLoc DL(N);
2312 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2313 DL,
2314 N->getValueType(0),
2315 Op0.getOperand(0),
2316 Op0.getOperand(1),
2317 Op1);
2318 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002319
Matt Arsenault5b39b342016-01-28 20:53:48 +00002320 // Try commuted.
2321 // max(a, max(b, c)) -> max3(a, b, c)
2322 // min(a, min(b, c)) -> min3(a, b, c)
2323 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
2324 SDLoc DL(N);
2325 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2326 DL,
2327 N->getValueType(0),
2328 Op0,
2329 Op1.getOperand(0),
2330 Op1.getOperand(1));
2331 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002332 }
2333
Matt Arsenaultf639c322016-01-28 20:53:42 +00002334 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
2335 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
2336 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
2337 return Med3;
2338 }
2339
2340 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
2341 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
2342 return Med3;
2343 }
2344
2345 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00002346 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
2347 (Opc == AMDGPUISD::FMIN_LEGACY &&
2348 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00002349 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
2350 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
2351 return Res;
2352 }
2353
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002354 return SDValue();
2355}
2356
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002357SDValue SITargetLowering::performSetCCCombine(SDNode *N,
2358 DAGCombinerInfo &DCI) const {
2359 SelectionDAG &DAG = DCI.DAG;
2360 SDLoc SL(N);
2361
2362 SDValue LHS = N->getOperand(0);
2363 SDValue RHS = N->getOperand(1);
2364 EVT VT = LHS.getValueType();
2365
2366 if (VT != MVT::f32 && VT != MVT::f64)
2367 return SDValue();
2368
2369 // Match isinf pattern
2370 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
2371 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
2372 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
2373 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2374 if (!CRHS)
2375 return SDValue();
2376
2377 const APFloat &APF = CRHS->getValueAPF();
2378 if (APF.isInfinity() && !APF.isNegative()) {
2379 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002380 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
2381 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002382 }
2383 }
2384
2385 return SDValue();
2386}
2387
Tom Stellard75aadc22012-12-11 21:25:42 +00002388SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
2389 DAGCombinerInfo &DCI) const {
2390 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002391 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00002392
2393 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00002394 default:
2395 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002396 case ISD::SETCC:
2397 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00002398 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002399 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002400 case ISD::SMAX:
2401 case ISD::SMIN:
2402 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00002403 case ISD::UMIN:
2404 case AMDGPUISD::FMIN_LEGACY:
2405 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002406 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00002407 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002408 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00002409 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002410 break;
2411 }
Matt Arsenault364a6742014-06-11 17:50:44 +00002412
2413 case AMDGPUISD::CVT_F32_UBYTE0:
2414 case AMDGPUISD::CVT_F32_UBYTE1:
2415 case AMDGPUISD::CVT_F32_UBYTE2:
2416 case AMDGPUISD::CVT_F32_UBYTE3: {
2417 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
2418
2419 SDValue Src = N->getOperand(0);
2420 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
2421
2422 APInt KnownZero, KnownOne;
2423 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2424 !DCI.isBeforeLegalizeOps());
2425 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2426 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
2427 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
2428 DCI.CommitTargetLoweringOpt(TLO);
2429 }
2430
2431 break;
2432 }
2433
2434 case ISD::UINT_TO_FP: {
2435 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002436 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002437 case ISD::FADD: {
2438 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2439 break;
2440
2441 EVT VT = N->getValueType(0);
2442 if (VT != MVT::f32)
2443 break;
2444
Matt Arsenault8d630032015-02-20 22:10:41 +00002445 // Only do this if we are not trying to support denormals. v_mad_f32 does
2446 // not support denormals ever.
2447 if (Subtarget->hasFP32Denormals())
2448 break;
2449
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002450 SDValue LHS = N->getOperand(0);
2451 SDValue RHS = N->getOperand(1);
2452
2453 // These should really be instruction patterns, but writing patterns with
2454 // source modiifiers is a pain.
2455
2456 // fadd (fadd (a, a), b) -> mad 2.0, a, b
2457 if (LHS.getOpcode() == ISD::FADD) {
2458 SDValue A = LHS.getOperand(0);
2459 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002460 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002461 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002462 }
2463 }
2464
2465 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
2466 if (RHS.getOpcode() == ISD::FADD) {
2467 SDValue A = RHS.getOperand(0);
2468 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002469 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002470 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002471 }
2472 }
2473
Matt Arsenault8d630032015-02-20 22:10:41 +00002474 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002475 }
Matt Arsenault8675db12014-08-29 16:01:14 +00002476 case ISD::FSUB: {
2477 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2478 break;
2479
2480 EVT VT = N->getValueType(0);
2481
2482 // Try to get the fneg to fold into the source modifier. This undoes generic
2483 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00002484 //
2485 // Only do this if we are not trying to support denormals. v_mad_f32 does
2486 // not support denormals ever.
2487 if (VT == MVT::f32 &&
2488 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00002489 SDValue LHS = N->getOperand(0);
2490 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002491 if (LHS.getOpcode() == ISD::FADD) {
2492 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
2493
2494 SDValue A = LHS.getOperand(0);
2495 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002496 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002497 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
2498
Matt Arsenault8d630032015-02-20 22:10:41 +00002499 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002500 }
2501 }
2502
2503 if (RHS.getOpcode() == ISD::FADD) {
2504 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
2505
2506 SDValue A = RHS.getOperand(0);
2507 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002508 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002509 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002510 }
2511 }
Matt Arsenault8d630032015-02-20 22:10:41 +00002512
2513 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00002514 }
2515
2516 break;
2517 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002518 case ISD::LOAD:
2519 case ISD::STORE:
2520 case ISD::ATOMIC_LOAD:
2521 case ISD::ATOMIC_STORE:
2522 case ISD::ATOMIC_CMP_SWAP:
2523 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2524 case ISD::ATOMIC_SWAP:
2525 case ISD::ATOMIC_LOAD_ADD:
2526 case ISD::ATOMIC_LOAD_SUB:
2527 case ISD::ATOMIC_LOAD_AND:
2528 case ISD::ATOMIC_LOAD_OR:
2529 case ISD::ATOMIC_LOAD_XOR:
2530 case ISD::ATOMIC_LOAD_NAND:
2531 case ISD::ATOMIC_LOAD_MIN:
2532 case ISD::ATOMIC_LOAD_MAX:
2533 case ISD::ATOMIC_LOAD_UMIN:
2534 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
2535 if (DCI.isBeforeLegalize())
2536 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002537
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002538 MemSDNode *MemNode = cast<MemSDNode>(N);
2539 SDValue Ptr = MemNode->getBasePtr();
2540
2541 // TODO: We could also do this for multiplies.
2542 unsigned AS = MemNode->getAddressSpace();
2543 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
2544 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
2545 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002546 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002547
2548 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
2549 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
2550 }
2551 }
2552 break;
2553 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002554 case ISD::AND:
2555 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002556 case ISD::OR:
2557 return performOrCombine(N, DCI);
2558 case AMDGPUISD::FP_CLASS:
2559 return performClassCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002560 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002561 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00002562}
Christian Konigd910b7d2013-02-26 17:52:16 +00002563
Christian Konigf82901a2013-02-26 17:52:23 +00002564/// \brief Analyze the possible immediate value Op
2565///
2566/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
2567/// and the immediate value if it's a literal immediate
2568int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
2569
Eric Christopher7792e322015-01-30 23:24:40 +00002570 const SIInstrInfo *TII =
2571 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00002572
Tom Stellardedbf1eb2013-04-05 23:31:20 +00002573 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00002574 if (TII->isInlineConstant(Node->getAPIntValue()))
2575 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00002576
Matt Arsenault11a4d672015-02-13 19:05:03 +00002577 uint64_t Val = Node->getZExtValue();
2578 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00002579 }
2580
2581 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
2582 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
2583 return 0;
2584
2585 if (Node->getValueType(0) == MVT::f32)
2586 return FloatToBits(Node->getValueAPF().convertToFloat());
2587
2588 return -1;
2589 }
2590
2591 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00002592}
2593
Christian Konig8e06e2a2013-04-10 08:39:08 +00002594/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00002595static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00002596 switch (Idx) {
2597 default: return 0;
2598 case AMDGPU::sub0: return 0;
2599 case AMDGPU::sub1: return 1;
2600 case AMDGPU::sub2: return 2;
2601 case AMDGPU::sub3: return 3;
2602 }
2603}
2604
2605/// \brief Adjust the writemask of MIMG instructions
2606void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
2607 SelectionDAG &DAG) const {
2608 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00002609 unsigned Lane = 0;
2610 unsigned OldDmask = Node->getConstantOperandVal(0);
2611 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002612
2613 // Try to figure out the used register components
2614 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
2615 I != E; ++I) {
2616
2617 // Abort if we can't understand the usage
2618 if (!I->isMachineOpcode() ||
2619 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
2620 return;
2621
Tom Stellard54774e52013-10-23 02:53:47 +00002622 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
2623 // Note that subregs are packed, i.e. Lane==0 is the first bit set
2624 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
2625 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00002626 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00002627
Tom Stellard54774e52013-10-23 02:53:47 +00002628 // Set which texture component corresponds to the lane.
2629 unsigned Comp;
2630 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
2631 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00002632 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00002633 Dmask &= ~(1 << Comp);
2634 }
2635
Christian Konig8e06e2a2013-04-10 08:39:08 +00002636 // Abort if we have more than one user per component
2637 if (Users[Lane])
2638 return;
2639
2640 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00002641 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002642 }
2643
Tom Stellard54774e52013-10-23 02:53:47 +00002644 // Abort if there's no change
2645 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00002646 return;
2647
2648 // Adjust the writemask in the node
2649 std::vector<SDValue> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002650 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002651 Ops.insert(Ops.end(), Node->op_begin() + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00002652 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002653
Christian Konig8b1ed282013-04-10 08:39:16 +00002654 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00002655 // (if NewDmask has only one bit set...)
2656 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002657 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
2658 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00002659 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002660 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00002661 SDValue(Node, 0), RC);
2662 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
2663 return;
2664 }
2665
Christian Konig8e06e2a2013-04-10 08:39:08 +00002666 // Update the users of the node with the new indices
2667 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
2668
2669 SDNode *User = Users[i];
2670 if (!User)
2671 continue;
2672
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002673 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002674 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
2675
2676 switch (Idx) {
2677 default: break;
2678 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
2679 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
2680 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
2681 }
2682 }
2683}
2684
Tom Stellardc98ee202015-07-16 19:40:07 +00002685static bool isFrameIndexOp(SDValue Op) {
2686 if (Op.getOpcode() == ISD::AssertZext)
2687 Op = Op.getOperand(0);
2688
2689 return isa<FrameIndexSDNode>(Op);
2690}
2691
Tom Stellard3457a842014-10-09 19:06:00 +00002692/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
2693/// with frame index operands.
2694/// LLVM assumes that inputs are to these instructions are registers.
2695void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
2696 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002697
2698 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00002699 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00002700 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00002701 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002702 continue;
2703 }
2704
Tom Stellard3457a842014-10-09 19:06:00 +00002705 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002706 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00002707 Node->getOperand(i).getValueType(),
2708 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002709 }
2710
Tom Stellard3457a842014-10-09 19:06:00 +00002711 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002712}
2713
Matt Arsenault08d84942014-06-03 23:06:13 +00002714/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00002715SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
2716 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002717 const SIInstrInfo *TII =
2718 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konig8e06e2a2013-04-10 08:39:08 +00002719
Tom Stellard16a9a202013-08-14 23:24:17 +00002720 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00002721 adjustWritemask(Node, DAG);
2722
Matt Arsenault7d858d82014-11-02 23:46:54 +00002723 if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG ||
2724 Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002725 legalizeTargetIndependentNode(Node, DAG);
2726 return Node;
2727 }
Tom Stellard654d6692015-01-08 15:08:17 +00002728 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002729}
Christian Konig8b1ed282013-04-10 08:39:16 +00002730
2731/// \brief Assign the register class depending on the number of
2732/// bits set in the writemask
2733void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
2734 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002735 const SIInstrInfo *TII =
2736 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002737
Tom Stellarda99ada52014-11-21 22:31:44 +00002738 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00002739
2740 if (TII->isVOP3(MI->getOpcode())) {
2741 // Make sure constant bus requirements are respected.
2742 TII->legalizeOperandsVOP3(MRI, MI);
2743 return;
2744 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00002745
Matt Arsenault3add6432015-10-20 04:35:43 +00002746 if (TII->isMIMG(*MI)) {
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002747 unsigned VReg = MI->getOperand(0).getReg();
2748 unsigned Writemask = MI->getOperand(1).getImm();
2749 unsigned BitsSet = 0;
2750 for (unsigned i = 0; i < 4; ++i)
2751 BitsSet += Writemask & (1 << i) ? 1 : 0;
2752
2753 const TargetRegisterClass *RC;
2754 switch (BitsSet) {
2755 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002756 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002757 case 2: RC = &AMDGPU::VReg_64RegClass; break;
2758 case 3: RC = &AMDGPU::VReg_96RegClass; break;
2759 }
2760
2761 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
2762 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002763 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00002764 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00002765 }
2766
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002767 // Replace unused atomics with the no return version.
2768 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
2769 if (NoRetAtomicOp != -1) {
2770 if (!Node->hasAnyUseOfValue(0)) {
2771 MI->setDesc(TII->get(NoRetAtomicOp));
2772 MI->RemoveOperand(0);
2773 }
2774
2775 return;
2776 }
Christian Konig8b1ed282013-04-10 08:39:16 +00002777}
Tom Stellard0518ff82013-06-03 17:39:58 +00002778
Matt Arsenault485defe2014-11-05 19:01:17 +00002779static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002780 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00002781 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
2782}
2783
2784MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
2785 SDLoc DL,
2786 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002787 const SIInstrInfo *TII =
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002788 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00002789
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002790 // Build the half of the subregister with the constants before building the
2791 // full 128-bit register. If we are building multiple resource descriptors,
2792 // this will allow CSEing of the 2-component register.
2793 const SDValue Ops0[] = {
2794 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
2795 buildSMovImm32(DAG, DL, 0),
2796 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
2797 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
2798 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
2799 };
Matt Arsenault485defe2014-11-05 19:01:17 +00002800
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002801 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
2802 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00002803
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002804 // Combine the constants and the pointer.
2805 const SDValue Ops1[] = {
2806 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
2807 Ptr,
2808 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
2809 SubRegHi,
2810 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
2811 };
Matt Arsenault485defe2014-11-05 19:01:17 +00002812
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002813 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00002814}
2815
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002816/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002817/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
2818/// of the resource descriptor) to create an offset, which is added to
2819/// the resource pointer.
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002820MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
2821 SDLoc DL,
2822 SDValue Ptr,
2823 uint32_t RsrcDword1,
2824 uint64_t RsrcDword2And3) const {
2825 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
2826 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
2827 if (RsrcDword1) {
2828 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002829 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
2830 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002831 }
2832
2833 SDValue DataLo = buildSMovImm32(DAG, DL,
2834 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
2835 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
2836
2837 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002838 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002839 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002840 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002841 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002842 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002843 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002844 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002845 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002846 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002847 };
2848
2849 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2850}
2851
Tom Stellard94593ee2013-06-03 17:40:18 +00002852SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2853 const TargetRegisterClass *RC,
2854 unsigned Reg, EVT VT) const {
2855 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2856
2857 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2858 cast<RegisterSDNode>(VReg)->getReg(), VT);
2859}
Tom Stellardd7e6f132015-04-08 01:09:26 +00002860
2861//===----------------------------------------------------------------------===//
2862// SI Inline Assembly Support
2863//===----------------------------------------------------------------------===//
2864
2865std::pair<unsigned, const TargetRegisterClass *>
2866SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002867 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00002868 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002869
2870 if (Constraint.size() == 1) {
2871 switch (Constraint[0]) {
2872 case 's':
2873 case 'r':
2874 switch (VT.getSizeInBits()) {
2875 default:
2876 return std::make_pair(0U, nullptr);
2877 case 32:
Tom Stellardd7e6f132015-04-08 01:09:26 +00002878 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002879 case 64:
2880 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
2881 case 128:
2882 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
2883 case 256:
2884 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
2885 }
2886
2887 case 'v':
2888 switch (VT.getSizeInBits()) {
2889 default:
2890 return std::make_pair(0U, nullptr);
2891 case 32:
2892 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
2893 case 64:
2894 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
2895 case 96:
2896 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
2897 case 128:
2898 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
2899 case 256:
2900 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
2901 case 512:
2902 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
2903 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00002904 }
2905 }
2906
2907 if (Constraint.size() > 1) {
2908 const TargetRegisterClass *RC = nullptr;
2909 if (Constraint[1] == 'v') {
2910 RC = &AMDGPU::VGPR_32RegClass;
2911 } else if (Constraint[1] == 's') {
2912 RC = &AMDGPU::SGPR_32RegClass;
2913 }
2914
2915 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00002916 uint32_t Idx;
2917 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
2918 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00002919 return std::make_pair(RC->getRegister(Idx), RC);
2920 }
2921 }
2922 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
2923}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002924
2925SITargetLowering::ConstraintType
2926SITargetLowering::getConstraintType(StringRef Constraint) const {
2927 if (Constraint.size() == 1) {
2928 switch (Constraint[0]) {
2929 default: break;
2930 case 's':
2931 case 'v':
2932 return C_RegisterClass;
2933 }
2934 }
2935 return TargetLowering::getConstraintType(Constraint);
2936}