blob: 46b73f7ed097c7f40cf498dc68d66ddd385b7e2e [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
Matt Arsenault61738cb2016-02-27 08:53:46 +0000138 // On SI this is s_memtime and s_memrealtime on VI.
139 setOperationAction(ISD::READCYCLECOUNTER, MVT::i64, Legal);
140
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000141 for (MVT VT : MVT::integer_valuetypes()) {
Matt Arsenaultbd223422015-01-14 01:35:17 +0000142 if (VT == MVT::i64)
143 continue;
144
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000145 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000146 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
147 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000148 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000149
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000150 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000151 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
152 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000153 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000154
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000155 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000156 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
157 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000158 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
159 }
160
161 for (MVT VT : MVT::integer_vector_valuetypes()) {
162 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
163 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
164 }
165
166 for (MVT VT : MVT::fp_valuetypes())
167 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000168
Matt Arsenault61001bb2015-11-25 19:58:34 +0000169 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
170 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
171
Matt Arsenault6f243792013-09-05 19:41:10 +0000172 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000173 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
Matt Arsenaulte1ce3442015-07-31 04:12:04 +0000174 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000175 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000176
Matt Arsenault61001bb2015-11-25 19:58:34 +0000177
178 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
179
180 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
181 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
182
Matt Arsenault470acd82014-04-15 22:28:39 +0000183 setOperationAction(ISD::LOAD, MVT::i1, Custom);
184
Matt Arsenault61001bb2015-11-25 19:58:34 +0000185 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
186 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
187
188 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
189 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
190
191 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
192
Tom Stellardfd155822013-08-26 15:05:36 +0000193 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000194 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000195 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000196
Tom Stellard5f337882014-04-29 23:12:43 +0000197 // These should use UDIVREM, so set them to expand
198 setOperationAction(ISD::UDIV, MVT::i64, Expand);
199 setOperationAction(ISD::UREM, MVT::i64, Expand);
200
Matt Arsenault0d89e842014-07-15 21:44:37 +0000201 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
202 setOperationAction(ISD::SELECT, MVT::i1, Promote);
203
Matt Arsenault61001bb2015-11-25 19:58:34 +0000204 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
205
206
207 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
208
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000209 // We only support LOAD/STORE and vector manipulation ops for vectors
210 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000211 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000212 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
213 switch(Op) {
214 case ISD::LOAD:
215 case ISD::STORE:
216 case ISD::BUILD_VECTOR:
217 case ISD::BITCAST:
218 case ISD::EXTRACT_VECTOR_ELT:
219 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000220 case ISD::INSERT_SUBVECTOR:
221 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000222 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000223 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000224 case ISD::CONCAT_VECTORS:
225 setOperationAction(Op, VT, Custom);
226 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000227 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000228 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000229 break;
230 }
231 }
232 }
233
Matt Arsenault61001bb2015-11-25 19:58:34 +0000234 // Most operations are naturally 32-bit vector operations. We only support
235 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
236 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
237 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
238 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
239
240 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
241 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
242
243 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
244 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
245
246 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
247 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
248 }
249
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000250 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
251 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
252 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000253 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000254 }
255
Marek Olsak7d777282015-03-24 13:40:15 +0000256 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000257 setOperationAction(ISD::FDIV, MVT::f32, Custom);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +0000258 setOperationAction(ISD::FDIV, MVT::f64, Custom);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000259
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000260 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000261 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000262 setTargetDAGCombine(ISD::FMINNUM);
263 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000264 setTargetDAGCombine(ISD::SMIN);
265 setTargetDAGCombine(ISD::SMAX);
266 setTargetDAGCombine(ISD::UMIN);
267 setTargetDAGCombine(ISD::UMAX);
Tom Stellard75aadc22012-12-11 21:25:42 +0000268 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000269 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000270 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000271 setTargetDAGCombine(ISD::UINT_TO_FP);
272
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000273 // All memory operations. Some folding on the pointer operand is done to help
274 // matching the constant offsets in the addressing modes.
275 setTargetDAGCombine(ISD::LOAD);
276 setTargetDAGCombine(ISD::STORE);
277 setTargetDAGCombine(ISD::ATOMIC_LOAD);
278 setTargetDAGCombine(ISD::ATOMIC_STORE);
279 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
280 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
281 setTargetDAGCombine(ISD::ATOMIC_SWAP);
282 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
283 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
284 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
285 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
286 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
287 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
288 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
289 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
290 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
291 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
292
Christian Konigeecebd02013-03-26 14:04:02 +0000293 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000294}
295
Tom Stellard0125f2a2013-06-25 02:39:35 +0000296//===----------------------------------------------------------------------===//
297// TargetLowering queries
298//===----------------------------------------------------------------------===//
299
Matt Arsenaulte306a322014-10-21 16:25:08 +0000300bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
301 EVT) const {
302 // SI has some legal vector types, but no legal vector operations. Say no
303 // shuffles are legal in order to prefer scalarizing some vector operations.
304 return false;
305}
306
Tom Stellard70580f82015-07-20 14:28:41 +0000307bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
308 // Flat instructions do not have offsets, and only have the register
309 // address.
310 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
311}
312
Matt Arsenault711b3902015-08-07 20:18:34 +0000313bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
314 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
315 // additionally can do r + r + i with addr64. 32-bit has more addressing
316 // mode options. Depending on the resource constant, it can also do
317 // (i64 r0) + (i32 r1) * (i14 i).
318 //
319 // Private arrays end up using a scratch buffer most of the time, so also
320 // assume those use MUBUF instructions. Scratch loads / stores are currently
321 // implemented as mubuf instructions with offen bit set, so slightly
322 // different than the normal addr64.
323 if (!isUInt<12>(AM.BaseOffs))
324 return false;
325
326 // FIXME: Since we can split immediate into soffset and immediate offset,
327 // would it make sense to allow any immediate?
328
329 switch (AM.Scale) {
330 case 0: // r + i or just i, depending on HasBaseReg.
331 return true;
332 case 1:
333 return true; // We have r + r or r + i.
334 case 2:
335 if (AM.HasBaseReg) {
336 // Reject 2 * r + r.
337 return false;
338 }
339
340 // Allow 2 * r as r + r
341 // Or 2 * r + i is allowed as r + r + i.
342 return true;
343 default: // Don't allow n * r
344 return false;
345 }
346}
347
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000348bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
349 const AddrMode &AM, Type *Ty,
350 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000351 // No global is ever allowed as a base.
352 if (AM.BaseGV)
353 return false;
354
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000355 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000356 case AMDGPUAS::GLOBAL_ADDRESS: {
Tom Stellard70580f82015-07-20 14:28:41 +0000357 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
358 // Assume the we will use FLAT for all global memory accesses
359 // on VI.
360 // FIXME: This assumption is currently wrong. On VI we still use
361 // MUBUF instructions for the r + i addressing mode. As currently
362 // implemented, the MUBUF instructions only work on buffer < 4GB.
363 // It may be possible to support > 4GB buffers with MUBUF instructions,
364 // by setting the stride value in the resource descriptor which would
365 // increase the size limit to (stride * 4GB). However, this is risky,
366 // because it has never been validated.
367 return isLegalFlatAddressingMode(AM);
368 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000369
Matt Arsenault711b3902015-08-07 20:18:34 +0000370 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000371 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000372 case AMDGPUAS::CONSTANT_ADDRESS: {
373 // If the offset isn't a multiple of 4, it probably isn't going to be
374 // correctly aligned.
375 if (AM.BaseOffs % 4 != 0)
376 return isLegalMUBUFAddressingMode(AM);
377
378 // There are no SMRD extloads, so if we have to do a small type access we
379 // will use a MUBUF load.
380 // FIXME?: We also need to do this if unaligned, but we don't know the
381 // alignment here.
382 if (DL.getTypeStoreSize(Ty) < 4)
383 return isLegalMUBUFAddressingMode(AM);
384
385 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
386 // SMRD instructions have an 8-bit, dword offset on SI.
387 if (!isUInt<8>(AM.BaseOffs / 4))
388 return false;
389 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) {
390 // On CI+, this can also be a 32-bit literal constant offset. If it fits
391 // in 8-bits, it can use a smaller encoding.
392 if (!isUInt<32>(AM.BaseOffs / 4))
393 return false;
394 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) {
395 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
396 if (!isUInt<20>(AM.BaseOffs))
397 return false;
398 } else
399 llvm_unreachable("unhandled generation");
400
401 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
402 return true;
403
404 if (AM.Scale == 1 && AM.HasBaseReg)
405 return true;
406
407 return false;
408 }
409
410 case AMDGPUAS::PRIVATE_ADDRESS:
411 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
412 return isLegalMUBUFAddressingMode(AM);
413
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000414 case AMDGPUAS::LOCAL_ADDRESS:
415 case AMDGPUAS::REGION_ADDRESS: {
416 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
417 // field.
418 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
419 // an 8-bit dword offset but we don't know the alignment here.
420 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000421 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000422
423 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
424 return true;
425
426 if (AM.Scale == 1 && AM.HasBaseReg)
427 return true;
428
Matt Arsenault5015a892014-08-15 17:17:07 +0000429 return false;
430 }
Tom Stellard70580f82015-07-20 14:28:41 +0000431 case AMDGPUAS::FLAT_ADDRESS:
432 return isLegalFlatAddressingMode(AM);
433
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000434 default:
435 llvm_unreachable("unhandled address space");
436 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000437}
438
Matt Arsenaulte6986632015-01-14 01:35:22 +0000439bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000440 unsigned AddrSpace,
441 unsigned Align,
442 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000443 if (IsFast)
444 *IsFast = false;
445
Matt Arsenault1018c892014-04-24 17:08:26 +0000446 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
447 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000448 if (!VT.isSimple() || VT == MVT::Other)
449 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000450
Tom Stellardc6b299c2015-02-02 18:02:28 +0000451 // TODO - CI+ supports unaligned memory accesses, but this requires driver
452 // support.
Matt Arsenault1018c892014-04-24 17:08:26 +0000453
Matt Arsenault1018c892014-04-24 17:08:26 +0000454 // XXX - The only mention I see of this in the ISA manual is for LDS direct
455 // reads the "byte address and must be dword aligned". Is it also true for the
456 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000457 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
458 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
459 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
460 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000461 bool AlignedBy4 = (Align % 4 == 0);
462 if (IsFast)
463 *IsFast = AlignedBy4;
464 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000465 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000466
Tom Stellard33e64c62015-02-04 20:49:52 +0000467 // Smaller than dword value must be aligned.
468 // FIXME: This should be allowed on CI+
469 if (VT.bitsLT(MVT::i32))
470 return false;
471
Matt Arsenault1018c892014-04-24 17:08:26 +0000472 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
473 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000474 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000475 if (IsFast)
476 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000477
478 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000479}
480
Matt Arsenault46645fa2014-07-28 17:49:26 +0000481EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
482 unsigned SrcAlign, bool IsMemset,
483 bool ZeroMemset,
484 bool MemcpyStrSrc,
485 MachineFunction &MF) const {
486 // FIXME: Should account for address space here.
487
488 // The default fallback uses the private pointer size as a guess for a type to
489 // use. Make sure we switch these to 64-bit accesses.
490
491 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
492 return MVT::v4i32;
493
494 if (Size >= 8 && DstAlign >= 4)
495 return MVT::v2i32;
496
497 // Use the default.
498 return MVT::Other;
499}
500
Matt Arsenaultf9bfeaf2015-12-01 23:04:00 +0000501static bool isFlatGlobalAddrSpace(unsigned AS) {
502 return AS == AMDGPUAS::GLOBAL_ADDRESS ||
503 AS == AMDGPUAS::FLAT_ADDRESS ||
504 AS == AMDGPUAS::CONSTANT_ADDRESS;
505}
506
507bool SITargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
508 unsigned DestAS) const {
509 return isFlatGlobalAddrSpace(SrcAS) && isFlatGlobalAddrSpace(DestAS);
510}
511
Tom Stellarda6f24c62015-12-15 20:55:55 +0000512
513bool SITargetLowering::isMemOpUniform(const SDNode *N) const {
514 const MemSDNode *MemNode = cast<MemSDNode>(N);
515 const Value *Ptr = MemNode->getMemOperand()->getValue();
516
517 // UndefValue means this is a load of a kernel input. These are uniform.
518 // Sometimes LDS instructions have constant pointers
519 if (isa<UndefValue>(Ptr) || isa<Argument>(Ptr) || isa<Constant>(Ptr) ||
520 isa<GlobalValue>(Ptr))
521 return true;
522
523 const Instruction *I = dyn_cast_or_null<Instruction>(Ptr);
524 return I && I->getMetadata("amdgpu.uniform");
525}
526
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000527TargetLoweringBase::LegalizeTypeAction
528SITargetLowering::getPreferredVectorAction(EVT VT) const {
529 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
530 return TypeSplitVector;
531
532 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000533}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000534
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000535bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
536 Type *Ty) const {
Eric Christopher7792e322015-01-30 23:24:40 +0000537 const SIInstrInfo *TII =
538 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000539 return TII->isInlineConstant(Imm);
540}
541
Tom Stellard2e045bb2016-01-20 00:13:22 +0000542bool SITargetLowering::isTypeDesirableForOp(unsigned Op, EVT VT) const {
543
544 // SimplifySetCC uses this function to determine whether or not it should
545 // create setcc with i1 operands. We don't have instructions for i1 setcc.
546 if (VT == MVT::i1 && Op == ISD::SETCC)
547 return false;
548
549 return TargetLowering::isTypeDesirableForOp(Op, VT);
550}
551
Tom Stellardaf775432013-10-23 00:44:32 +0000552SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000553 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000554 unsigned Offset, bool Signed) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000555 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000556 MachineFunction &MF = DAG.getMachineFunction();
557 const SIRegisterInfo *TRI =
558 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
Matt Arsenaultac234b62015-11-30 21:15:57 +0000559 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::KERNARG_SEGMENT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000560
Matt Arsenault86033ca2014-07-28 17:31:39 +0000561 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
562
563 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000564 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000565 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000566 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
567 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
568 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
569 DAG.getConstant(Offset, SL, PtrVT));
Mehdi Amini44ede332015-07-09 02:09:04 +0000570 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000571 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
572
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000573 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000574
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000575 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000576 if (MemVT.isFloatingPoint())
577 ExtTy = ISD::EXTLOAD;
578
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000579 return DAG.getLoad(ISD::UNINDEXED, ExtTy,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000580 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
581 false, // isVolatile
582 true, // isNonTemporal
583 true, // isInvariant
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000584 Align); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000585}
586
Christian Konig2c8f6d52013-03-07 09:03:52 +0000587SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000588 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
589 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
590 SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardec2e43c2014-09-22 15:35:29 +0000591 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000592 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000593
594 MachineFunction &MF = DAG.getMachineFunction();
595 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000596 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000597 const AMDGPUSubtarget &ST = MF.getSubtarget<AMDGPUSubtarget>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000598
Matt Arsenaultd48da142015-11-02 23:23:02 +0000599 if (Subtarget->isAmdHsaOS() && Info->getShaderType() != ShaderType::COMPUTE) {
600 const Function *Fn = MF.getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000601 DiagnosticInfoUnsupported NoGraphicsHSA(
602 *Fn, "unsupported non-compute shaders with HSA", DL.getDebugLoc());
Matt Arsenaultd48da142015-11-02 23:23:02 +0000603 DAG.getContext()->diagnose(NoGraphicsHSA);
604 return SDValue();
605 }
606
Tom Stellard0fbf8992015-10-06 21:16:34 +0000607 // FIXME: We currently assume all calling conventions are kernels.
Christian Konig2c8f6d52013-03-07 09:03:52 +0000608
609 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000610 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000611
612 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000613 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000614
615 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000616 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Marek Olsakb6c8c3d2016-01-13 11:46:10 +0000617 !Arg.Flags.isByVal() && PSInputNum <= 15) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000618
Marek Olsakfccabaf2016-01-13 11:45:36 +0000619 if (!Arg.Used && !Info->isPSInputAllocated(PSInputNum)) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000620 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000621 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000622 ++PSInputNum;
623 continue;
624 }
625
Marek Olsakfccabaf2016-01-13 11:45:36 +0000626 Info->markPSInputAllocated(PSInputNum);
627 if (Arg.Used)
628 Info->PSInputEna |= 1 << PSInputNum;
629
630 ++PSInputNum;
Christian Konig99ee0f42013-03-07 09:04:14 +0000631 }
632
633 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000634 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000635 ISD::InputArg NewArg = Arg;
636 NewArg.Flags.setSplit();
637 NewArg.VT = Arg.VT.getVectorElementType();
638
639 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
640 // three or five element vertex only needs three or five registers,
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000641 // NOT four or eight.
Andrew Trick05938a52015-02-16 18:10:47 +0000642 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000643 unsigned NumElements = ParamType->getVectorNumElements();
644
645 for (unsigned j = 0; j != NumElements; ++j) {
646 Splits.push_back(NewArg);
647 NewArg.PartOffset += NewArg.VT.getStoreSize();
648 }
649
Matt Arsenault762af962014-07-13 03:06:39 +0000650 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000651 Splits.push_back(Arg);
652 }
653 }
654
655 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000656 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
657 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000658
Christian Konig99ee0f42013-03-07 09:04:14 +0000659 // At least one interpolation mode must be enabled or else the GPU will hang.
Marek Olsakfccabaf2016-01-13 11:45:36 +0000660 //
661 // Check PSInputAddr instead of PSInputEna. The idea is that if the user set
662 // PSInputAddr, the user wants to enable some bits after the compilation
663 // based on run-time states. Since we can't know what the final PSInputEna
664 // will look like, so we shouldn't do anything here and the user should take
665 // responsibility for the correct programming.
Marek Olsak46dadbf2016-01-13 17:23:20 +0000666 //
667 // Otherwise, the following restrictions apply:
668 // - At least one of PERSP_* (0xF) or LINEAR_* (0x70) must be enabled.
669 // - If POS_W_FLOAT (11) is enabled, at least one of PERSP_* must be
670 // enabled too.
Matt Arsenault762af962014-07-13 03:06:39 +0000671 if (Info->getShaderType() == ShaderType::PIXEL &&
Marek Olsak46dadbf2016-01-13 17:23:20 +0000672 ((Info->getPSInputAddr() & 0x7F) == 0 ||
673 ((Info->getPSInputAddr() & 0xF) == 0 &&
674 Info->isPSInputAllocated(11)))) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000675 CCInfo.AllocateReg(AMDGPU::VGPR0);
676 CCInfo.AllocateReg(AMDGPU::VGPR1);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000677 Info->markPSInputAllocated(0);
678 Info->PSInputEna |= 1;
Christian Konig99ee0f42013-03-07 09:04:14 +0000679 }
680
Matt Arsenault762af962014-07-13 03:06:39 +0000681 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000682 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
683 Splits);
684 }
685
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000686 // FIXME: How should these inputs interact with inreg / custom SGPR inputs?
687 if (Info->hasPrivateSegmentBuffer()) {
688 unsigned PrivateSegmentBufferReg = Info->addPrivateSegmentBuffer(*TRI);
689 MF.addLiveIn(PrivateSegmentBufferReg, &AMDGPU::SReg_128RegClass);
690 CCInfo.AllocateReg(PrivateSegmentBufferReg);
691 }
692
693 if (Info->hasDispatchPtr()) {
694 unsigned DispatchPtrReg = Info->addDispatchPtr(*TRI);
695 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
696 CCInfo.AllocateReg(DispatchPtrReg);
697 }
698
699 if (Info->hasKernargSegmentPtr()) {
700 unsigned InputPtrReg = Info->addKernargSegmentPtr(*TRI);
701 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
702 CCInfo.AllocateReg(InputPtrReg);
703 }
704
Matt Arsenault296b8492016-02-12 06:31:30 +0000705 if (Info->hasFlatScratchInit()) {
706 unsigned FlatScratchInitReg = Info->addFlatScratchInit(*TRI);
707 MF.addLiveIn(FlatScratchInitReg, &AMDGPU::SReg_64RegClass);
708 CCInfo.AllocateReg(FlatScratchInitReg);
709 }
710
Christian Konig2c8f6d52013-03-07 09:03:52 +0000711 AnalyzeFormalArguments(CCInfo, Splits);
712
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000713 SmallVector<SDValue, 16> Chains;
714
Christian Konig2c8f6d52013-03-07 09:03:52 +0000715 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
716
Christian Konigb7be72d2013-05-17 09:46:48 +0000717 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000718 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000719 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000720 continue;
721 }
722
Christian Konig2c8f6d52013-03-07 09:03:52 +0000723 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000724 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000725
726 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000727 VT = Ins[i].VT;
728 EVT MemVT = Splits[i].VT;
Tom Stellardb5798b02015-06-26 21:15:03 +0000729 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
730 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000731 // The first 36 bytes of the input buffer contains information about
732 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000733 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000734 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000735 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000736
Craig Toppere3dcce92015-08-01 22:20:21 +0000737 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000738 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000739 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
740 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
741 // On SI local pointers are just offsets into LDS, so they are always
742 // less than 16-bits. On CI and newer they could potentially be
743 // real pointers, so we can't guarantee their size.
744 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
745 DAG.getValueType(MVT::i16));
746 }
747
Tom Stellarded882c22013-06-03 17:40:11 +0000748 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000749 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000750 continue;
751 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000752 assert(VA.isRegLoc() && "Parameter must be in a register!");
753
754 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000755
756 if (VT == MVT::i64) {
757 // For now assume it is a pointer
758 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
759 &AMDGPU::SReg_64RegClass);
760 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000761 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
762 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000763 continue;
764 }
765
766 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
767
768 Reg = MF.addLiveIn(Reg, RC);
769 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
770
Christian Konig2c8f6d52013-03-07 09:03:52 +0000771 if (Arg.VT.isVector()) {
772
773 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000774 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000775 unsigned NumElements = ParamType->getVectorNumElements();
776
777 SmallVector<SDValue, 4> Regs;
778 Regs.push_back(Val);
779 for (unsigned j = 1; j != NumElements; ++j) {
780 Reg = ArgLocs[ArgIdx++].getLocReg();
781 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000782
783 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
784 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000785 }
786
787 // Fill up the missing vector elements
788 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000789 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000790
Craig Topper48d114b2014-04-26 18:35:24 +0000791 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000792 continue;
793 }
794
795 InVals.push_back(Val);
796 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000797
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000798 // TODO: Add GridWorkGroupCount user SGPRs when used. For now with HSA we read
799 // these from the dispatch pointer.
800
801 // Start adding system SGPRs.
802 if (Info->hasWorkGroupIDX()) {
803 unsigned Reg = Info->addWorkGroupIDX();
804 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
805 CCInfo.AllocateReg(Reg);
806 } else
807 llvm_unreachable("work group id x is always enabled");
808
809 if (Info->hasWorkGroupIDY()) {
810 unsigned Reg = Info->addWorkGroupIDY();
811 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
812 CCInfo.AllocateReg(Reg);
Tom Stellarde99fb652015-01-20 19:33:04 +0000813 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000814
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000815 if (Info->hasWorkGroupIDZ()) {
816 unsigned Reg = Info->addWorkGroupIDZ();
817 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
818 CCInfo.AllocateReg(Reg);
819 }
820
821 if (Info->hasWorkGroupInfo()) {
822 unsigned Reg = Info->addWorkGroupInfo();
823 MF.addLiveIn(Reg, &AMDGPU::SReg_32RegClass);
824 CCInfo.AllocateReg(Reg);
825 }
826
827 if (Info->hasPrivateSegmentWaveByteOffset()) {
828 // Scratch wave offset passed in system SGPR.
829 unsigned PrivateSegmentWaveByteOffsetReg
830 = Info->addPrivateSegmentWaveByteOffset();
831
832 MF.addLiveIn(PrivateSegmentWaveByteOffsetReg, &AMDGPU::SGPR_32RegClass);
833 CCInfo.AllocateReg(PrivateSegmentWaveByteOffsetReg);
834 }
835
836 // Now that we've figured out where the scratch register inputs are, see if
837 // should reserve the arguments and use them directly.
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000838 bool HasStackObjects = MF.getFrameInfo()->hasStackObjects();
Matt Arsenault296b8492016-02-12 06:31:30 +0000839 // Record that we know we have non-spill stack objects so we don't need to
840 // check all stack objects later.
841 if (HasStackObjects)
842 Info->setHasNonSpillStackObjects(true);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000843
844 if (ST.isAmdHsaOS()) {
845 // TODO: Assume we will spill without optimizations.
846 if (HasStackObjects) {
847 // If we have stack objects, we unquestionably need the private buffer
848 // resource. For the HSA ABI, this will be the first 4 user SGPR
849 // inputs. We can reserve those and use them directly.
850
851 unsigned PrivateSegmentBufferReg = TRI->getPreloadedValue(
852 MF, SIRegisterInfo::PRIVATE_SEGMENT_BUFFER);
853 Info->setScratchRSrcReg(PrivateSegmentBufferReg);
854
855 unsigned PrivateSegmentWaveByteOffsetReg = TRI->getPreloadedValue(
856 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
857 Info->setScratchWaveOffsetReg(PrivateSegmentWaveByteOffsetReg);
858 } else {
859 unsigned ReservedBufferReg
860 = TRI->reservedPrivateSegmentBufferReg(MF);
861 unsigned ReservedOffsetReg
862 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
863
864 // We tentatively reserve the last registers (skipping the last two
865 // which may contain VCC). After register allocation, we'll replace
866 // these with the ones immediately after those which were really
867 // allocated. In the prologue copies will be inserted from the argument
868 // to these reserved registers.
869 Info->setScratchRSrcReg(ReservedBufferReg);
870 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
871 }
872 } else {
873 unsigned ReservedBufferReg = TRI->reservedPrivateSegmentBufferReg(MF);
874
875 // Without HSA, relocations are used for the scratch pointer and the
876 // buffer resource setup is always inserted in the prologue. Scratch wave
877 // offset is still in an input SGPR.
878 Info->setScratchRSrcReg(ReservedBufferReg);
879
880 if (HasStackObjects) {
881 unsigned ScratchWaveOffsetReg = TRI->getPreloadedValue(
882 MF, SIRegisterInfo::PRIVATE_SEGMENT_WAVE_BYTE_OFFSET);
883 Info->setScratchWaveOffsetReg(ScratchWaveOffsetReg);
884 } else {
885 unsigned ReservedOffsetReg
886 = TRI->reservedPrivateSegmentWaveByteOffsetReg(MF);
887 Info->setScratchWaveOffsetReg(ReservedOffsetReg);
888 }
889 }
890
891 if (Info->hasWorkItemIDX()) {
892 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X);
893 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
894 CCInfo.AllocateReg(Reg);
895 } else
896 llvm_unreachable("workitem id x should always be enabled");
897
898 if (Info->hasWorkItemIDY()) {
899 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y);
900 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
901 CCInfo.AllocateReg(Reg);
902 }
903
904 if (Info->hasWorkItemIDZ()) {
905 unsigned Reg = TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z);
906 MF.addLiveIn(Reg, &AMDGPU::VGPR_32RegClass);
907 CCInfo.AllocateReg(Reg);
908 }
Matt Arsenault0e3d3892015-11-30 21:15:53 +0000909
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000910 if (Chains.empty())
911 return Chain;
912
913 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000914}
915
Marek Olsak8a0f3352016-01-13 17:23:04 +0000916SDValue SITargetLowering::LowerReturn(SDValue Chain,
917 CallingConv::ID CallConv,
918 bool isVarArg,
919 const SmallVectorImpl<ISD::OutputArg> &Outs,
920 const SmallVectorImpl<SDValue> &OutVals,
921 SDLoc DL, SelectionDAG &DAG) const {
922 MachineFunction &MF = DAG.getMachineFunction();
923 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
924
925 if (Info->getShaderType() == ShaderType::COMPUTE)
926 return AMDGPUTargetLowering::LowerReturn(Chain, CallConv, isVarArg, Outs,
927 OutVals, DL, DAG);
928
Marek Olsak8e9cc632016-01-13 17:23:09 +0000929 Info->setIfReturnsVoid(Outs.size() == 0);
930
Marek Olsak8a0f3352016-01-13 17:23:04 +0000931 SmallVector<ISD::OutputArg, 48> Splits;
932 SmallVector<SDValue, 48> SplitVals;
933
934 // Split vectors into their elements.
935 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
936 const ISD::OutputArg &Out = Outs[i];
937
938 if (Out.VT.isVector()) {
939 MVT VT = Out.VT.getVectorElementType();
940 ISD::OutputArg NewOut = Out;
941 NewOut.Flags.setSplit();
942 NewOut.VT = VT;
943
944 // We want the original number of vector elements here, e.g.
945 // three or five, not four or eight.
946 unsigned NumElements = Out.ArgVT.getVectorNumElements();
947
948 for (unsigned j = 0; j != NumElements; ++j) {
949 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, OutVals[i],
950 DAG.getConstant(j, DL, MVT::i32));
951 SplitVals.push_back(Elem);
952 Splits.push_back(NewOut);
953 NewOut.PartOffset += NewOut.VT.getStoreSize();
954 }
955 } else {
956 SplitVals.push_back(OutVals[i]);
957 Splits.push_back(Out);
958 }
959 }
960
961 // CCValAssign - represent the assignment of the return value to a location.
962 SmallVector<CCValAssign, 48> RVLocs;
963
964 // CCState - Info about the registers and stack slots.
965 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
966 *DAG.getContext());
967
968 // Analyze outgoing return values.
969 AnalyzeReturn(CCInfo, Splits);
970
971 SDValue Flag;
972 SmallVector<SDValue, 48> RetOps;
973 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
974
975 // Copy the result values into the output registers.
976 for (unsigned i = 0, realRVLocIdx = 0;
977 i != RVLocs.size();
978 ++i, ++realRVLocIdx) {
979 CCValAssign &VA = RVLocs[i];
980 assert(VA.isRegLoc() && "Can only return in registers!");
981
982 SDValue Arg = SplitVals[realRVLocIdx];
983
984 // Copied from other backends.
985 switch (VA.getLocInfo()) {
986 default: llvm_unreachable("Unknown loc info!");
987 case CCValAssign::Full:
988 break;
989 case CCValAssign::BCvt:
990 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
991 break;
992 }
993
994 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Arg, Flag);
995 Flag = Chain.getValue(1);
996 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
997 }
998
999 // Update chain and glue.
1000 RetOps[0] = Chain;
1001 if (Flag.getNode())
1002 RetOps.push_back(Flag);
1003
1004 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, RetOps);
1005}
1006
Matt Arsenault9a10cea2016-01-26 04:29:24 +00001007unsigned SITargetLowering::getRegisterByName(const char* RegName, EVT VT,
1008 SelectionDAG &DAG) const {
1009 unsigned Reg = StringSwitch<unsigned>(RegName)
1010 .Case("m0", AMDGPU::M0)
1011 .Case("exec", AMDGPU::EXEC)
1012 .Case("exec_lo", AMDGPU::EXEC_LO)
1013 .Case("exec_hi", AMDGPU::EXEC_HI)
1014 .Case("flat_scratch", AMDGPU::FLAT_SCR)
1015 .Case("flat_scratch_lo", AMDGPU::FLAT_SCR_LO)
1016 .Case("flat_scratch_hi", AMDGPU::FLAT_SCR_HI)
1017 .Default(AMDGPU::NoRegister);
1018
1019 if (Reg == AMDGPU::NoRegister) {
1020 report_fatal_error(Twine("invalid register name \""
1021 + StringRef(RegName) + "\"."));
1022
1023 }
1024
1025 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
1026 Subtarget->getRegisterInfo()->regsOverlap(Reg, AMDGPU::FLAT_SCR)) {
1027 report_fatal_error(Twine("invalid register \""
1028 + StringRef(RegName) + "\" for subtarget."));
1029 }
1030
1031 switch (Reg) {
1032 case AMDGPU::M0:
1033 case AMDGPU::EXEC_LO:
1034 case AMDGPU::EXEC_HI:
1035 case AMDGPU::FLAT_SCR_LO:
1036 case AMDGPU::FLAT_SCR_HI:
1037 if (VT.getSizeInBits() == 32)
1038 return Reg;
1039 break;
1040 case AMDGPU::EXEC:
1041 case AMDGPU::FLAT_SCR:
1042 if (VT.getSizeInBits() == 64)
1043 return Reg;
1044 break;
1045 default:
1046 llvm_unreachable("missing register type checking");
1047 }
1048
1049 report_fatal_error(Twine("invalid type for register \""
1050 + StringRef(RegName) + "\"."));
1051}
1052
Tom Stellard75aadc22012-12-11 21:25:42 +00001053MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
1054 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +00001055
Tom Stellard75aadc22012-12-11 21:25:42 +00001056 switch (MI->getOpcode()) {
1057 default:
1058 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Matt Arsenault20711b72015-02-20 22:10:45 +00001059 case AMDGPU::BRANCH:
1060 return BB;
Tom Stellard75aadc22012-12-11 21:25:42 +00001061 }
1062 return BB;
1063}
1064
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001065bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
1066 // This currently forces unfolding various combinations of fsub into fma with
1067 // free fneg'd operands. As long as we have fast FMA (controlled by
1068 // isFMAFasterThanFMulAndFAdd), we should perform these.
1069
1070 // When fma is quarter rate, for f64 where add / sub are at best half rate,
1071 // most of these combines appear to be cycle neutral but save on instruction
1072 // count / code size.
1073 return true;
1074}
1075
Mehdi Amini44ede332015-07-09 02:09:04 +00001076EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
1077 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +00001078 if (!VT.isVector()) {
1079 return MVT::i1;
1080 }
Matt Arsenault8596f712014-11-28 22:51:38 +00001081 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +00001082}
1083
Mehdi Aminieaabc512015-07-09 15:12:23 +00001084MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +00001085 return MVT::i32;
1086}
1087
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001088// Answering this is somewhat tricky and depends on the specific device which
1089// have different rates for fma or all f64 operations.
1090//
1091// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
1092// regardless of which device (although the number of cycles differs between
1093// devices), so it is always profitable for f64.
1094//
1095// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
1096// only on full rate devices. Normally, we should prefer selecting v_mad_f32
1097// which we can always do even without fused FP ops since it returns the same
1098// result as the separate operations and since it is always full
1099// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
1100// however does not support denormals, so we do report fma as faster if we have
1101// a fast fma device and require denormals.
1102//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001103bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
1104 VT = VT.getScalarType();
1105
1106 if (!VT.isSimple())
1107 return false;
1108
1109 switch (VT.getSimpleVT().SimpleTy) {
1110 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +00001111 // This is as fast on some subtargets. However, we always have full rate f32
1112 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +00001113 // which we should prefer over fma. We can't use this if we want to support
1114 // denormals, so only report this in these cases.
1115 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +00001116 case MVT::f64:
1117 return true;
1118 default:
1119 break;
1120 }
1121
1122 return false;
1123}
1124
Tom Stellard75aadc22012-12-11 21:25:42 +00001125//===----------------------------------------------------------------------===//
1126// Custom DAG Lowering Operations
1127//===----------------------------------------------------------------------===//
1128
1129SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
1130 switch (Op.getOpcode()) {
1131 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001132 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +00001133 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +00001134 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +00001135 SDValue Result = LowerLOAD(Op, DAG);
1136 assert((!Result.getNode() ||
1137 Result.getNode()->getNumValues() == 2) &&
1138 "Load should return a value and a chain");
1139 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +00001140 }
Tom Stellardaf775432013-10-23 00:44:32 +00001141
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001142 case ISD::FSIN:
1143 case ISD::FCOS:
1144 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001145 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001146 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001147 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001148 case ISD::GlobalAddress: {
1149 MachineFunction &MF = DAG.getMachineFunction();
1150 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1151 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +00001152 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001153 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
1154 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +00001155 }
1156 return SDValue();
1157}
1158
Tom Stellardf8794352012-12-19 22:10:31 +00001159/// \brief Helper function for LowerBRCOND
1160static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +00001161
Tom Stellardf8794352012-12-19 22:10:31 +00001162 SDNode *Parent = Value.getNode();
1163 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
1164 I != E; ++I) {
1165
1166 if (I.getUse().get() != Value)
1167 continue;
1168
1169 if (I->getOpcode() == Opcode)
1170 return *I;
1171 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001172 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001173}
1174
Tom Stellardb02094e2014-07-21 15:45:01 +00001175SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
1176
Tom Stellardc98ee202015-07-16 19:40:07 +00001177 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +00001178 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
1179 unsigned FrameIndex = FINode->getIndex();
1180
Tom Stellardc98ee202015-07-16 19:40:07 +00001181 // A FrameIndex node represents a 32-bit offset into scratch memory. If
1182 // the high bit of a frame index offset were to be set, this would mean
1183 // that it represented an offset of ~2GB * 64 = ~128GB from the start of the
1184 // scratch buffer, with 64 being the number of threads per wave.
1185 //
1186 // If we know the machine uses less than 128GB of scratch, then we can
1187 // amrk the high bit of the FrameIndex node as known zero,
1188 // which is important, because it means in most situations we can
1189 // prove that values derived from FrameIndex nodes are non-negative.
1190 // This enables us to take advantage of more addressing modes when
1191 // accessing scratch buffers, since for scratch reads/writes, the register
1192 // offset must always be positive.
1193
1194 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
1195 if (Subtarget->enableHugeScratchBuffer())
1196 return TFI;
1197
1198 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
1199 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), 31)));
Tom Stellardb02094e2014-07-21 15:45:01 +00001200}
1201
Tom Stellardbc4497b2016-02-12 23:45:29 +00001202bool SITargetLowering::isCFIntrinsic(const SDNode *Intr) const {
Matt Arsenault16f48d72016-02-13 00:36:10 +00001203 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN)
Tom Stellardbc4497b2016-02-12 23:45:29 +00001204 return false;
1205
1206 switch (cast<ConstantSDNode>(Intr->getOperand(1))->getZExtValue()) {
1207 default: return false;
1208 case AMDGPUIntrinsic::amdgcn_if:
1209 case AMDGPUIntrinsic::amdgcn_else:
1210 case AMDGPUIntrinsic::amdgcn_break:
1211 case AMDGPUIntrinsic::amdgcn_if_break:
1212 case AMDGPUIntrinsic::amdgcn_else_break:
1213 case AMDGPUIntrinsic::amdgcn_loop:
1214 case AMDGPUIntrinsic::amdgcn_end_cf:
1215 return true;
1216 }
1217}
1218
Tom Stellardf8794352012-12-19 22:10:31 +00001219/// This transforms the control flow intrinsics to get the branch destination as
1220/// last parameter, also switches branch target with BR if the need arise
1221SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
1222 SelectionDAG &DAG) const {
1223
Andrew Trickef9de2a2013-05-25 02:42:55 +00001224 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +00001225
1226 SDNode *Intr = BRCOND.getOperand(1).getNode();
1227 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +00001228 SDNode *BR = nullptr;
Tom Stellardbc4497b2016-02-12 23:45:29 +00001229 SDNode *SetCC = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +00001230
1231 if (Intr->getOpcode() == ISD::SETCC) {
1232 // As long as we negate the condition everything is fine
Tom Stellardbc4497b2016-02-12 23:45:29 +00001233 SetCC = Intr;
Tom Stellardf8794352012-12-19 22:10:31 +00001234 Intr = SetCC->getOperand(0).getNode();
1235
1236 } else {
1237 // Get the target from BR if we don't negate the condition
1238 BR = findUser(BRCOND, ISD::BR);
1239 Target = BR->getOperand(1);
1240 }
1241
Tom Stellardbc4497b2016-02-12 23:45:29 +00001242 if (Intr->getOpcode() != ISD::INTRINSIC_W_CHAIN) {
1243 // This is a uniform branch so we don't need to legalize.
1244 return BRCOND;
1245 }
1246
1247 assert(!SetCC ||
1248 (SetCC->getConstantOperandVal(1) == 1 &&
1249 isCFIntrinsic(Intr) &&
1250 cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
1251 ISD::SETNE));
Tom Stellardf8794352012-12-19 22:10:31 +00001252
1253 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001254 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001255
1256 // operands of the new intrinsic call
1257 SmallVector<SDValue, 4> Ops;
1258 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001259 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +00001260 Ops.push_back(Target);
1261
1262 // build the new intrinsic call
1263 SDNode *Result = DAG.getNode(
1264 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +00001265 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001266
1267 if (BR) {
1268 // Give the branch instruction our target
1269 SDValue Ops[] = {
1270 BR->getOperand(0),
1271 BRCOND.getOperand(2)
1272 };
Chandler Carruth356665a2014-08-01 22:09:43 +00001273 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
1274 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
1275 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +00001276 }
1277
1278 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
1279
1280 // Copy the intrinsic results to registers
1281 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
1282 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
1283 if (!CopyToReg)
1284 continue;
1285
1286 Chain = DAG.getCopyToReg(
1287 Chain, DL,
1288 CopyToReg->getOperand(1),
1289 SDValue(Result, i - 1),
1290 SDValue());
1291
1292 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
1293 }
1294
1295 // Remove the old intrinsic from the chain
1296 DAG.ReplaceAllUsesOfValueWith(
1297 SDValue(Intr, Intr->getNumValues() - 1),
1298 Intr->getOperand(0));
1299
1300 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +00001301}
1302
Tom Stellard067c8152014-07-21 14:01:14 +00001303SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1304 SDValue Op,
1305 SelectionDAG &DAG) const {
1306 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1307
1308 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
1309 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1310
1311 SDLoc DL(GSD);
1312 const GlobalValue *GV = GSD->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001313 MVT PtrVT = getPointerTy(DAG.getDataLayout(), GSD->getAddressSpace());
Tom Stellard067c8152014-07-21 14:01:14 +00001314
Tom Stellard067c8152014-07-21 14:01:14 +00001315 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
Tom Stellardc93fc112015-12-10 02:13:01 +00001316 return DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT, GA);
Tom Stellard067c8152014-07-21 14:01:14 +00001317}
1318
Tom Stellardfc92e772015-05-12 14:18:14 +00001319SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL,
1320 SDValue V) const {
1321 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1322 // so we will end up with redundant moves to m0.
1323 //
1324 // We can't use S_MOV_B32, because there is no way to specify m0 as the
1325 // destination register.
1326 //
1327 // We have to use them both. Machine cse will combine all the S_MOV_B32
1328 // instructions and the register coalescer eliminate the extra copies.
1329 SDNode *M0 = DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, V.getValueType(), V);
1330 return DAG.getCopyToReg(Chain, DL, DAG.getRegister(AMDGPU::M0, MVT::i32),
1331 SDValue(M0, 0), SDValue()); // Glue
1332 // A Null SDValue creates
1333 // a glue result.
1334}
1335
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001336SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1337 SDValue Op,
1338 MVT VT,
1339 unsigned Offset) const {
1340 SDLoc SL(Op);
1341 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1342 DAG.getEntryNode(), Offset, false);
1343 // The local size values will have the hi 16-bits as zero.
1344 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1345 DAG.getValueType(VT));
1346}
1347
Matt Arsenaulte0132462016-01-30 05:19:45 +00001348static SDValue emitNonHSAIntrinsicError(SelectionDAG& DAG, EVT VT) {
1349 DiagnosticInfoUnsupported BadIntrin(*DAG.getMachineFunction().getFunction(),
1350 "non-hsa intrinsic with hsa target");
1351 DAG.getContext()->diagnose(BadIntrin);
1352 return DAG.getUNDEF(VT);
1353}
1354
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001355SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1356 SelectionDAG &DAG) const {
1357 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001358 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardec2e43c2014-09-22 15:35:29 +00001359 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +00001360 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001361
1362 EVT VT = Op.getValueType();
1363 SDLoc DL(Op);
1364 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1365
Sanjay Patela2607012015-09-16 16:31:21 +00001366 // TODO: Should this propagate fast-math-flags?
1367
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001368 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001369 case Intrinsic::amdgcn_dispatch_ptr:
Matt Arsenault800fecf2016-01-11 21:18:33 +00001370 if (!Subtarget->isAmdHsaOS()) {
Oliver Stannard7e7d9832016-02-02 13:52:43 +00001371 DiagnosticInfoUnsupported BadIntrin(
1372 *MF.getFunction(), "unsupported hsa intrinsic without hsa target",
1373 DL.getDebugLoc());
Matt Arsenault800fecf2016-01-11 21:18:33 +00001374 DAG.getContext()->diagnose(BadIntrin);
1375 return DAG.getUNDEF(VT);
1376 }
1377
Tom Stellard48f29f22015-11-26 00:43:29 +00001378 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
1379 TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_PTR), VT);
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001380 case Intrinsic::amdgcn_rcp:
1381 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
1382 case Intrinsic::amdgcn_rsq:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001383 case AMDGPUIntrinsic::AMDGPU_rsq: // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001384 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
Matt Arsenault79963e82016-02-13 01:03:00 +00001385 case Intrinsic::amdgcn_rsq_clamp:
Matt Arsenault0c3e2332016-01-26 04:14:16 +00001386 case AMDGPUIntrinsic::AMDGPU_rsq_clamped: { // Legacy name
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001387 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
Matt Arsenault79963e82016-02-13 01:03:00 +00001388 return DAG.getNode(AMDGPUISD::RSQ_CLAMP, DL, VT, Op.getOperand(1));
Tom Stellard48f29f22015-11-26 00:43:29 +00001389
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001390 Type *Type = VT.getTypeForEVT(*DAG.getContext());
1391 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
1392 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
1393
1394 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
1395 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
1396 DAG.getConstantFP(Max, DL, VT));
1397 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
1398 DAG.getConstantFP(Min, DL, VT));
1399 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001400 case Intrinsic::r600_read_ngroups_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001401 if (Subtarget->isAmdHsaOS())
1402 return emitNonHSAIntrinsicError(DAG, VT);
1403
Tom Stellardec2e43c2014-09-22 15:35:29 +00001404 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1405 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001406 case Intrinsic::r600_read_ngroups_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001407 if (Subtarget->isAmdHsaOS())
1408 return emitNonHSAIntrinsicError(DAG, VT);
1409
Tom Stellardec2e43c2014-09-22 15:35:29 +00001410 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1411 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001412 case Intrinsic::r600_read_ngroups_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001413 if (Subtarget->isAmdHsaOS())
1414 return emitNonHSAIntrinsicError(DAG, VT);
1415
Tom Stellardec2e43c2014-09-22 15:35:29 +00001416 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1417 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001418 case Intrinsic::r600_read_global_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001419 if (Subtarget->isAmdHsaOS())
1420 return emitNonHSAIntrinsicError(DAG, VT);
1421
Tom Stellardec2e43c2014-09-22 15:35:29 +00001422 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1423 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001424 case Intrinsic::r600_read_global_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001425 if (Subtarget->isAmdHsaOS())
1426 return emitNonHSAIntrinsicError(DAG, VT);
1427
Tom Stellardec2e43c2014-09-22 15:35:29 +00001428 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1429 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001430 case Intrinsic::r600_read_global_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001431 if (Subtarget->isAmdHsaOS())
1432 return emitNonHSAIntrinsicError(DAG, VT);
1433
Tom Stellardec2e43c2014-09-22 15:35:29 +00001434 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1435 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001436 case Intrinsic::r600_read_local_size_x:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001437 if (Subtarget->isAmdHsaOS())
1438 return emitNonHSAIntrinsicError(DAG, VT);
1439
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001440 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1441 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001442 case Intrinsic::r600_read_local_size_y:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001443 if (Subtarget->isAmdHsaOS())
1444 return emitNonHSAIntrinsicError(DAG, VT);
1445
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001446 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1447 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001448 case Intrinsic::r600_read_local_size_z:
Matt Arsenaulte0132462016-01-30 05:19:45 +00001449 if (Subtarget->isAmdHsaOS())
1450 return emitNonHSAIntrinsicError(DAG, VT);
1451
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001452 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1453 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Matt Arsenaultbef34e22016-01-22 21:30:34 +00001454 case Intrinsic::amdgcn_read_workdim:
1455 case AMDGPUIntrinsic::AMDGPU_read_workdim: // Legacy name.
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001456 // Really only 2 bits.
1457 return lowerImplicitZextParam(DAG, Op, MVT::i8,
1458 getImplicitParameterOffset(MFI, GRID_DIM));
Matt Arsenault43976df2016-01-30 04:25:19 +00001459 case Intrinsic::amdgcn_workgroup_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001460 case Intrinsic::r600_read_tgid_x:
1461 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001462 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001463 case Intrinsic::amdgcn_workgroup_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001464 case Intrinsic::r600_read_tgid_y:
1465 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001466 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001467 case Intrinsic::amdgcn_workgroup_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001468 case Intrinsic::r600_read_tgid_z:
1469 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001470 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKGROUP_ID_Z), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001471 case Intrinsic::amdgcn_workitem_id_x:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001472 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001473 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001474 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_X), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001475 case Intrinsic::amdgcn_workitem_id_y:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001476 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001477 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001478 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Y), VT);
Matt Arsenault43976df2016-01-30 04:25:19 +00001479 case Intrinsic::amdgcn_workitem_id_z:
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001480 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001481 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Matt Arsenaultac234b62015-11-30 21:15:57 +00001482 TRI->getPreloadedValue(MF, SIRegisterInfo::WORKITEM_ID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001483 case AMDGPUIntrinsic::SI_load_const: {
1484 SDValue Ops[] = {
1485 Op.getOperand(1),
1486 Op.getOperand(2)
1487 };
1488
1489 MachineMemOperand *MMO = MF.getMachineMemOperand(
1490 MachinePointerInfo(),
1491 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
1492 VT.getStoreSize(), 4);
1493 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
1494 Op->getVTList(), Ops, VT, MMO);
1495 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001496 case AMDGPUIntrinsic::SI_vs_load_input:
1497 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1498 Op.getOperand(1),
1499 Op.getOperand(2),
1500 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00001501
Tom Stellard2a9d9472015-05-12 15:00:46 +00001502 case AMDGPUIntrinsic::SI_fs_constant: {
1503 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1504 SDValue Glue = M0.getValue(1);
1505 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
1506 DAG.getConstant(2, DL, MVT::i32), // P0
1507 Op.getOperand(1), Op.getOperand(2), Glue);
1508 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00001509 case AMDGPUIntrinsic::SI_packf16:
1510 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
1511 return DAG.getUNDEF(MVT::i32);
1512 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00001513 case AMDGPUIntrinsic::SI_fs_interp: {
1514 SDValue IJ = Op.getOperand(4);
1515 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1516 DAG.getConstant(0, DL, MVT::i32));
1517 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1518 DAG.getConstant(1, DL, MVT::i32));
1519 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1520 SDValue Glue = M0.getValue(1);
1521 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
1522 DAG.getVTList(MVT::f32, MVT::Glue),
1523 I, Op.getOperand(1), Op.getOperand(2), Glue);
1524 Glue = SDValue(P1.getNode(), 1);
1525 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
1526 Op.getOperand(1), Op.getOperand(2), Glue);
1527 }
Tom Stellardad7d03d2015-12-15 17:02:49 +00001528 case Intrinsic::amdgcn_interp_p1: {
1529 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(4));
1530 SDValue Glue = M0.getValue(1);
1531 return DAG.getNode(AMDGPUISD::INTERP_P1, DL, MVT::f32, Op.getOperand(1),
1532 Op.getOperand(2), Op.getOperand(3), Glue);
1533 }
1534 case Intrinsic::amdgcn_interp_p2: {
1535 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(5));
1536 SDValue Glue = SDValue(M0.getNode(), 1);
1537 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, Op.getOperand(1),
1538 Op.getOperand(2), Op.getOperand(3), Op.getOperand(4),
1539 Glue);
1540 }
Matt Arsenaultce56a0e2016-02-13 01:19:56 +00001541 case Intrinsic::amdgcn_sin:
1542 return DAG.getNode(AMDGPUISD::SIN_HW, DL, VT, Op.getOperand(1));
1543
1544 case Intrinsic::amdgcn_cos:
1545 return DAG.getNode(AMDGPUISD::COS_HW, DL, VT, Op.getOperand(1));
1546
1547 case Intrinsic::amdgcn_log_clamp: {
1548 if (Subtarget->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS)
1549 return SDValue();
1550
1551 DiagnosticInfoUnsupported BadIntrin(
1552 *MF.getFunction(), "intrinsic not supported on subtarget",
1553 DL.getDebugLoc());
1554 DAG.getContext()->diagnose(BadIntrin);
1555 return DAG.getUNDEF(VT);
1556 }
Matt Arsenaultf75257a2016-01-23 05:32:20 +00001557 case Intrinsic::amdgcn_ldexp:
1558 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT,
1559 Op.getOperand(1), Op.getOperand(2));
1560 case Intrinsic::amdgcn_class:
1561 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
1562 Op.getOperand(1), Op.getOperand(2));
1563 case Intrinsic::amdgcn_div_fmas:
1564 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
1565 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
1566 Op.getOperand(4));
1567
1568 case Intrinsic::amdgcn_div_fixup:
1569 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
1570 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
1571
1572 case Intrinsic::amdgcn_trig_preop:
1573 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
1574 Op.getOperand(1), Op.getOperand(2));
1575 case Intrinsic::amdgcn_div_scale: {
1576 // 3rd parameter required to be a constant.
1577 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1578 if (!Param)
1579 return DAG.getUNDEF(VT);
1580
1581 // Translate to the operands expected by the machine instruction. The
1582 // first parameter must be the same as the first instruction.
1583 SDValue Numerator = Op.getOperand(1);
1584 SDValue Denominator = Op.getOperand(2);
1585
1586 // Note this order is opposite of the machine instruction's operations,
1587 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
1588 // intrinsic has the numerator as the first operand to match a normal
1589 // division operation.
1590
1591 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
1592
1593 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
1594 Denominator, Numerator);
1595 }
1596 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte0:
1597 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Op.getOperand(1));
1598 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte1:
1599 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE1, DL, VT, Op.getOperand(1));
1600 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte2:
1601 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE2, DL, VT, Op.getOperand(1));
1602 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte3:
1603 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE3, DL, VT, Op.getOperand(1));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001604 default:
1605 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1606 }
1607}
1608
1609SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1610 SelectionDAG &DAG) const {
1611 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00001612 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001613 SDValue Chain = Op.getOperand(0);
1614 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1615
1616 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00001617 case AMDGPUIntrinsic::SI_sendmsg: {
1618 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1619 SDValue Glue = Chain.getValue(1);
1620 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1621 Op.getOperand(2), Glue);
1622 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001623 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001624 SDValue Ops[] = {
1625 Chain,
1626 Op.getOperand(2),
1627 Op.getOperand(3),
1628 Op.getOperand(4),
1629 Op.getOperand(5),
1630 Op.getOperand(6),
1631 Op.getOperand(7),
1632 Op.getOperand(8),
1633 Op.getOperand(9),
1634 Op.getOperand(10),
1635 Op.getOperand(11),
1636 Op.getOperand(12),
1637 Op.getOperand(13),
1638 Op.getOperand(14)
1639 };
1640
1641 EVT VT = Op.getOperand(3).getValueType();
1642
1643 MachineMemOperand *MMO = MF.getMachineMemOperand(
1644 MachinePointerInfo(),
1645 MachineMemOperand::MOStore,
1646 VT.getStoreSize(), 4);
1647 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1648 Op->getVTList(), Ops, VT, MMO);
1649 }
1650 default:
1651 return SDValue();
1652 }
1653}
1654
Tom Stellard81d871d2013-11-13 23:36:50 +00001655SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1656 SDLoc DL(Op);
1657 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault6dfda962016-02-10 18:21:39 +00001658 ISD::LoadExtType ExtType = Load->getExtensionType();
Matt Arsenaulta1436412016-02-10 18:21:45 +00001659 EVT MemVT = Load->getMemoryVT();
Matt Arsenault6dfda962016-02-10 18:21:39 +00001660
Matt Arsenaulta1436412016-02-10 18:21:45 +00001661 if (ExtType == ISD::NON_EXTLOAD && MemVT.getSizeInBits() < 32) {
1662 assert(MemVT == MVT::i1 && "Only i1 non-extloads expected");
Matt Arsenault6dfda962016-02-10 18:21:39 +00001663 // FIXME: Copied from PPC
1664 // First, load into 32 bits, then truncate to 1 bit.
1665
1666 SDValue Chain = Load->getChain();
1667 SDValue BasePtr = Load->getBasePtr();
1668 MachineMemOperand *MMO = Load->getMemOperand();
1669
1670 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
1671 BasePtr, MVT::i8, MMO);
1672
1673 SDValue Ops[] = {
Matt Arsenaulta1436412016-02-10 18:21:45 +00001674 DAG.getNode(ISD::TRUNCATE, DL, MemVT, NewLD),
Matt Arsenault6dfda962016-02-10 18:21:39 +00001675 NewLD.getValue(1)
1676 };
1677
1678 return DAG.getMergeValues(Ops, DL);
1679 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001680
Matt Arsenaulta1436412016-02-10 18:21:45 +00001681 if (!MemVT.isVector())
1682 return SDValue();
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001683
Matt Arsenaulta1436412016-02-10 18:21:45 +00001684 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1685 "Custom lowering for non-i32 vectors hasn't been implemented.");
1686 unsigned NumElements = MemVT.getVectorNumElements();
1687 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001688
Matt Arsenaulta1436412016-02-10 18:21:45 +00001689 switch (Load->getAddressSpace()) {
1690 case AMDGPUAS::CONSTANT_ADDRESS:
1691 if (isMemOpUniform(Load))
1692 return SDValue();
1693 // Non-uniform loads will be selected to MUBUF instructions, so they
1694 // have the same legalization requires ments as global and private
1695 // loads.
1696 //
1697 // Fall-through
1698 case AMDGPUAS::GLOBAL_ADDRESS:
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001699 case AMDGPUAS::FLAT_ADDRESS:
1700 if (NumElements > 4)
Matt Arsenaulta1436412016-02-10 18:21:45 +00001701 return SplitVectorLoad(Op, DAG);
1702 // v4 loads are supported for private and global memory.
1703 return SDValue();
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001704 case AMDGPUAS::PRIVATE_ADDRESS: {
1705 // Depending on the setting of the private_element_size field in the
1706 // resource descriptor, we can only make private accesses up to a certain
1707 // size.
1708 switch (Subtarget->getMaxPrivateElementSize()) {
1709 case 4:
1710 return ScalarizeVectorLoad(Op, DAG);
1711 case 8:
1712 if (NumElements > 2)
1713 return SplitVectorLoad(Op, DAG);
1714 return SDValue();
1715 case 16:
1716 // Same as global/flat
1717 if (NumElements > 4)
1718 return SplitVectorLoad(Op, DAG);
1719 return SDValue();
1720 default:
1721 llvm_unreachable("unsupported private_element_size");
1722 }
1723 }
Matt Arsenaulta1436412016-02-10 18:21:45 +00001724 case AMDGPUAS::LOCAL_ADDRESS:
1725 // If properly aligned, if we split we might be able to use ds_read_b64.
1726 return SplitVectorLoad(Op, DAG);
1727 default:
1728 return SDValue();
Tom Stellarde9373602014-01-22 19:24:14 +00001729 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001730}
1731
Tom Stellard0ec134f2014-02-04 17:18:40 +00001732SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1733 if (Op.getValueType() != MVT::i64)
1734 return SDValue();
1735
1736 SDLoc DL(Op);
1737 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001738
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001739 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1740 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001741
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001742 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1743 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1744
1745 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1746 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001747
1748 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1749
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001750 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1751 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001752
1753 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1754
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001755 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1756 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001757}
1758
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001759// Catch division cases where we can use shortcuts with rcp and rsq
1760// instructions.
1761SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001762 SDLoc SL(Op);
1763 SDValue LHS = Op.getOperand(0);
1764 SDValue RHS = Op.getOperand(1);
1765 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001766 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001767
1768 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001769 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1770 CLHS->isExactlyValue(1.0)) {
1771 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1772 // the CI documentation has a worst case error of 1 ulp.
1773 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1774 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001775
1776 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001777 //
1778 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1779 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001780 if (RHS.getOpcode() == ISD::FSQRT)
1781 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1782
1783 // 1.0 / x -> rcp(x)
1784 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1785 }
1786 }
1787
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001788 if (Unsafe) {
1789 // Turn into multiply by the reciprocal.
1790 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00001791 SDNodeFlags Flags;
1792 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001793 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00001794 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001795 }
1796
1797 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001798}
1799
1800SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Ahmed Bougachaf8dfb472016-02-09 22:54:12 +00001801 if (SDValue FastLowered = LowerFastFDIV(Op, DAG))
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001802 return FastLowered;
1803
1804 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1805 // selection error for now rather than do something incorrect.
1806 if (Subtarget->hasFP32Denormals())
1807 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001808
1809 SDLoc SL(Op);
1810 SDValue LHS = Op.getOperand(0);
1811 SDValue RHS = Op.getOperand(1);
1812
1813 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1814
1815 const APFloat K0Val(BitsToFloat(0x6f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001816 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001817
1818 const APFloat K1Val(BitsToFloat(0x2f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001819 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001820
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001821 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001822
Mehdi Amini44ede332015-07-09 02:09:04 +00001823 EVT SetCCVT =
1824 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001825
1826 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1827
1828 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1829
Sanjay Patela2607012015-09-16 16:31:21 +00001830 // TODO: Should this propagate fast-math-flags?
1831
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001832 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1833
1834 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1835
1836 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1837
1838 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1839}
1840
1841SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001842 if (DAG.getTarget().Options.UnsafeFPMath)
1843 return LowerFastFDIV(Op, DAG);
1844
1845 SDLoc SL(Op);
1846 SDValue X = Op.getOperand(0);
1847 SDValue Y = Op.getOperand(1);
1848
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001849 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001850
1851 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
1852
1853 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
1854
1855 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
1856
1857 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
1858
1859 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
1860
1861 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
1862
1863 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
1864
1865 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
1866
1867 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
1868 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
1869
1870 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
1871 NegDivScale0, Mul, DivScale1);
1872
1873 SDValue Scale;
1874
1875 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1876 // Workaround a hardware bug on SI where the condition output from div_scale
1877 // is not usable.
1878
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001879 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001880
1881 // Figure out if the scale to use for div_fmas.
1882 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1883 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
1884 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
1885 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
1886
1887 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
1888 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
1889
1890 SDValue Scale0Hi
1891 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
1892 SDValue Scale1Hi
1893 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
1894
1895 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
1896 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
1897 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
1898 } else {
1899 Scale = DivScale1.getValue(1);
1900 }
1901
1902 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
1903 Fma4, Fma3, Mul, Scale);
1904
1905 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001906}
1907
1908SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1909 EVT VT = Op.getValueType();
1910
1911 if (VT == MVT::f32)
1912 return LowerFDIV32(Op, DAG);
1913
1914 if (VT == MVT::f64)
1915 return LowerFDIV64(Op, DAG);
1916
1917 llvm_unreachable("Unexpected type for fdiv");
1918}
1919
Tom Stellard81d871d2013-11-13 23:36:50 +00001920SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1921 SDLoc DL(Op);
1922 StoreSDNode *Store = cast<StoreSDNode>(Op);
1923 EVT VT = Store->getMemoryVT();
1924
Matt Arsenault95245662016-02-11 05:32:46 +00001925 if (VT == MVT::i1) {
1926 return DAG.getTruncStore(Store->getChain(), DL,
1927 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1928 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
Tom Stellardb02094e2014-07-21 15:45:01 +00001929 }
1930
Matt Arsenault95245662016-02-11 05:32:46 +00001931 assert(Store->getValue().getValueType().getScalarType() == MVT::i32);
Tom Stellard81d871d2013-11-13 23:36:50 +00001932
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001933 unsigned NumElements = VT.getVectorNumElements();
1934 switch (Store->getAddressSpace()) {
1935 case AMDGPUAS::GLOBAL_ADDRESS:
1936 case AMDGPUAS::FLAT_ADDRESS:
1937 if (NumElements > 4)
1938 return SplitVectorStore(Op, DAG);
1939 return SDValue();
1940 case AMDGPUAS::PRIVATE_ADDRESS: {
1941 switch (Subtarget->getMaxPrivateElementSize()) {
1942 case 4:
1943 return ScalarizeVectorStore(Op, DAG);
1944 case 8:
1945 if (NumElements > 2)
1946 return SplitVectorStore(Op, DAG);
1947 return SDValue();
1948 case 16:
1949 if (NumElements > 4)
1950 return SplitVectorStore(Op, DAG);
1951 return SDValue();
1952 default:
1953 llvm_unreachable("unsupported private_element_size");
1954 }
1955 }
1956 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenault95245662016-02-11 05:32:46 +00001957 // If properly aligned, if we split we might be able to use ds_write_b64.
1958 return SplitVectorStore(Op, DAG);
Matt Arsenaultf2ddbf02016-02-13 04:18:53 +00001959 default:
1960 llvm_unreachable("unhandled address space");
Matt Arsenault95245662016-02-11 05:32:46 +00001961 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001962}
1963
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001964SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001965 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001966 EVT VT = Op.getValueType();
1967 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00001968 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001969 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
1970 DAG.getNode(ISD::FMUL, DL, VT, Arg,
1971 DAG.getConstantFP(0.5/M_PI, DL,
1972 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001973
1974 switch (Op.getOpcode()) {
1975 case ISD::FCOS:
1976 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1977 case ISD::FSIN:
1978 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1979 default:
1980 llvm_unreachable("Wrong trig opcode");
1981 }
1982}
1983
Tom Stellard75aadc22012-12-11 21:25:42 +00001984//===----------------------------------------------------------------------===//
1985// Custom DAG optimizations
1986//===----------------------------------------------------------------------===//
1987
Matt Arsenault364a6742014-06-11 17:50:44 +00001988SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00001989 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00001990 EVT VT = N->getValueType(0);
1991 EVT ScalarVT = VT.getScalarType();
1992 if (ScalarVT != MVT::f32)
1993 return SDValue();
1994
1995 SelectionDAG &DAG = DCI.DAG;
1996 SDLoc DL(N);
1997
1998 SDValue Src = N->getOperand(0);
1999 EVT SrcVT = Src.getValueType();
2000
2001 // TODO: We could try to match extracting the higher bytes, which would be
2002 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
2003 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
2004 // about in practice.
2005 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
2006 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
2007 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
2008 DCI.AddToWorklist(Cvt.getNode());
2009 return Cvt;
2010 }
2011 }
2012
2013 // We are primarily trying to catch operations on illegal vector types
2014 // before they are expanded.
2015 // For scalars, we can use the more flexible method of checking masked bits
2016 // after legalization.
2017 if (!DCI.isBeforeLegalize() ||
2018 !SrcVT.isVector() ||
2019 SrcVT.getVectorElementType() != MVT::i8) {
2020 return SDValue();
2021 }
2022
2023 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
2024
2025 // Weird sized vectors are a pain to handle, but we know 3 is really the same
2026 // size as 4.
2027 unsigned NElts = SrcVT.getVectorNumElements();
2028 if (!SrcVT.isSimple() && NElts != 3)
2029 return SDValue();
2030
2031 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
2032 // prevent a mess from expanding to v4i32 and repacking.
2033 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
2034 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
2035 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
2036 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00002037 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00002038
2039 unsigned AS = Load->getAddressSpace();
2040 unsigned Align = Load->getAlignment();
2041 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +00002042 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
Matt Arsenaulte6986632015-01-14 01:35:22 +00002043
2044 // Don't try to replace the load if we have to expand it due to alignment
2045 // problems. Otherwise we will end up scalarizing the load, and trying to
2046 // repack into the vector for no real reason.
2047 if (Align < ABIAlignment &&
2048 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
2049 return SDValue();
2050 }
2051
Matt Arsenault364a6742014-06-11 17:50:44 +00002052 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
2053 Load->getChain(),
2054 Load->getBasePtr(),
2055 LoadVT,
2056 Load->getMemOperand());
2057
2058 // Make sure successors of the original load stay after it by updating
2059 // them to use the new Chain.
2060 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
2061
2062 SmallVector<SDValue, 4> Elts;
2063 if (RegVT.isVector())
2064 DAG.ExtractVectorElements(NewLoad, Elts);
2065 else
2066 Elts.push_back(NewLoad);
2067
2068 SmallVector<SDValue, 4> Ops;
2069
2070 unsigned EltIdx = 0;
2071 for (SDValue Elt : Elts) {
2072 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
2073 for (unsigned I = 0; I < ComponentsInElt; ++I) {
2074 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
2075 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
2076 DCI.AddToWorklist(Cvt.getNode());
2077 Ops.push_back(Cvt);
2078 }
2079
2080 ++EltIdx;
2081 }
2082
2083 assert(Ops.size() == NElts);
2084
2085 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
2086 }
2087
2088 return SDValue();
2089}
2090
Eric Christopher6c5b5112015-03-11 18:43:21 +00002091/// \brief Return true if the given offset Size in bytes can be folded into
2092/// the immediate offsets of a memory instruction for the given address space.
2093static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
2094 const AMDGPUSubtarget &STI) {
2095 switch (AS) {
2096 case AMDGPUAS::GLOBAL_ADDRESS: {
2097 // MUBUF instructions a 12-bit offset in bytes.
2098 return isUInt<12>(OffsetSize);
2099 }
2100 case AMDGPUAS::CONSTANT_ADDRESS: {
2101 // SMRD instructions have an 8-bit offset in dwords on SI and
2102 // a 20-bit offset in bytes on VI.
2103 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
2104 return isUInt<20>(OffsetSize);
2105 else
2106 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
2107 }
2108 case AMDGPUAS::LOCAL_ADDRESS:
2109 case AMDGPUAS::REGION_ADDRESS: {
2110 // The single offset versions have a 16-bit offset in bytes.
2111 return isUInt<16>(OffsetSize);
2112 }
2113 case AMDGPUAS::PRIVATE_ADDRESS:
2114 // Indirect register addressing does not use any offsets.
2115 default:
2116 return 0;
2117 }
2118}
2119
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002120// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
2121
2122// This is a variant of
2123// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
2124//
2125// The normal DAG combiner will do this, but only if the add has one use since
2126// that would increase the number of instructions.
2127//
2128// This prevents us from seeing a constant offset that can be folded into a
2129// memory instruction's addressing mode. If we know the resulting add offset of
2130// a pointer can be folded into an addressing offset, we can replace the pointer
2131// operand with the add of new constant offset. This eliminates one of the uses,
2132// and may allow the remaining use to also be simplified.
2133//
2134SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
2135 unsigned AddrSpace,
2136 DAGCombinerInfo &DCI) const {
2137 SDValue N0 = N->getOperand(0);
2138 SDValue N1 = N->getOperand(1);
2139
2140 if (N0.getOpcode() != ISD::ADD)
2141 return SDValue();
2142
2143 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
2144 if (!CN1)
2145 return SDValue();
2146
2147 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2148 if (!CAdd)
2149 return SDValue();
2150
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002151 // If the resulting offset is too large, we can't fold it into the addressing
2152 // mode offset.
2153 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Eric Christopher6c5b5112015-03-11 18:43:21 +00002154 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002155 return SDValue();
2156
2157 SelectionDAG &DAG = DCI.DAG;
2158 SDLoc SL(N);
2159 EVT VT = N->getValueType(0);
2160
2161 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002162 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002163
2164 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
2165}
2166
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002167SDValue SITargetLowering::performAndCombine(SDNode *N,
2168 DAGCombinerInfo &DCI) const {
2169 if (DCI.isBeforeLegalize())
2170 return SDValue();
2171
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002172 if (SDValue Base = AMDGPUTargetLowering::performAndCombine(N, DCI))
2173 return Base;
2174
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002175 SelectionDAG &DAG = DCI.DAG;
2176
2177 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
2178 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
2179 SDValue LHS = N->getOperand(0);
2180 SDValue RHS = N->getOperand(1);
2181
2182 if (LHS.getOpcode() == ISD::SETCC &&
2183 RHS.getOpcode() == ISD::SETCC) {
2184 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
2185 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
2186
2187 SDValue X = LHS.getOperand(0);
2188 SDValue Y = RHS.getOperand(0);
2189 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
2190 return SDValue();
2191
2192 if (LCC == ISD::SETO) {
2193 if (X != LHS.getOperand(1))
2194 return SDValue();
2195
2196 if (RCC == ISD::SETUNE) {
2197 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
2198 if (!C1 || !C1->isInfinity() || C1->isNegative())
2199 return SDValue();
2200
2201 const uint32_t Mask = SIInstrFlags::N_NORMAL |
2202 SIInstrFlags::N_SUBNORMAL |
2203 SIInstrFlags::N_ZERO |
2204 SIInstrFlags::P_ZERO |
2205 SIInstrFlags::P_SUBNORMAL |
2206 SIInstrFlags::P_NORMAL;
2207
2208 static_assert(((~(SIInstrFlags::S_NAN |
2209 SIInstrFlags::Q_NAN |
2210 SIInstrFlags::N_INFINITY |
2211 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
2212 "mask not equal");
2213
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002214 SDLoc DL(N);
2215 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2216 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002217 }
2218 }
2219 }
2220
2221 return SDValue();
2222}
2223
Matt Arsenaultf2290332015-01-06 23:00:39 +00002224SDValue SITargetLowering::performOrCombine(SDNode *N,
2225 DAGCombinerInfo &DCI) const {
2226 SelectionDAG &DAG = DCI.DAG;
2227 SDValue LHS = N->getOperand(0);
2228 SDValue RHS = N->getOperand(1);
2229
2230 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
2231 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
2232 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
2233 SDValue Src = LHS.getOperand(0);
2234 if (Src != RHS.getOperand(0))
2235 return SDValue();
2236
2237 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
2238 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
2239 if (!CLHS || !CRHS)
2240 return SDValue();
2241
2242 // Only 10 bits are used.
2243 static const uint32_t MaxMask = 0x3ff;
2244
2245 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002246 SDLoc DL(N);
2247 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
2248 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00002249 }
2250
2251 return SDValue();
2252}
2253
2254SDValue SITargetLowering::performClassCombine(SDNode *N,
2255 DAGCombinerInfo &DCI) const {
2256 SelectionDAG &DAG = DCI.DAG;
2257 SDValue Mask = N->getOperand(1);
2258
2259 // fp_class x, 0 -> false
2260 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
2261 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002262 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002263 }
2264
2265 return SDValue();
2266}
2267
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002268static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
2269 switch (Opc) {
2270 case ISD::FMAXNUM:
2271 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002272 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002273 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002274 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002275 return AMDGPUISD::UMAX3;
2276 case ISD::FMINNUM:
2277 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002278 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002279 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002280 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002281 return AMDGPUISD::UMIN3;
2282 default:
2283 llvm_unreachable("Not a min/max opcode");
2284 }
2285}
2286
Matt Arsenaultf639c322016-01-28 20:53:42 +00002287static SDValue performIntMed3ImmCombine(SelectionDAG &DAG,
2288 SDLoc SL,
2289 SDValue Op0,
2290 SDValue Op1,
2291 bool Signed) {
2292 ConstantSDNode *K1 = dyn_cast<ConstantSDNode>(Op1);
2293 if (!K1)
2294 return SDValue();
2295
2296 ConstantSDNode *K0 = dyn_cast<ConstantSDNode>(Op0.getOperand(1));
2297 if (!K0)
2298 return SDValue();
2299
2300
2301 if (Signed) {
2302 if (K0->getAPIntValue().sge(K1->getAPIntValue()))
2303 return SDValue();
2304 } else {
2305 if (K0->getAPIntValue().uge(K1->getAPIntValue()))
2306 return SDValue();
2307 }
2308
2309 EVT VT = K0->getValueType(0);
2310 return DAG.getNode(Signed ? AMDGPUISD::SMED3 : AMDGPUISD::UMED3, SL, VT,
2311 Op0.getOperand(0), SDValue(K0, 0), SDValue(K1, 0));
2312}
2313
2314static bool isKnownNeverSNan(SelectionDAG &DAG, SDValue Op) {
2315 if (!DAG.getTargetLoweringInfo().hasFloatingPointExceptions())
2316 return true;
2317
2318 return DAG.isKnownNeverNaN(Op);
2319}
2320
2321static SDValue performFPMed3ImmCombine(SelectionDAG &DAG,
2322 SDLoc SL,
2323 SDValue Op0,
2324 SDValue Op1) {
2325 ConstantFPSDNode *K1 = dyn_cast<ConstantFPSDNode>(Op1);
2326 if (!K1)
2327 return SDValue();
2328
2329 ConstantFPSDNode *K0 = dyn_cast<ConstantFPSDNode>(Op0.getOperand(1));
2330 if (!K0)
2331 return SDValue();
2332
2333 // Ordered >= (although NaN inputs should have folded away by now).
2334 APFloat::cmpResult Cmp = K0->getValueAPF().compare(K1->getValueAPF());
2335 if (Cmp == APFloat::cmpGreaterThan)
2336 return SDValue();
2337
2338 // This isn't safe with signaling NaNs because in IEEE mode, min/max on a
2339 // signaling NaN gives a quiet NaN. The quiet NaN input to the min would then
2340 // give the other result, which is different from med3 with a NaN input.
2341 SDValue Var = Op0.getOperand(0);
2342 if (!isKnownNeverSNan(DAG, Var))
2343 return SDValue();
2344
2345 return DAG.getNode(AMDGPUISD::FMED3, SL, K0->getValueType(0),
2346 Var, SDValue(K0, 0), SDValue(K1, 0));
2347}
2348
2349SDValue SITargetLowering::performMinMaxCombine(SDNode *N,
2350 DAGCombinerInfo &DCI) const {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002351 SelectionDAG &DAG = DCI.DAG;
2352
2353 unsigned Opc = N->getOpcode();
2354 SDValue Op0 = N->getOperand(0);
2355 SDValue Op1 = N->getOperand(1);
2356
2357 // Only do this if the inner op has one use since this will just increases
2358 // register pressure for no benefit.
2359
Matt Arsenault5b39b342016-01-28 20:53:48 +00002360 if (Opc != AMDGPUISD::FMIN_LEGACY && Opc != AMDGPUISD::FMAX_LEGACY) {
2361 // max(max(a, b), c) -> max3(a, b, c)
2362 // min(min(a, b), c) -> min3(a, b, c)
2363 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
2364 SDLoc DL(N);
2365 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2366 DL,
2367 N->getValueType(0),
2368 Op0.getOperand(0),
2369 Op0.getOperand(1),
2370 Op1);
2371 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002372
Matt Arsenault5b39b342016-01-28 20:53:48 +00002373 // Try commuted.
2374 // max(a, max(b, c)) -> max3(a, b, c)
2375 // min(a, min(b, c)) -> min3(a, b, c)
2376 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
2377 SDLoc DL(N);
2378 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
2379 DL,
2380 N->getValueType(0),
2381 Op0,
2382 Op1.getOperand(0),
2383 Op1.getOperand(1));
2384 }
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002385 }
2386
Matt Arsenaultf639c322016-01-28 20:53:42 +00002387 // min(max(x, K0), K1), K0 < K1 -> med3(x, K0, K1)
2388 if (Opc == ISD::SMIN && Op0.getOpcode() == ISD::SMAX && Op0.hasOneUse()) {
2389 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, true))
2390 return Med3;
2391 }
2392
2393 if (Opc == ISD::UMIN && Op0.getOpcode() == ISD::UMAX && Op0.hasOneUse()) {
2394 if (SDValue Med3 = performIntMed3ImmCombine(DAG, SDLoc(N), Op0, Op1, false))
2395 return Med3;
2396 }
2397
2398 // fminnum(fmaxnum(x, K0), K1), K0 < K1 && !is_snan(x) -> fmed3(x, K0, K1)
Matt Arsenault5b39b342016-01-28 20:53:48 +00002399 if (((Opc == ISD::FMINNUM && Op0.getOpcode() == ISD::FMAXNUM) ||
2400 (Opc == AMDGPUISD::FMIN_LEGACY &&
2401 Op0.getOpcode() == AMDGPUISD::FMAX_LEGACY)) &&
Matt Arsenaultf639c322016-01-28 20:53:42 +00002402 N->getValueType(0) == MVT::f32 && Op0.hasOneUse()) {
2403 if (SDValue Res = performFPMed3ImmCombine(DAG, SDLoc(N), Op0, Op1))
2404 return Res;
2405 }
2406
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002407 return SDValue();
2408}
2409
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002410SDValue SITargetLowering::performSetCCCombine(SDNode *N,
2411 DAGCombinerInfo &DCI) const {
2412 SelectionDAG &DAG = DCI.DAG;
2413 SDLoc SL(N);
2414
2415 SDValue LHS = N->getOperand(0);
2416 SDValue RHS = N->getOperand(1);
2417 EVT VT = LHS.getValueType();
2418
2419 if (VT != MVT::f32 && VT != MVT::f64)
2420 return SDValue();
2421
2422 // Match isinf pattern
2423 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
2424 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
2425 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
2426 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2427 if (!CRHS)
2428 return SDValue();
2429
2430 const APFloat &APF = CRHS->getValueAPF();
2431 if (APF.isInfinity() && !APF.isNegative()) {
2432 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002433 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
2434 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002435 }
2436 }
2437
2438 return SDValue();
2439}
2440
Tom Stellard75aadc22012-12-11 21:25:42 +00002441SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
2442 DAGCombinerInfo &DCI) const {
2443 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00002444 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00002445
2446 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00002447 default:
2448 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00002449 case ISD::SETCC:
2450 return performSetCCCombine(N, DCI);
Matt Arsenault5b39b342016-01-28 20:53:48 +00002451 case ISD::FMAXNUM:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002452 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00002453 case ISD::SMAX:
2454 case ISD::SMIN:
2455 case ISD::UMAX:
Matt Arsenault5b39b342016-01-28 20:53:48 +00002456 case ISD::UMIN:
2457 case AMDGPUISD::FMIN_LEGACY:
2458 case AMDGPUISD::FMAX_LEGACY: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002459 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00002460 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002461 getTargetMachine().getOptLevel() > CodeGenOpt::None)
Matt Arsenaultf639c322016-01-28 20:53:42 +00002462 return performMinMaxCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00002463 break;
2464 }
Matt Arsenault364a6742014-06-11 17:50:44 +00002465
2466 case AMDGPUISD::CVT_F32_UBYTE0:
2467 case AMDGPUISD::CVT_F32_UBYTE1:
2468 case AMDGPUISD::CVT_F32_UBYTE2:
2469 case AMDGPUISD::CVT_F32_UBYTE3: {
2470 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
2471
2472 SDValue Src = N->getOperand(0);
2473 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
2474
2475 APInt KnownZero, KnownOne;
2476 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2477 !DCI.isBeforeLegalizeOps());
2478 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2479 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
2480 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
2481 DCI.CommitTargetLoweringOpt(TLO);
2482 }
2483
2484 break;
2485 }
2486
2487 case ISD::UINT_TO_FP: {
2488 return performUCharToFloatCombine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002489 }
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002490 case ISD::FADD: {
2491 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2492 break;
2493
2494 EVT VT = N->getValueType(0);
2495 if (VT != MVT::f32)
2496 break;
2497
Matt Arsenault8d630032015-02-20 22:10:41 +00002498 // Only do this if we are not trying to support denormals. v_mad_f32 does
2499 // not support denormals ever.
2500 if (Subtarget->hasFP32Denormals())
2501 break;
2502
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002503 SDValue LHS = N->getOperand(0);
2504 SDValue RHS = N->getOperand(1);
2505
2506 // These should really be instruction patterns, but writing patterns with
2507 // source modiifiers is a pain.
2508
2509 // fadd (fadd (a, a), b) -> mad 2.0, a, b
2510 if (LHS.getOpcode() == ISD::FADD) {
2511 SDValue A = LHS.getOperand(0);
2512 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002513 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002514 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002515 }
2516 }
2517
2518 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
2519 if (RHS.getOpcode() == ISD::FADD) {
2520 SDValue A = RHS.getOperand(0);
2521 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002522 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002523 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002524 }
2525 }
2526
Matt Arsenault8d630032015-02-20 22:10:41 +00002527 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00002528 }
Matt Arsenault8675db12014-08-29 16:01:14 +00002529 case ISD::FSUB: {
2530 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2531 break;
2532
2533 EVT VT = N->getValueType(0);
2534
2535 // Try to get the fneg to fold into the source modifier. This undoes generic
2536 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00002537 //
2538 // Only do this if we are not trying to support denormals. v_mad_f32 does
2539 // not support denormals ever.
2540 if (VT == MVT::f32 &&
2541 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00002542 SDValue LHS = N->getOperand(0);
2543 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002544 if (LHS.getOpcode() == ISD::FADD) {
2545 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
2546
2547 SDValue A = LHS.getOperand(0);
2548 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002549 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002550 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
2551
Matt Arsenault8d630032015-02-20 22:10:41 +00002552 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002553 }
2554 }
2555
2556 if (RHS.getOpcode() == ISD::FADD) {
2557 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
2558
2559 SDValue A = RHS.getOperand(0);
2560 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002561 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00002562 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00002563 }
2564 }
Matt Arsenault8d630032015-02-20 22:10:41 +00002565
2566 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00002567 }
2568
2569 break;
2570 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002571 case ISD::LOAD:
2572 case ISD::STORE:
2573 case ISD::ATOMIC_LOAD:
2574 case ISD::ATOMIC_STORE:
2575 case ISD::ATOMIC_CMP_SWAP:
2576 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2577 case ISD::ATOMIC_SWAP:
2578 case ISD::ATOMIC_LOAD_ADD:
2579 case ISD::ATOMIC_LOAD_SUB:
2580 case ISD::ATOMIC_LOAD_AND:
2581 case ISD::ATOMIC_LOAD_OR:
2582 case ISD::ATOMIC_LOAD_XOR:
2583 case ISD::ATOMIC_LOAD_NAND:
2584 case ISD::ATOMIC_LOAD_MIN:
2585 case ISD::ATOMIC_LOAD_MAX:
2586 case ISD::ATOMIC_LOAD_UMIN:
2587 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
2588 if (DCI.isBeforeLegalize())
2589 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002590
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002591 MemSDNode *MemNode = cast<MemSDNode>(N);
2592 SDValue Ptr = MemNode->getBasePtr();
2593
2594 // TODO: We could also do this for multiplies.
2595 unsigned AS = MemNode->getAddressSpace();
2596 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
2597 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
2598 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002599 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002600
2601 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
2602 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
2603 }
2604 }
2605 break;
2606 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002607 case ISD::AND:
2608 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002609 case ISD::OR:
2610 return performOrCombine(N, DCI);
2611 case AMDGPUISD::FP_CLASS:
2612 return performClassCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002613 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002614 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00002615}
Christian Konigd910b7d2013-02-26 17:52:16 +00002616
Christian Konigf82901a2013-02-26 17:52:23 +00002617/// \brief Analyze the possible immediate value Op
2618///
2619/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
2620/// and the immediate value if it's a literal immediate
2621int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
2622
Eric Christopher7792e322015-01-30 23:24:40 +00002623 const SIInstrInfo *TII =
2624 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00002625
Tom Stellardedbf1eb2013-04-05 23:31:20 +00002626 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00002627 if (TII->isInlineConstant(Node->getAPIntValue()))
2628 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00002629
Matt Arsenault11a4d672015-02-13 19:05:03 +00002630 uint64_t Val = Node->getZExtValue();
2631 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00002632 }
2633
2634 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
2635 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
2636 return 0;
2637
2638 if (Node->getValueType(0) == MVT::f32)
2639 return FloatToBits(Node->getValueAPF().convertToFloat());
2640
2641 return -1;
2642 }
2643
2644 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00002645}
2646
Christian Konig8e06e2a2013-04-10 08:39:08 +00002647/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00002648static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00002649 switch (Idx) {
2650 default: return 0;
2651 case AMDGPU::sub0: return 0;
2652 case AMDGPU::sub1: return 1;
2653 case AMDGPU::sub2: return 2;
2654 case AMDGPU::sub3: return 3;
2655 }
2656}
2657
2658/// \brief Adjust the writemask of MIMG instructions
2659void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
2660 SelectionDAG &DAG) const {
2661 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00002662 unsigned Lane = 0;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002663 unsigned DmaskIdx = (Node->getNumOperands() - Node->getNumValues() == 9) ? 2 : 3;
2664 unsigned OldDmask = Node->getConstantOperandVal(DmaskIdx);
Tom Stellard54774e52013-10-23 02:53:47 +00002665 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002666
2667 // Try to figure out the used register components
2668 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
2669 I != E; ++I) {
2670
2671 // Abort if we can't understand the usage
2672 if (!I->isMachineOpcode() ||
2673 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
2674 return;
2675
Tom Stellard54774e52013-10-23 02:53:47 +00002676 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
2677 // Note that subregs are packed, i.e. Lane==0 is the first bit set
2678 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
2679 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00002680 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00002681
Tom Stellard54774e52013-10-23 02:53:47 +00002682 // Set which texture component corresponds to the lane.
2683 unsigned Comp;
2684 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
2685 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00002686 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00002687 Dmask &= ~(1 << Comp);
2688 }
2689
Christian Konig8e06e2a2013-04-10 08:39:08 +00002690 // Abort if we have more than one user per component
2691 if (Users[Lane])
2692 return;
2693
2694 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00002695 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002696 }
2697
Tom Stellard54774e52013-10-23 02:53:47 +00002698 // Abort if there's no change
2699 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00002700 return;
2701
2702 // Adjust the writemask in the node
2703 std::vector<SDValue> Ops;
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002704 Ops.insert(Ops.end(), Node->op_begin(), Node->op_begin() + DmaskIdx);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002705 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002706 Ops.insert(Ops.end(), Node->op_begin() + DmaskIdx + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00002707 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002708
Christian Konig8b1ed282013-04-10 08:39:16 +00002709 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00002710 // (if NewDmask has only one bit set...)
2711 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002712 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
2713 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00002714 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002715 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00002716 SDValue(Node, 0), RC);
2717 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
2718 return;
2719 }
2720
Christian Konig8e06e2a2013-04-10 08:39:08 +00002721 // Update the users of the node with the new indices
2722 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
2723
2724 SDNode *User = Users[i];
2725 if (!User)
2726 continue;
2727
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002728 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002729 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
2730
2731 switch (Idx) {
2732 default: break;
2733 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
2734 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
2735 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
2736 }
2737 }
2738}
2739
Tom Stellardc98ee202015-07-16 19:40:07 +00002740static bool isFrameIndexOp(SDValue Op) {
2741 if (Op.getOpcode() == ISD::AssertZext)
2742 Op = Op.getOperand(0);
2743
2744 return isa<FrameIndexSDNode>(Op);
2745}
2746
Tom Stellard3457a842014-10-09 19:06:00 +00002747/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
2748/// with frame index operands.
2749/// LLVM assumes that inputs are to these instructions are registers.
2750void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
2751 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002752
2753 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00002754 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00002755 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00002756 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002757 continue;
2758 }
2759
Tom Stellard3457a842014-10-09 19:06:00 +00002760 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002761 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00002762 Node->getOperand(i).getValueType(),
2763 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002764 }
2765
Tom Stellard3457a842014-10-09 19:06:00 +00002766 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002767}
2768
Matt Arsenault08d84942014-06-03 23:06:13 +00002769/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00002770SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
2771 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002772 const SIInstrInfo *TII =
2773 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00002774 unsigned Opcode = Node->getMachineOpcode();
Christian Konig8e06e2a2013-04-10 08:39:08 +00002775
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00002776 if (TII->isMIMG(Opcode) && !TII->get(Opcode).mayStore())
Christian Konig8e06e2a2013-04-10 08:39:08 +00002777 adjustWritemask(Node, DAG);
2778
Nicolai Haehnlef2c64db2016-02-18 16:44:18 +00002779 if (Opcode == AMDGPU::INSERT_SUBREG ||
2780 Opcode == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002781 legalizeTargetIndependentNode(Node, DAG);
2782 return Node;
2783 }
Tom Stellard654d6692015-01-08 15:08:17 +00002784 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002785}
Christian Konig8b1ed282013-04-10 08:39:16 +00002786
2787/// \brief Assign the register class depending on the number of
2788/// bits set in the writemask
2789void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
2790 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002791 const SIInstrInfo *TII =
2792 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002793
Tom Stellarda99ada52014-11-21 22:31:44 +00002794 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00002795
2796 if (TII->isVOP3(MI->getOpcode())) {
2797 // Make sure constant bus requirements are respected.
2798 TII->legalizeOperandsVOP3(MRI, MI);
2799 return;
2800 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00002801
Matt Arsenault3add6432015-10-20 04:35:43 +00002802 if (TII->isMIMG(*MI)) {
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002803 unsigned VReg = MI->getOperand(0).getReg();
Nikolay Haustov2f684f12016-02-26 09:51:05 +00002804 unsigned DmaskIdx = MI->getNumOperands() == 12 ? 3 : 4;
2805 unsigned Writemask = MI->getOperand(DmaskIdx).getImm();
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002806 unsigned BitsSet = 0;
2807 for (unsigned i = 0; i < 4; ++i)
2808 BitsSet += Writemask & (1 << i) ? 1 : 0;
2809
2810 const TargetRegisterClass *RC;
2811 switch (BitsSet) {
2812 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002813 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002814 case 2: RC = &AMDGPU::VReg_64RegClass; break;
2815 case 3: RC = &AMDGPU::VReg_96RegClass; break;
2816 }
2817
2818 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
2819 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002820 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00002821 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00002822 }
2823
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002824 // Replace unused atomics with the no return version.
2825 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
2826 if (NoRetAtomicOp != -1) {
2827 if (!Node->hasAnyUseOfValue(0)) {
2828 MI->setDesc(TII->get(NoRetAtomicOp));
2829 MI->RemoveOperand(0);
2830 }
2831
2832 return;
2833 }
Christian Konig8b1ed282013-04-10 08:39:16 +00002834}
Tom Stellard0518ff82013-06-03 17:39:58 +00002835
Matt Arsenault485defe2014-11-05 19:01:17 +00002836static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002837 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00002838 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
2839}
2840
2841MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
2842 SDLoc DL,
2843 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002844 const SIInstrInfo *TII =
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002845 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00002846
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002847 // Build the half of the subregister with the constants before building the
2848 // full 128-bit register. If we are building multiple resource descriptors,
2849 // this will allow CSEing of the 2-component register.
2850 const SDValue Ops0[] = {
2851 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
2852 buildSMovImm32(DAG, DL, 0),
2853 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
2854 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
2855 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
2856 };
Matt Arsenault485defe2014-11-05 19:01:17 +00002857
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002858 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
2859 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00002860
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002861 // Combine the constants and the pointer.
2862 const SDValue Ops1[] = {
2863 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
2864 Ptr,
2865 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
2866 SubRegHi,
2867 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
2868 };
Matt Arsenault485defe2014-11-05 19:01:17 +00002869
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002870 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00002871}
2872
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002873/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002874/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
2875/// of the resource descriptor) to create an offset, which is added to
2876/// the resource pointer.
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002877MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
2878 SDLoc DL,
2879 SDValue Ptr,
2880 uint32_t RsrcDword1,
2881 uint64_t RsrcDword2And3) const {
2882 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
2883 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
2884 if (RsrcDword1) {
2885 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002886 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
2887 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002888 }
2889
2890 SDValue DataLo = buildSMovImm32(DAG, DL,
2891 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
2892 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
2893
2894 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002895 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002896 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002897 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002898 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002899 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002900 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002901 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002902 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002903 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002904 };
2905
2906 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2907}
2908
Tom Stellard94593ee2013-06-03 17:40:18 +00002909SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2910 const TargetRegisterClass *RC,
2911 unsigned Reg, EVT VT) const {
2912 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2913
2914 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2915 cast<RegisterSDNode>(VReg)->getReg(), VT);
2916}
Tom Stellardd7e6f132015-04-08 01:09:26 +00002917
2918//===----------------------------------------------------------------------===//
2919// SI Inline Assembly Support
2920//===----------------------------------------------------------------------===//
2921
2922std::pair<unsigned, const TargetRegisterClass *>
2923SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002924 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00002925 MVT VT) const {
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002926
2927 if (Constraint.size() == 1) {
2928 switch (Constraint[0]) {
2929 case 's':
2930 case 'r':
2931 switch (VT.getSizeInBits()) {
2932 default:
2933 return std::make_pair(0U, nullptr);
2934 case 32:
Tom Stellardd7e6f132015-04-08 01:09:26 +00002935 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002936 case 64:
2937 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
2938 case 128:
2939 return std::make_pair(0U, &AMDGPU::SReg_128RegClass);
2940 case 256:
2941 return std::make_pair(0U, &AMDGPU::SReg_256RegClass);
2942 }
2943
2944 case 'v':
2945 switch (VT.getSizeInBits()) {
2946 default:
2947 return std::make_pair(0U, nullptr);
2948 case 32:
2949 return std::make_pair(0U, &AMDGPU::VGPR_32RegClass);
2950 case 64:
2951 return std::make_pair(0U, &AMDGPU::VReg_64RegClass);
2952 case 96:
2953 return std::make_pair(0U, &AMDGPU::VReg_96RegClass);
2954 case 128:
2955 return std::make_pair(0U, &AMDGPU::VReg_128RegClass);
2956 case 256:
2957 return std::make_pair(0U, &AMDGPU::VReg_256RegClass);
2958 case 512:
2959 return std::make_pair(0U, &AMDGPU::VReg_512RegClass);
2960 }
Tom Stellardd7e6f132015-04-08 01:09:26 +00002961 }
2962 }
2963
2964 if (Constraint.size() > 1) {
2965 const TargetRegisterClass *RC = nullptr;
2966 if (Constraint[1] == 'v') {
2967 RC = &AMDGPU::VGPR_32RegClass;
2968 } else if (Constraint[1] == 's') {
2969 RC = &AMDGPU::SGPR_32RegClass;
2970 }
2971
2972 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00002973 uint32_t Idx;
2974 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
2975 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00002976 return std::make_pair(RC->getRegister(Idx), RC);
2977 }
2978 }
2979 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
2980}
Tom Stellardb3c3bda2015-12-10 02:12:53 +00002981
2982SITargetLowering::ConstraintType
2983SITargetLowering::getConstraintType(StringRef Constraint) const {
2984 if (Constraint.size() == 1) {
2985 switch (Constraint[0]) {
2986 default: break;
2987 case 's':
2988 case 'v':
2989 return C_RegisterClass;
2990 }
2991 }
2992 return TargetLowering::getConstraintType(Constraint);
2993}