blob: 28694103c713df5bae5f23c78ca3ed88bbc92d2d [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"
Christian Konig2c8f6d52013-03-07 09:03:52 +000029#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000030#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/MachineRegisterInfo.h"
32#include "llvm/CodeGen/SelectionDAG.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000033#include "llvm/IR/Function.h"
Matt Arsenault364a6742014-06-11 17:50:44 +000034#include "llvm/ADT/SmallString.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000035
36using namespace llvm;
37
Eric Christopher7792e322015-01-30 23:24:40 +000038SITargetLowering::SITargetLowering(TargetMachine &TM,
39 const AMDGPUSubtarget &STI)
40 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000041 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000042 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000043
Christian Konig2214f142013-03-07 09:03:38 +000044 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
45 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
46
Tom Stellard334b29c2014-04-17 21:00:09 +000047 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000048 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000049
Tom Stellard436780b2014-05-15 14:41:57 +000050 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
51 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
52 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000053
Tom Stellard436780b2014-05-15 14:41:57 +000054 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
55 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000056
Tom Stellardf0a21072014-11-18 20:39:39 +000057 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000058 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
59
Tom Stellardf0a21072014-11-18 20:39:39 +000060 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000061 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000062
Eric Christopher23a3a7c2015-02-26 00:00:24 +000063 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000064
Christian Konig2989ffc2013-03-18 11:34:16 +000065 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
66 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
67 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
68 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
69
Tom Stellard75aadc22012-12-11 21:25:42 +000070 setOperationAction(ISD::ADD, MVT::i32, Legal);
Matt Arsenaulte8d21462013-11-18 20:09:40 +000071 setOperationAction(ISD::ADDC, MVT::i32, Legal);
72 setOperationAction(ISD::ADDE, MVT::i32, Legal);
Matt Arsenaultb8b51532014-06-23 18:00:38 +000073 setOperationAction(ISD::SUBC, MVT::i32, Legal);
74 setOperationAction(ISD::SUBE, MVT::i32, Legal);
Aaron Watrydaabb202013-06-25 13:55:52 +000075
Matt Arsenaultad14ce82014-07-19 18:44:39 +000076 setOperationAction(ISD::FSIN, MVT::f32, Custom);
77 setOperationAction(ISD::FCOS, MVT::f32, Custom);
78
Matt Arsenault7c936902014-10-21 23:01:01 +000079 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
80 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
81
Tom Stellard35bb18c2013-08-26 15:06:04 +000082 // We need to custom lower vector stores from local memory
Tom Stellard35bb18c2013-08-26 15:06:04 +000083 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000084 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
85 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
86
87 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
88 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000089
Tom Stellard1c8788e2014-03-07 20:12:33 +000090 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000091 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
92
Tom Stellard0ec134f2014-02-04 17:18:40 +000093 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +000094 setOperationAction(ISD::SELECT, MVT::f64, Promote);
95 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +000096
Tom Stellard3ca1bfc2014-06-10 16:01:22 +000097 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
98 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
99 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
100 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000101
Tom Stellard83747202013-07-18 21:43:53 +0000102 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
103 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
104
Matt Arsenaulte306a322014-10-21 16:25:08 +0000105 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
106
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000107 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000108 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
109 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
110
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000111 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
114
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000115 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000116 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
118
Matt Arsenault94812212014-11-14 18:18:16 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
121
Tom Stellard94593ee2013-06-03 17:40:18 +0000122 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000123 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
124 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
125 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000126
Tom Stellardafcf12f2013-09-12 02:55:14 +0000127 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000128 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000129
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000130 for (MVT VT : MVT::integer_valuetypes()) {
Matt Arsenaultbd223422015-01-14 01:35:17 +0000131 if (VT == MVT::i64)
132 continue;
133
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000134 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000135 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
136 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000137 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000138
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000139 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000140 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
141 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000142 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000143
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000144 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000145 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
146 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000147 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
148 }
149
150 for (MVT VT : MVT::integer_vector_valuetypes()) {
151 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
152 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
153 }
154
155 for (MVT VT : MVT::fp_valuetypes())
156 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000157
Matt Arsenault6f243792013-09-05 19:41:10 +0000158 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000159 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
160 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000161
Matt Arsenault470acd82014-04-15 22:28:39 +0000162 setOperationAction(ISD::LOAD, MVT::i1, Custom);
163
Tom Stellardfd155822013-08-26 15:05:36 +0000164 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000165 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000166 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000167
Tom Stellard5f337882014-04-29 23:12:43 +0000168 // These should use UDIVREM, so set them to expand
169 setOperationAction(ISD::UDIV, MVT::i64, Expand);
170 setOperationAction(ISD::UREM, MVT::i64, Expand);
171
Matt Arsenault0d89e842014-07-15 21:44:37 +0000172 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
173 setOperationAction(ISD::SELECT, MVT::i1, Promote);
174
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000175 // We only support LOAD/STORE and vector manipulation ops for vectors
176 // with > 4 elements.
177 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000178 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
179 switch(Op) {
180 case ISD::LOAD:
181 case ISD::STORE:
182 case ISD::BUILD_VECTOR:
183 case ISD::BITCAST:
184 case ISD::EXTRACT_VECTOR_ELT:
185 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000186 case ISD::INSERT_SUBVECTOR:
187 case ISD::EXTRACT_SUBVECTOR:
188 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000189 case ISD::CONCAT_VECTORS:
190 setOperationAction(Op, VT, Custom);
191 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000192 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000193 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000194 break;
195 }
196 }
197 }
198
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000199 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
200 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
201 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000202 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000203 }
204
Marek Olsak7d777282015-03-24 13:40:15 +0000205 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000206 setOperationAction(ISD::FDIV, MVT::f32, Custom);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +0000207 setOperationAction(ISD::FDIV, MVT::f64, Custom);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000208
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000209 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000210 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000211 setTargetDAGCombine(ISD::FMINNUM);
212 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000213 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +0000214 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000215 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000216 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000217 setTargetDAGCombine(ISD::UINT_TO_FP);
218
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000219 // All memory operations. Some folding on the pointer operand is done to help
220 // matching the constant offsets in the addressing modes.
221 setTargetDAGCombine(ISD::LOAD);
222 setTargetDAGCombine(ISD::STORE);
223 setTargetDAGCombine(ISD::ATOMIC_LOAD);
224 setTargetDAGCombine(ISD::ATOMIC_STORE);
225 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
226 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
227 setTargetDAGCombine(ISD::ATOMIC_SWAP);
228 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
229 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
230 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
231 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
232 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
233 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
234 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
235 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
236 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
237 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
238
Christian Konigeecebd02013-03-26 14:04:02 +0000239 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000240}
241
Tom Stellard0125f2a2013-06-25 02:39:35 +0000242//===----------------------------------------------------------------------===//
243// TargetLowering queries
244//===----------------------------------------------------------------------===//
245
Matt Arsenaulte306a322014-10-21 16:25:08 +0000246bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
247 EVT) const {
248 // SI has some legal vector types, but no legal vector operations. Say no
249 // shuffles are legal in order to prefer scalarizing some vector operations.
250 return false;
251}
252
Matt Arsenault5015a892014-08-15 17:17:07 +0000253bool SITargetLowering::isLegalAddressingMode(const AddrMode &AM,
Matt Arsenaultbd7d80a2015-06-01 05:31:59 +0000254 Type *Ty, unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000255 // No global is ever allowed as a base.
256 if (AM.BaseGV)
257 return false;
258
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000259 switch (AS) {
260 case AMDGPUAS::GLOBAL_ADDRESS:
261 case AMDGPUAS::CONSTANT_ADDRESS: // XXX - Should we assume SMRD instructions?
262 case AMDGPUAS::PRIVATE_ADDRESS:
263 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE: {
264 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
265 // additionally can do r + r + i with addr64. 32-bit has more addressing
266 // mode options. Depending on the resource constant, it can also do
267 // (i64 r0) + (i32 r1) * (i14 i).
268 //
269 // SMRD instructions have an 8-bit, dword offset.
270 //
271 // Assume nonunifom access, since the address space isn't enough to know
272 // what instruction we will use, and since we don't know if this is a load
273 // or store and scalar stores are only available on VI.
274 //
275 // We also know if we are doing an extload, we can't do a scalar load.
276 //
277 // Private arrays end up using a scratch buffer most of the time, so also
278 // assume those use MUBUF instructions. Scratch loads / stores are currently
279 // implemented as mubuf instructions with offen bit set, so slightly
280 // different than the normal addr64.
281 if (!isUInt<12>(AM.BaseOffs))
282 return false;
Matt Arsenault5015a892014-08-15 17:17:07 +0000283
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000284 // FIXME: Since we can split immediate into soffset and immediate offset,
285 // would it make sense to allow any immediate?
286
287 switch (AM.Scale) {
288 case 0: // r + i or just i, depending on HasBaseReg.
289 return true;
290 case 1:
291 return true; // We have r + r or r + i.
292 case 2:
293 if (AM.HasBaseReg) {
294 // Reject 2 * r + r.
295 return false;
296 }
297
298 // Allow 2 * r as r + r
299 // Or 2 * r + i is allowed as r + r + i.
300 return true;
301 default: // Don't allow n * r
Matt Arsenault5015a892014-08-15 17:17:07 +0000302 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000303 }
304 }
305 case AMDGPUAS::LOCAL_ADDRESS:
306 case AMDGPUAS::REGION_ADDRESS: {
307 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
308 // field.
309 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
310 // an 8-bit dword offset but we don't know the alignment here.
311 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000312 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000313
314 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
315 return true;
316
317 if (AM.Scale == 1 && AM.HasBaseReg)
318 return true;
319
Matt Arsenault5015a892014-08-15 17:17:07 +0000320 return false;
321 }
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000322 case AMDGPUAS::FLAT_ADDRESS: {
323 // Flat instructions do not have offsets, and only have the register
324 // address.
325 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
326 }
327 default:
328 llvm_unreachable("unhandled address space");
329 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000330}
331
Matt Arsenaulte6986632015-01-14 01:35:22 +0000332bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000333 unsigned AddrSpace,
334 unsigned Align,
335 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000336 if (IsFast)
337 *IsFast = false;
338
Matt Arsenault1018c892014-04-24 17:08:26 +0000339 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
340 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000341 if (!VT.isSimple() || VT == MVT::Other)
342 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000343
Tom Stellardc6b299c2015-02-02 18:02:28 +0000344 // TODO - CI+ supports unaligned memory accesses, but this requires driver
345 // support.
Matt Arsenault1018c892014-04-24 17:08:26 +0000346
Matt Arsenault1018c892014-04-24 17:08:26 +0000347 // XXX - The only mention I see of this in the ISA manual is for LDS direct
348 // reads the "byte address and must be dword aligned". Is it also true for the
349 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000350 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
351 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
352 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
353 // with adjacent offsets.
354 return Align % 4 == 0;
355 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000356
Tom Stellard33e64c62015-02-04 20:49:52 +0000357 // Smaller than dword value must be aligned.
358 // FIXME: This should be allowed on CI+
359 if (VT.bitsLT(MVT::i32))
360 return false;
361
Matt Arsenault1018c892014-04-24 17:08:26 +0000362 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
363 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000364 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000365 if (IsFast)
366 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000367
368 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000369}
370
Matt Arsenault46645fa2014-07-28 17:49:26 +0000371EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
372 unsigned SrcAlign, bool IsMemset,
373 bool ZeroMemset,
374 bool MemcpyStrSrc,
375 MachineFunction &MF) const {
376 // FIXME: Should account for address space here.
377
378 // The default fallback uses the private pointer size as a guess for a type to
379 // use. Make sure we switch these to 64-bit accesses.
380
381 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
382 return MVT::v4i32;
383
384 if (Size >= 8 && DstAlign >= 4)
385 return MVT::v2i32;
386
387 // Use the default.
388 return MVT::Other;
389}
390
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000391TargetLoweringBase::LegalizeTypeAction
392SITargetLowering::getPreferredVectorAction(EVT VT) const {
393 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
394 return TypeSplitVector;
395
396 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000397}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000398
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000399bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
400 Type *Ty) const {
Eric Christopher7792e322015-01-30 23:24:40 +0000401 const SIInstrInfo *TII =
402 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000403 return TII->isInlineConstant(Imm);
404}
405
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000406static EVT toIntegerVT(EVT VT) {
407 if (VT.isVector())
408 return VT.changeVectorElementTypeToInteger();
409 return MVT::getIntegerVT(VT.getSizeInBits());
410}
411
Tom Stellardaf775432013-10-23 00:44:32 +0000412SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000413 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000414 unsigned Offset, bool Signed) const {
Matt Arsenault86033ca2014-07-28 17:31:39 +0000415 const DataLayout *DL = getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000416 MachineFunction &MF = DAG.getMachineFunction();
417 const SIRegisterInfo *TRI =
418 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
419 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000420
Matt Arsenault86033ca2014-07-28 17:31:39 +0000421 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
422
423 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000424 MVT PtrVT = getPointerTy(AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000425 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000426 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
427 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
428 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
429 DAG.getConstant(Offset, SL, PtrVT));
Matt Arsenault86033ca2014-07-28 17:31:39 +0000430 SDValue PtrOffset = DAG.getUNDEF(getPointerTy(AMDGPUAS::CONSTANT_ADDRESS));
431 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
432
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000433 unsigned Align = DL->getABITypeAlignment(Ty);
434
435 if (VT != MemVT && VT.isFloatingPoint()) {
436 // Do an integer load and convert.
437 // FIXME: This is mostly because load legalization after type legalization
438 // doesn't handle FP extloads.
439 assert(VT.getScalarType() == MVT::f32 &&
440 MemVT.getScalarType() == MVT::f16);
441
442 EVT IVT = toIntegerVT(VT);
443 EVT MemIVT = toIntegerVT(MemVT);
444 SDValue Load = DAG.getLoad(ISD::UNINDEXED, ISD::ZEXTLOAD,
445 IVT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemIVT,
446 false, // isVolatile
447 true, // isNonTemporal
448 true, // isInvariant
449 Align); // Alignment
450 return DAG.getNode(ISD::FP16_TO_FP, SL, VT, Load);
451 }
452
453 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
454 return DAG.getLoad(ISD::UNINDEXED, ExtTy,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000455 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
456 false, // isVolatile
457 true, // isNonTemporal
458 true, // isInvariant
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000459 Align); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000460}
461
Christian Konig2c8f6d52013-03-07 09:03:52 +0000462SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000463 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
464 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
465 SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardec2e43c2014-09-22 15:35:29 +0000466 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000467 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000468
469 MachineFunction &MF = DAG.getMachineFunction();
470 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000471 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000472
473 assert(CallConv == CallingConv::C);
474
475 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000476 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000477
478 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000479 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000480
481 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000482 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Vincent Lejeuned6236442013-10-13 17:56:16 +0000483 !Arg.Flags.isByVal()) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000484
485 assert((PSInputNum <= 15) && "Too many PS inputs!");
486
487 if (!Arg.Used) {
488 // We can savely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000489 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000490 ++PSInputNum;
491 continue;
492 }
493
494 Info->PSInputAddr |= 1 << PSInputNum++;
495 }
496
497 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000498 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000499 ISD::InputArg NewArg = Arg;
500 NewArg.Flags.setSplit();
501 NewArg.VT = Arg.VT.getVectorElementType();
502
503 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
504 // three or five element vertex only needs three or five registers,
505 // NOT four or eigth.
Andrew Trick05938a52015-02-16 18:10:47 +0000506 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000507 unsigned NumElements = ParamType->getVectorNumElements();
508
509 for (unsigned j = 0; j != NumElements; ++j) {
510 Splits.push_back(NewArg);
511 NewArg.PartOffset += NewArg.VT.getStoreSize();
512 }
513
Matt Arsenault762af962014-07-13 03:06:39 +0000514 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000515 Splits.push_back(Arg);
516 }
517 }
518
519 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000520 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
521 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000522
Christian Konig99ee0f42013-03-07 09:04:14 +0000523 // At least one interpolation mode must be enabled or else the GPU will hang.
Matt Arsenault762af962014-07-13 03:06:39 +0000524 if (Info->getShaderType() == ShaderType::PIXEL &&
525 (Info->PSInputAddr & 0x7F) == 0) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000526 Info->PSInputAddr |= 1;
527 CCInfo.AllocateReg(AMDGPU::VGPR0);
528 CCInfo.AllocateReg(AMDGPU::VGPR1);
529 }
530
Tom Stellarded882c22013-06-03 17:40:11 +0000531 // The pointer to the list of arguments is stored in SGPR0, SGPR1
Tom Stellardb02094e2014-07-21 15:45:01 +0000532 // The pointer to the scratch buffer is stored in SGPR2, SGPR3
Matt Arsenault762af962014-07-13 03:06:39 +0000533 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardfeab91c2014-12-02 17:41:43 +0000534 if (Subtarget->isAmdHsaOS())
535 Info->NumUserSGPRs = 2; // FIXME: Need to support scratch buffers.
536 else
537 Info->NumUserSGPRs = 4;
Tom Stellardec2e43c2014-09-22 15:35:29 +0000538
539 unsigned InputPtrReg =
540 TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
541 unsigned InputPtrRegLo =
542 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 0);
543 unsigned InputPtrRegHi =
544 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 1);
545
546 unsigned ScratchPtrReg =
547 TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_PTR);
548 unsigned ScratchPtrRegLo =
549 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 0);
550 unsigned ScratchPtrRegHi =
551 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 1);
552
553 CCInfo.AllocateReg(InputPtrRegLo);
554 CCInfo.AllocateReg(InputPtrRegHi);
555 CCInfo.AllocateReg(ScratchPtrRegLo);
556 CCInfo.AllocateReg(ScratchPtrRegHi);
557 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
558 MF.addLiveIn(ScratchPtrReg, &AMDGPU::SReg_64RegClass);
Tom Stellarded882c22013-06-03 17:40:11 +0000559 }
560
Matt Arsenault762af962014-07-13 03:06:39 +0000561 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000562 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
563 Splits);
564 }
565
Christian Konig2c8f6d52013-03-07 09:03:52 +0000566 AnalyzeFormalArguments(CCInfo, Splits);
567
568 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
569
Christian Konigb7be72d2013-05-17 09:46:48 +0000570 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000571 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000572 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000573 continue;
574 }
575
Christian Konig2c8f6d52013-03-07 09:03:52 +0000576 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000577 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000578
579 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000580 VT = Ins[i].VT;
581 EVT MemVT = Splits[i].VT;
Jan Veselye5121f32014-10-14 20:05:26 +0000582 const unsigned Offset = 36 + VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000583 // The first 36 bytes of the input buffer contains information about
584 // thread group and global sizes.
Tom Stellardaf775432013-10-23 00:44:32 +0000585 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
Jan Veselye5121f32014-10-14 20:05:26 +0000586 Offset, Ins[i].Flags.isSExt());
Tom Stellardca7ecf32014-08-22 18:49:31 +0000587
588 const PointerType *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000589 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000590 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
591 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
592 // On SI local pointers are just offsets into LDS, so they are always
593 // less than 16-bits. On CI and newer they could potentially be
594 // real pointers, so we can't guarantee their size.
595 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
596 DAG.getValueType(MVT::i16));
597 }
598
Tom Stellarded882c22013-06-03 17:40:11 +0000599 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000600 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000601 continue;
602 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000603 assert(VA.isRegLoc() && "Parameter must be in a register!");
604
605 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000606
607 if (VT == MVT::i64) {
608 // For now assume it is a pointer
609 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
610 &AMDGPU::SReg_64RegClass);
611 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
612 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
613 continue;
614 }
615
616 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
617
618 Reg = MF.addLiveIn(Reg, RC);
619 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
620
Christian Konig2c8f6d52013-03-07 09:03:52 +0000621 if (Arg.VT.isVector()) {
622
623 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000624 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000625 unsigned NumElements = ParamType->getVectorNumElements();
626
627 SmallVector<SDValue, 4> Regs;
628 Regs.push_back(Val);
629 for (unsigned j = 1; j != NumElements; ++j) {
630 Reg = ArgLocs[ArgIdx++].getLocReg();
631 Reg = MF.addLiveIn(Reg, RC);
632 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
633 }
634
635 // Fill up the missing vector elements
636 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000637 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000638
Craig Topper48d114b2014-04-26 18:35:24 +0000639 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000640 continue;
641 }
642
643 InVals.push_back(Val);
644 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000645
646 if (Info->getShaderType() != ShaderType::COMPUTE) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000647 unsigned ScratchIdx = CCInfo.getFirstUnallocated(ArrayRef<MCPhysReg>(
648 AMDGPU::SGPR_32RegClass.begin(), AMDGPU::SGPR_32RegClass.getNumRegs()));
Tom Stellarde99fb652015-01-20 19:33:04 +0000649 Info->ScratchOffsetReg = AMDGPU::SGPR_32RegClass.getRegister(ScratchIdx);
650 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000651 return Chain;
652}
653
Tom Stellard75aadc22012-12-11 21:25:42 +0000654MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
655 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000656
Tom Stellard556d9aa2013-06-03 17:39:37 +0000657 MachineBasicBlock::iterator I = *MI;
Eric Christopher7792e322015-01-30 23:24:40 +0000658 const SIInstrInfo *TII =
659 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellard556d9aa2013-06-03 17:39:37 +0000660
Tom Stellard75aadc22012-12-11 21:25:42 +0000661 switch (MI->getOpcode()) {
662 default:
663 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Matt Arsenault20711b72015-02-20 22:10:45 +0000664 case AMDGPU::BRANCH:
665 return BB;
Tom Stellard81d871d2013-11-13 23:36:50 +0000666 case AMDGPU::SI_RegisterStorePseudo: {
667 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard81d871d2013-11-13 23:36:50 +0000668 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
669 MachineInstrBuilder MIB =
670 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
671 Reg);
672 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
673 MIB.addOperand(MI->getOperand(i));
674
675 MI->eraseFromParent();
Vincent Lejeune79a58342014-05-10 19:18:25 +0000676 break;
677 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000678 }
679 return BB;
680}
681
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000682bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
683 // This currently forces unfolding various combinations of fsub into fma with
684 // free fneg'd operands. As long as we have fast FMA (controlled by
685 // isFMAFasterThanFMulAndFAdd), we should perform these.
686
687 // When fma is quarter rate, for f64 where add / sub are at best half rate,
688 // most of these combines appear to be cycle neutral but save on instruction
689 // count / code size.
690 return true;
691}
692
Matt Arsenault8596f712014-11-28 22:51:38 +0000693EVT SITargetLowering::getSetCCResultType(LLVMContext &Ctx, EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +0000694 if (!VT.isVector()) {
695 return MVT::i1;
696 }
Matt Arsenault8596f712014-11-28 22:51:38 +0000697 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +0000698}
699
Christian Konig082a14a2013-03-18 11:34:05 +0000700MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
701 return MVT::i32;
702}
703
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000704// Answering this is somewhat tricky and depends on the specific device which
705// have different rates for fma or all f64 operations.
706//
707// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
708// regardless of which device (although the number of cycles differs between
709// devices), so it is always profitable for f64.
710//
711// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
712// only on full rate devices. Normally, we should prefer selecting v_mad_f32
713// which we can always do even without fused FP ops since it returns the same
714// result as the separate operations and since it is always full
715// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
716// however does not support denormals, so we do report fma as faster if we have
717// a fast fma device and require denormals.
718//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000719bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
720 VT = VT.getScalarType();
721
722 if (!VT.isSimple())
723 return false;
724
725 switch (VT.getSimpleVT().SimpleTy) {
726 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000727 // This is as fast on some subtargets. However, we always have full rate f32
728 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +0000729 // which we should prefer over fma. We can't use this if we want to support
730 // denormals, so only report this in these cases.
731 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000732 case MVT::f64:
733 return true;
734 default:
735 break;
736 }
737
738 return false;
739}
740
Tom Stellard75aadc22012-12-11 21:25:42 +0000741//===----------------------------------------------------------------------===//
742// Custom DAG Lowering Operations
743//===----------------------------------------------------------------------===//
744
745SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
746 switch (Op.getOpcode()) {
747 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +0000748 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000749 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000750 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +0000751 SDValue Result = LowerLOAD(Op, DAG);
752 assert((!Result.getNode() ||
753 Result.getNode()->getNumValues() == 2) &&
754 "Load should return a value and a chain");
755 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +0000756 }
Tom Stellardaf775432013-10-23 00:44:32 +0000757
Matt Arsenaultad14ce82014-07-19 18:44:39 +0000758 case ISD::FSIN:
759 case ISD::FCOS:
760 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000761 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000762 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000763 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000764 case ISD::GlobalAddress: {
765 MachineFunction &MF = DAG.getMachineFunction();
766 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
767 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +0000768 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000769 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
770 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000771 }
772 return SDValue();
773}
774
Tom Stellardf8794352012-12-19 22:10:31 +0000775/// \brief Helper function for LowerBRCOND
776static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000777
Tom Stellardf8794352012-12-19 22:10:31 +0000778 SDNode *Parent = Value.getNode();
779 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
780 I != E; ++I) {
781
782 if (I.getUse().get() != Value)
783 continue;
784
785 if (I->getOpcode() == Opcode)
786 return *I;
787 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000788 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000789}
790
Tom Stellardb02094e2014-07-21 15:45:01 +0000791SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
792
Tom Stellardb02094e2014-07-21 15:45:01 +0000793 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
794 unsigned FrameIndex = FINode->getIndex();
795
Tom Stellardb02094e2014-07-21 15:45:01 +0000796 return DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
797}
798
Tom Stellardf8794352012-12-19 22:10:31 +0000799/// This transforms the control flow intrinsics to get the branch destination as
800/// last parameter, also switches branch target with BR if the need arise
801SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
802 SelectionDAG &DAG) const {
803
Andrew Trickef9de2a2013-05-25 02:42:55 +0000804 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000805
806 SDNode *Intr = BRCOND.getOperand(1).getNode();
807 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +0000808 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000809
810 if (Intr->getOpcode() == ISD::SETCC) {
811 // As long as we negate the condition everything is fine
812 SDNode *SetCC = Intr;
813 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000814 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
815 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000816 Intr = SetCC->getOperand(0).getNode();
817
818 } else {
819 // Get the target from BR if we don't negate the condition
820 BR = findUser(BRCOND, ISD::BR);
821 Target = BR->getOperand(1);
822 }
823
824 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
825
826 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000827 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +0000828
829 // operands of the new intrinsic call
830 SmallVector<SDValue, 4> Ops;
831 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000832 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +0000833 Ops.push_back(Target);
834
835 // build the new intrinsic call
836 SDNode *Result = DAG.getNode(
837 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +0000838 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000839
840 if (BR) {
841 // Give the branch instruction our target
842 SDValue Ops[] = {
843 BR->getOperand(0),
844 BRCOND.getOperand(2)
845 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000846 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
847 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
848 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000849 }
850
851 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
852
853 // Copy the intrinsic results to registers
854 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
855 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
856 if (!CopyToReg)
857 continue;
858
859 Chain = DAG.getCopyToReg(
860 Chain, DL,
861 CopyToReg->getOperand(1),
862 SDValue(Result, i - 1),
863 SDValue());
864
865 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
866 }
867
868 // Remove the old intrinsic from the chain
869 DAG.ReplaceAllUsesOfValueWith(
870 SDValue(Intr, Intr->getNumValues() - 1),
871 Intr->getOperand(0));
872
873 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000874}
875
Tom Stellard067c8152014-07-21 14:01:14 +0000876SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
877 SDValue Op,
878 SelectionDAG &DAG) const {
879 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
880
881 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
882 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
883
884 SDLoc DL(GSD);
885 const GlobalValue *GV = GSD->getGlobal();
886 MVT PtrVT = getPointerTy(GSD->getAddressSpace());
887
888 SDValue Ptr = DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT);
889 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
890
891 SDValue PtrLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000892 DAG.getConstant(0, DL, MVT::i32));
Tom Stellard067c8152014-07-21 14:01:14 +0000893 SDValue PtrHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000894 DAG.getConstant(1, DL, MVT::i32));
Tom Stellard067c8152014-07-21 14:01:14 +0000895
896 SDValue Lo = DAG.getNode(ISD::ADDC, DL, DAG.getVTList(MVT::i32, MVT::Glue),
897 PtrLo, GA);
898 SDValue Hi = DAG.getNode(ISD::ADDE, DL, DAG.getVTList(MVT::i32, MVT::Glue),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000899 PtrHi, DAG.getConstant(0, DL, MVT::i32),
Tom Stellard067c8152014-07-21 14:01:14 +0000900 SDValue(Lo.getNode(), 1));
901 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
902}
903
Tom Stellardfc92e772015-05-12 14:18:14 +0000904SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL,
905 SDValue V) const {
906 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
907 // so we will end up with redundant moves to m0.
908 //
909 // We can't use S_MOV_B32, because there is no way to specify m0 as the
910 // destination register.
911 //
912 // We have to use them both. Machine cse will combine all the S_MOV_B32
913 // instructions and the register coalescer eliminate the extra copies.
914 SDNode *M0 = DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, V.getValueType(), V);
915 return DAG.getCopyToReg(Chain, DL, DAG.getRegister(AMDGPU::M0, MVT::i32),
916 SDValue(M0, 0), SDValue()); // Glue
917 // A Null SDValue creates
918 // a glue result.
919}
920
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000921SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
922 SelectionDAG &DAG) const {
923 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000924 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000925 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000926
927 EVT VT = Op.getValueType();
928 SDLoc DL(Op);
929 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
930
931 switch (IntrinsicID) {
932 case Intrinsic::r600_read_ngroups_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000933 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
934 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000935 case Intrinsic::r600_read_ngroups_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000936 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
937 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000938 case Intrinsic::r600_read_ngroups_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000939 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
940 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000941 case Intrinsic::r600_read_global_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000942 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
943 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000944 case Intrinsic::r600_read_global_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000945 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
946 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000947 case Intrinsic::r600_read_global_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000948 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
949 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000950 case Intrinsic::r600_read_local_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000951 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
952 SI::KernelInputOffsets::LOCAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000953 case Intrinsic::r600_read_local_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000954 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
955 SI::KernelInputOffsets::LOCAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000956 case Intrinsic::r600_read_local_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000957 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
958 SI::KernelInputOffsets::LOCAL_SIZE_Z, false);
Jan Veselye5121f32014-10-14 20:05:26 +0000959
960 case Intrinsic::AMDGPU_read_workdim:
961 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
962 MF.getInfo<SIMachineFunctionInfo>()->ABIArgOffset,
963 false);
964
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000965 case Intrinsic::r600_read_tgid_x:
966 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000967 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000968 case Intrinsic::r600_read_tgid_y:
969 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000970 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000971 case Intrinsic::r600_read_tgid_z:
972 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000973 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000974 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000975 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000976 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000977 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000978 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000979 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000980 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000981 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000982 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000983 case AMDGPUIntrinsic::SI_load_const: {
984 SDValue Ops[] = {
985 Op.getOperand(1),
986 Op.getOperand(2)
987 };
988
989 MachineMemOperand *MMO = MF.getMachineMemOperand(
990 MachinePointerInfo(),
991 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
992 VT.getStoreSize(), 4);
993 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
994 Op->getVTList(), Ops, VT, MMO);
995 }
996 case AMDGPUIntrinsic::SI_sample:
997 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
998 case AMDGPUIntrinsic::SI_sampleb:
999 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
1000 case AMDGPUIntrinsic::SI_sampled:
1001 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
1002 case AMDGPUIntrinsic::SI_samplel:
1003 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
1004 case AMDGPUIntrinsic::SI_vs_load_input:
1005 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1006 Op.getOperand(1),
1007 Op.getOperand(2),
1008 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00001009
1010 case AMDGPUIntrinsic::AMDGPU_fract:
1011 case AMDGPUIntrinsic::AMDIL_fraction: // Legacy name.
1012 return DAG.getNode(ISD::FSUB, DL, VT, Op.getOperand(1),
1013 DAG.getNode(ISD::FFLOOR, DL, VT, Op.getOperand(1)));
Tom Stellard2a9d9472015-05-12 15:00:46 +00001014 case AMDGPUIntrinsic::SI_fs_constant: {
1015 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1016 SDValue Glue = M0.getValue(1);
1017 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
1018 DAG.getConstant(2, DL, MVT::i32), // P0
1019 Op.getOperand(1), Op.getOperand(2), Glue);
1020 }
1021 case AMDGPUIntrinsic::SI_fs_interp: {
1022 SDValue IJ = Op.getOperand(4);
1023 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1024 DAG.getConstant(0, DL, MVT::i32));
1025 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1026 DAG.getConstant(1, DL, MVT::i32));
1027 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1028 SDValue Glue = M0.getValue(1);
1029 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
1030 DAG.getVTList(MVT::f32, MVT::Glue),
1031 I, Op.getOperand(1), Op.getOperand(2), Glue);
1032 Glue = SDValue(P1.getNode(), 1);
1033 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
1034 Op.getOperand(1), Op.getOperand(2), Glue);
1035 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001036 default:
1037 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1038 }
1039}
1040
1041SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1042 SelectionDAG &DAG) const {
1043 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00001044 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001045 SDValue Chain = Op.getOperand(0);
1046 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1047
1048 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00001049 case AMDGPUIntrinsic::SI_sendmsg: {
1050 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1051 SDValue Glue = Chain.getValue(1);
1052 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1053 Op.getOperand(2), Glue);
1054 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001055 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001056 SDValue Ops[] = {
1057 Chain,
1058 Op.getOperand(2),
1059 Op.getOperand(3),
1060 Op.getOperand(4),
1061 Op.getOperand(5),
1062 Op.getOperand(6),
1063 Op.getOperand(7),
1064 Op.getOperand(8),
1065 Op.getOperand(9),
1066 Op.getOperand(10),
1067 Op.getOperand(11),
1068 Op.getOperand(12),
1069 Op.getOperand(13),
1070 Op.getOperand(14)
1071 };
1072
1073 EVT VT = Op.getOperand(3).getValueType();
1074
1075 MachineMemOperand *MMO = MF.getMachineMemOperand(
1076 MachinePointerInfo(),
1077 MachineMemOperand::MOStore,
1078 VT.getStoreSize(), 4);
1079 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1080 Op->getVTList(), Ops, VT, MMO);
1081 }
1082 default:
1083 return SDValue();
1084 }
1085}
1086
Tom Stellard81d871d2013-11-13 23:36:50 +00001087SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1088 SDLoc DL(Op);
1089 LoadSDNode *Load = cast<LoadSDNode>(Op);
1090
Tom Stellarde812f2f2014-07-21 15:45:06 +00001091 if (Op.getValueType().isVector()) {
1092 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1093 "Custom lowering for non-i32 vectors hasn't been implemented.");
1094 unsigned NumElements = Op.getValueType().getVectorNumElements();
1095 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
1096 switch (Load->getAddressSpace()) {
1097 default: break;
1098 case AMDGPUAS::GLOBAL_ADDRESS:
1099 case AMDGPUAS::PRIVATE_ADDRESS:
1100 // v4 loads are supported for private and global memory.
1101 if (NumElements <= 4)
1102 break;
1103 // fall-through
1104 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenault83e60582014-07-24 17:10:35 +00001105 return ScalarizeVectorLoad(Op, DAG);
Tom Stellarde812f2f2014-07-21 15:45:06 +00001106 }
Tom Stellarde9373602014-01-22 19:24:14 +00001107 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001108
Tom Stellarde812f2f2014-07-21 15:45:06 +00001109 return AMDGPUTargetLowering::LowerLOAD(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001110}
1111
Tom Stellard9fa17912013-08-14 23:24:45 +00001112SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
1113 const SDValue &Op,
1114 SelectionDAG &DAG) const {
1115 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
1116 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +00001117 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +00001118 Op.getOperand(4));
1119}
1120
Tom Stellard0ec134f2014-02-04 17:18:40 +00001121SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1122 if (Op.getValueType() != MVT::i64)
1123 return SDValue();
1124
1125 SDLoc DL(Op);
1126 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001127
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001128 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1129 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001130
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001131 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1132 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1133
1134 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1135 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001136
1137 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1138
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001139 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1140 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001141
1142 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1143
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001144 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1145 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001146}
1147
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001148// Catch division cases where we can use shortcuts with rcp and rsq
1149// instructions.
1150SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001151 SDLoc SL(Op);
1152 SDValue LHS = Op.getOperand(0);
1153 SDValue RHS = Op.getOperand(1);
1154 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001155 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001156
1157 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001158 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1159 CLHS->isExactlyValue(1.0)) {
1160 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1161 // the CI documentation has a worst case error of 1 ulp.
1162 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1163 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001164
1165 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001166 //
1167 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1168 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001169 if (RHS.getOpcode() == ISD::FSQRT)
1170 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1171
1172 // 1.0 / x -> rcp(x)
1173 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1174 }
1175 }
1176
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001177 if (Unsafe) {
1178 // Turn into multiply by the reciprocal.
1179 // x / y -> x * (1.0 / y)
1180 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1181 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip);
1182 }
1183
1184 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001185}
1186
1187SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001188 SDValue FastLowered = LowerFastFDIV(Op, DAG);
1189 if (FastLowered.getNode())
1190 return FastLowered;
1191
1192 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1193 // selection error for now rather than do something incorrect.
1194 if (Subtarget->hasFP32Denormals())
1195 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001196
1197 SDLoc SL(Op);
1198 SDValue LHS = Op.getOperand(0);
1199 SDValue RHS = Op.getOperand(1);
1200
1201 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1202
1203 const APFloat K0Val(BitsToFloat(0x6f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001204 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001205
1206 const APFloat K1Val(BitsToFloat(0x2f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001207 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001208
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001209 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001210
1211 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f32);
1212
1213 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1214
1215 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1216
1217 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1218
1219 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1220
1221 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1222
1223 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1224}
1225
1226SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001227 if (DAG.getTarget().Options.UnsafeFPMath)
1228 return LowerFastFDIV(Op, DAG);
1229
1230 SDLoc SL(Op);
1231 SDValue X = Op.getOperand(0);
1232 SDValue Y = Op.getOperand(1);
1233
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001234 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001235
1236 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
1237
1238 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
1239
1240 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
1241
1242 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
1243
1244 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
1245
1246 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
1247
1248 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
1249
1250 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
1251
1252 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
1253 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
1254
1255 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
1256 NegDivScale0, Mul, DivScale1);
1257
1258 SDValue Scale;
1259
1260 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1261 // Workaround a hardware bug on SI where the condition output from div_scale
1262 // is not usable.
1263
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001264 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001265
1266 // Figure out if the scale to use for div_fmas.
1267 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1268 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
1269 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
1270 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
1271
1272 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
1273 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
1274
1275 SDValue Scale0Hi
1276 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
1277 SDValue Scale1Hi
1278 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
1279
1280 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
1281 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
1282 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
1283 } else {
1284 Scale = DivScale1.getValue(1);
1285 }
1286
1287 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
1288 Fma4, Fma3, Mul, Scale);
1289
1290 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001291}
1292
1293SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1294 EVT VT = Op.getValueType();
1295
1296 if (VT == MVT::f32)
1297 return LowerFDIV32(Op, DAG);
1298
1299 if (VT == MVT::f64)
1300 return LowerFDIV64(Op, DAG);
1301
1302 llvm_unreachable("Unexpected type for fdiv");
1303}
1304
Tom Stellard81d871d2013-11-13 23:36:50 +00001305SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1306 SDLoc DL(Op);
1307 StoreSDNode *Store = cast<StoreSDNode>(Op);
1308 EVT VT = Store->getMemoryVT();
1309
Tom Stellard9b3816b2014-06-24 23:33:04 +00001310 // These stores are legal.
Tom Stellardb02094e2014-07-21 15:45:01 +00001311 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1312 if (VT.isVector() && VT.getVectorNumElements() > 4)
Matt Arsenault83e60582014-07-24 17:10:35 +00001313 return ScalarizeVectorStore(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001314 return SDValue();
1315 }
1316
Tom Stellard81d871d2013-11-13 23:36:50 +00001317 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1318 if (Ret.getNode())
1319 return Ret;
1320
1321 if (VT.isVector() && VT.getVectorNumElements() >= 8)
Matt Arsenault83e60582014-07-24 17:10:35 +00001322 return ScalarizeVectorStore(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001323
Tom Stellard1c8788e2014-03-07 20:12:33 +00001324 if (VT == MVT::i1)
1325 return DAG.getTruncStore(Store->getChain(), DL,
1326 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1327 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1328
Tom Stellarde812f2f2014-07-21 15:45:06 +00001329 return SDValue();
Tom Stellard81d871d2013-11-13 23:36:50 +00001330}
1331
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001332SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001333 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001334 EVT VT = Op.getValueType();
1335 SDValue Arg = Op.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001336 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
1337 DAG.getNode(ISD::FMUL, DL, VT, Arg,
1338 DAG.getConstantFP(0.5/M_PI, DL,
1339 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001340
1341 switch (Op.getOpcode()) {
1342 case ISD::FCOS:
1343 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1344 case ISD::FSIN:
1345 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1346 default:
1347 llvm_unreachable("Wrong trig opcode");
1348 }
1349}
1350
Tom Stellard75aadc22012-12-11 21:25:42 +00001351//===----------------------------------------------------------------------===//
1352// Custom DAG optimizations
1353//===----------------------------------------------------------------------===//
1354
Matt Arsenault364a6742014-06-11 17:50:44 +00001355SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00001356 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00001357 EVT VT = N->getValueType(0);
1358 EVT ScalarVT = VT.getScalarType();
1359 if (ScalarVT != MVT::f32)
1360 return SDValue();
1361
1362 SelectionDAG &DAG = DCI.DAG;
1363 SDLoc DL(N);
1364
1365 SDValue Src = N->getOperand(0);
1366 EVT SrcVT = Src.getValueType();
1367
1368 // TODO: We could try to match extracting the higher bytes, which would be
1369 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1370 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1371 // about in practice.
1372 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1373 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1374 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1375 DCI.AddToWorklist(Cvt.getNode());
1376 return Cvt;
1377 }
1378 }
1379
1380 // We are primarily trying to catch operations on illegal vector types
1381 // before they are expanded.
1382 // For scalars, we can use the more flexible method of checking masked bits
1383 // after legalization.
1384 if (!DCI.isBeforeLegalize() ||
1385 !SrcVT.isVector() ||
1386 SrcVT.getVectorElementType() != MVT::i8) {
1387 return SDValue();
1388 }
1389
1390 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1391
1392 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1393 // size as 4.
1394 unsigned NElts = SrcVT.getVectorNumElements();
1395 if (!SrcVT.isSimple() && NElts != 3)
1396 return SDValue();
1397
1398 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1399 // prevent a mess from expanding to v4i32 and repacking.
1400 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1401 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1402 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1403 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00001404 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001405
1406 unsigned AS = Load->getAddressSpace();
1407 unsigned Align = Load->getAlignment();
1408 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
1409 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
1410
1411 // Don't try to replace the load if we have to expand it due to alignment
1412 // problems. Otherwise we will end up scalarizing the load, and trying to
1413 // repack into the vector for no real reason.
1414 if (Align < ABIAlignment &&
1415 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
1416 return SDValue();
1417 }
1418
Matt Arsenault364a6742014-06-11 17:50:44 +00001419 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1420 Load->getChain(),
1421 Load->getBasePtr(),
1422 LoadVT,
1423 Load->getMemOperand());
1424
1425 // Make sure successors of the original load stay after it by updating
1426 // them to use the new Chain.
1427 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1428
1429 SmallVector<SDValue, 4> Elts;
1430 if (RegVT.isVector())
1431 DAG.ExtractVectorElements(NewLoad, Elts);
1432 else
1433 Elts.push_back(NewLoad);
1434
1435 SmallVector<SDValue, 4> Ops;
1436
1437 unsigned EltIdx = 0;
1438 for (SDValue Elt : Elts) {
1439 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1440 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1441 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1442 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1443 DCI.AddToWorklist(Cvt.getNode());
1444 Ops.push_back(Cvt);
1445 }
1446
1447 ++EltIdx;
1448 }
1449
1450 assert(Ops.size() == NElts);
1451
1452 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1453 }
1454
1455 return SDValue();
1456}
1457
Eric Christopher6c5b5112015-03-11 18:43:21 +00001458/// \brief Return true if the given offset Size in bytes can be folded into
1459/// the immediate offsets of a memory instruction for the given address space.
1460static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
1461 const AMDGPUSubtarget &STI) {
1462 switch (AS) {
1463 case AMDGPUAS::GLOBAL_ADDRESS: {
1464 // MUBUF instructions a 12-bit offset in bytes.
1465 return isUInt<12>(OffsetSize);
1466 }
1467 case AMDGPUAS::CONSTANT_ADDRESS: {
1468 // SMRD instructions have an 8-bit offset in dwords on SI and
1469 // a 20-bit offset in bytes on VI.
1470 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
1471 return isUInt<20>(OffsetSize);
1472 else
1473 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
1474 }
1475 case AMDGPUAS::LOCAL_ADDRESS:
1476 case AMDGPUAS::REGION_ADDRESS: {
1477 // The single offset versions have a 16-bit offset in bytes.
1478 return isUInt<16>(OffsetSize);
1479 }
1480 case AMDGPUAS::PRIVATE_ADDRESS:
1481 // Indirect register addressing does not use any offsets.
1482 default:
1483 return 0;
1484 }
1485}
1486
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001487// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
1488
1489// This is a variant of
1490// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
1491//
1492// The normal DAG combiner will do this, but only if the add has one use since
1493// that would increase the number of instructions.
1494//
1495// This prevents us from seeing a constant offset that can be folded into a
1496// memory instruction's addressing mode. If we know the resulting add offset of
1497// a pointer can be folded into an addressing offset, we can replace the pointer
1498// operand with the add of new constant offset. This eliminates one of the uses,
1499// and may allow the remaining use to also be simplified.
1500//
1501SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
1502 unsigned AddrSpace,
1503 DAGCombinerInfo &DCI) const {
1504 SDValue N0 = N->getOperand(0);
1505 SDValue N1 = N->getOperand(1);
1506
1507 if (N0.getOpcode() != ISD::ADD)
1508 return SDValue();
1509
1510 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
1511 if (!CN1)
1512 return SDValue();
1513
1514 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1515 if (!CAdd)
1516 return SDValue();
1517
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001518 // If the resulting offset is too large, we can't fold it into the addressing
1519 // mode offset.
1520 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Eric Christopher6c5b5112015-03-11 18:43:21 +00001521 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001522 return SDValue();
1523
1524 SelectionDAG &DAG = DCI.DAG;
1525 SDLoc SL(N);
1526 EVT VT = N->getValueType(0);
1527
1528 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001529 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001530
1531 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
1532}
1533
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001534SDValue SITargetLowering::performAndCombine(SDNode *N,
1535 DAGCombinerInfo &DCI) const {
1536 if (DCI.isBeforeLegalize())
1537 return SDValue();
1538
1539 SelectionDAG &DAG = DCI.DAG;
1540
1541 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
1542 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
1543 SDValue LHS = N->getOperand(0);
1544 SDValue RHS = N->getOperand(1);
1545
1546 if (LHS.getOpcode() == ISD::SETCC &&
1547 RHS.getOpcode() == ISD::SETCC) {
1548 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
1549 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
1550
1551 SDValue X = LHS.getOperand(0);
1552 SDValue Y = RHS.getOperand(0);
1553 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
1554 return SDValue();
1555
1556 if (LCC == ISD::SETO) {
1557 if (X != LHS.getOperand(1))
1558 return SDValue();
1559
1560 if (RCC == ISD::SETUNE) {
1561 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
1562 if (!C1 || !C1->isInfinity() || C1->isNegative())
1563 return SDValue();
1564
1565 const uint32_t Mask = SIInstrFlags::N_NORMAL |
1566 SIInstrFlags::N_SUBNORMAL |
1567 SIInstrFlags::N_ZERO |
1568 SIInstrFlags::P_ZERO |
1569 SIInstrFlags::P_SUBNORMAL |
1570 SIInstrFlags::P_NORMAL;
1571
1572 static_assert(((~(SIInstrFlags::S_NAN |
1573 SIInstrFlags::Q_NAN |
1574 SIInstrFlags::N_INFINITY |
1575 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
1576 "mask not equal");
1577
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001578 SDLoc DL(N);
1579 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
1580 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001581 }
1582 }
1583 }
1584
1585 return SDValue();
1586}
1587
Matt Arsenaultf2290332015-01-06 23:00:39 +00001588SDValue SITargetLowering::performOrCombine(SDNode *N,
1589 DAGCombinerInfo &DCI) const {
1590 SelectionDAG &DAG = DCI.DAG;
1591 SDValue LHS = N->getOperand(0);
1592 SDValue RHS = N->getOperand(1);
1593
1594 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
1595 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
1596 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
1597 SDValue Src = LHS.getOperand(0);
1598 if (Src != RHS.getOperand(0))
1599 return SDValue();
1600
1601 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
1602 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
1603 if (!CLHS || !CRHS)
1604 return SDValue();
1605
1606 // Only 10 bits are used.
1607 static const uint32_t MaxMask = 0x3ff;
1608
1609 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001610 SDLoc DL(N);
1611 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
1612 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00001613 }
1614
1615 return SDValue();
1616}
1617
1618SDValue SITargetLowering::performClassCombine(SDNode *N,
1619 DAGCombinerInfo &DCI) const {
1620 SelectionDAG &DAG = DCI.DAG;
1621 SDValue Mask = N->getOperand(1);
1622
1623 // fp_class x, 0 -> false
1624 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
1625 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001626 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00001627 }
1628
1629 return SDValue();
1630}
1631
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001632static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
1633 switch (Opc) {
1634 case ISD::FMAXNUM:
1635 return AMDGPUISD::FMAX3;
1636 case AMDGPUISD::SMAX:
1637 return AMDGPUISD::SMAX3;
1638 case AMDGPUISD::UMAX:
1639 return AMDGPUISD::UMAX3;
1640 case ISD::FMINNUM:
1641 return AMDGPUISD::FMIN3;
1642 case AMDGPUISD::SMIN:
1643 return AMDGPUISD::SMIN3;
1644 case AMDGPUISD::UMIN:
1645 return AMDGPUISD::UMIN3;
1646 default:
1647 llvm_unreachable("Not a min/max opcode");
1648 }
1649}
1650
1651SDValue SITargetLowering::performMin3Max3Combine(SDNode *N,
1652 DAGCombinerInfo &DCI) const {
1653 SelectionDAG &DAG = DCI.DAG;
1654
1655 unsigned Opc = N->getOpcode();
1656 SDValue Op0 = N->getOperand(0);
1657 SDValue Op1 = N->getOperand(1);
1658
1659 // Only do this if the inner op has one use since this will just increases
1660 // register pressure for no benefit.
1661
1662 // max(max(a, b), c)
1663 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
1664 SDLoc DL(N);
1665 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1666 DL,
1667 N->getValueType(0),
1668 Op0.getOperand(0),
1669 Op0.getOperand(1),
1670 Op1);
1671 }
1672
1673 // max(a, max(b, c))
1674 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
1675 SDLoc DL(N);
1676 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1677 DL,
1678 N->getValueType(0),
1679 Op0,
1680 Op1.getOperand(0),
1681 Op1.getOperand(1));
1682 }
1683
1684 return SDValue();
1685}
1686
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001687SDValue SITargetLowering::performSetCCCombine(SDNode *N,
1688 DAGCombinerInfo &DCI) const {
1689 SelectionDAG &DAG = DCI.DAG;
1690 SDLoc SL(N);
1691
1692 SDValue LHS = N->getOperand(0);
1693 SDValue RHS = N->getOperand(1);
1694 EVT VT = LHS.getValueType();
1695
1696 if (VT != MVT::f32 && VT != MVT::f64)
1697 return SDValue();
1698
1699 // Match isinf pattern
1700 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
1701 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
1702 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
1703 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
1704 if (!CRHS)
1705 return SDValue();
1706
1707 const APFloat &APF = CRHS->getValueAPF();
1708 if (APF.isInfinity() && !APF.isNegative()) {
1709 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001710 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
1711 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001712 }
1713 }
1714
1715 return SDValue();
1716}
1717
Tom Stellard75aadc22012-12-11 21:25:42 +00001718SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1719 DAGCombinerInfo &DCI) const {
1720 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001721 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001722
1723 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00001724 default:
1725 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001726 case ISD::SETCC:
1727 return performSetCCCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001728 case ISD::FMAXNUM: // TODO: What about fmax_legacy?
1729 case ISD::FMINNUM:
1730 case AMDGPUISD::SMAX:
1731 case AMDGPUISD::SMIN:
1732 case AMDGPUISD::UMAX:
1733 case AMDGPUISD::UMIN: {
1734 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00001735 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001736 getTargetMachine().getOptLevel() > CodeGenOpt::None)
1737 return performMin3Max3Combine(N, DCI);
1738 break;
1739 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001740
1741 case AMDGPUISD::CVT_F32_UBYTE0:
1742 case AMDGPUISD::CVT_F32_UBYTE1:
1743 case AMDGPUISD::CVT_F32_UBYTE2:
1744 case AMDGPUISD::CVT_F32_UBYTE3: {
1745 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
1746
1747 SDValue Src = N->getOperand(0);
1748 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
1749
1750 APInt KnownZero, KnownOne;
1751 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1752 !DCI.isBeforeLegalizeOps());
1753 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1754 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
1755 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
1756 DCI.CommitTargetLoweringOpt(TLO);
1757 }
1758
1759 break;
1760 }
1761
1762 case ISD::UINT_TO_FP: {
1763 return performUCharToFloatCombine(N, DCI);
Matt Arsenault8675db12014-08-29 16:01:14 +00001764
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001765 case ISD::FADD: {
1766 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1767 break;
1768
1769 EVT VT = N->getValueType(0);
1770 if (VT != MVT::f32)
1771 break;
1772
Matt Arsenault8d630032015-02-20 22:10:41 +00001773 // Only do this if we are not trying to support denormals. v_mad_f32 does
1774 // not support denormals ever.
1775 if (Subtarget->hasFP32Denormals())
1776 break;
1777
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001778 SDValue LHS = N->getOperand(0);
1779 SDValue RHS = N->getOperand(1);
1780
1781 // These should really be instruction patterns, but writing patterns with
1782 // source modiifiers is a pain.
1783
1784 // fadd (fadd (a, a), b) -> mad 2.0, a, b
1785 if (LHS.getOpcode() == ISD::FADD) {
1786 SDValue A = LHS.getOperand(0);
1787 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001788 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00001789 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001790 }
1791 }
1792
1793 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
1794 if (RHS.getOpcode() == ISD::FADD) {
1795 SDValue A = RHS.getOperand(0);
1796 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001797 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00001798 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001799 }
1800 }
1801
Matt Arsenault8d630032015-02-20 22:10:41 +00001802 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001803 }
Matt Arsenault8675db12014-08-29 16:01:14 +00001804 case ISD::FSUB: {
1805 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1806 break;
1807
1808 EVT VT = N->getValueType(0);
1809
1810 // Try to get the fneg to fold into the source modifier. This undoes generic
1811 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00001812 //
1813 // Only do this if we are not trying to support denormals. v_mad_f32 does
1814 // not support denormals ever.
1815 if (VT == MVT::f32 &&
1816 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00001817 SDValue LHS = N->getOperand(0);
1818 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001819 if (LHS.getOpcode() == ISD::FADD) {
1820 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
1821
1822 SDValue A = LHS.getOperand(0);
1823 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001824 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001825 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
1826
Matt Arsenault8d630032015-02-20 22:10:41 +00001827 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001828 }
1829 }
1830
1831 if (RHS.getOpcode() == ISD::FADD) {
1832 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
1833
1834 SDValue A = RHS.getOperand(0);
1835 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001836 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00001837 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001838 }
1839 }
Matt Arsenault8d630032015-02-20 22:10:41 +00001840
1841 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00001842 }
1843
1844 break;
1845 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001846 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001847 case ISD::LOAD:
1848 case ISD::STORE:
1849 case ISD::ATOMIC_LOAD:
1850 case ISD::ATOMIC_STORE:
1851 case ISD::ATOMIC_CMP_SWAP:
1852 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
1853 case ISD::ATOMIC_SWAP:
1854 case ISD::ATOMIC_LOAD_ADD:
1855 case ISD::ATOMIC_LOAD_SUB:
1856 case ISD::ATOMIC_LOAD_AND:
1857 case ISD::ATOMIC_LOAD_OR:
1858 case ISD::ATOMIC_LOAD_XOR:
1859 case ISD::ATOMIC_LOAD_NAND:
1860 case ISD::ATOMIC_LOAD_MIN:
1861 case ISD::ATOMIC_LOAD_MAX:
1862 case ISD::ATOMIC_LOAD_UMIN:
1863 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
1864 if (DCI.isBeforeLegalize())
1865 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001866
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001867 MemSDNode *MemNode = cast<MemSDNode>(N);
1868 SDValue Ptr = MemNode->getBasePtr();
1869
1870 // TODO: We could also do this for multiplies.
1871 unsigned AS = MemNode->getAddressSpace();
1872 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
1873 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
1874 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001875 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001876
1877 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
1878 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
1879 }
1880 }
1881 break;
1882 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001883 case ISD::AND:
1884 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00001885 case ISD::OR:
1886 return performOrCombine(N, DCI);
1887 case AMDGPUISD::FP_CLASS:
1888 return performClassCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001889 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001890 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001891}
Christian Konigd910b7d2013-02-26 17:52:16 +00001892
Christian Konigf82901a2013-02-26 17:52:23 +00001893/// \brief Analyze the possible immediate value Op
1894///
1895/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1896/// and the immediate value if it's a literal immediate
1897int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1898
Eric Christopher7792e322015-01-30 23:24:40 +00001899 const SIInstrInfo *TII =
1900 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001901
Tom Stellardedbf1eb2013-04-05 23:31:20 +00001902 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00001903 if (TII->isInlineConstant(Node->getAPIntValue()))
1904 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00001905
Matt Arsenault11a4d672015-02-13 19:05:03 +00001906 uint64_t Val = Node->getZExtValue();
1907 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00001908 }
1909
1910 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1911 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
1912 return 0;
1913
1914 if (Node->getValueType(0) == MVT::f32)
1915 return FloatToBits(Node->getValueAPF().convertToFloat());
1916
1917 return -1;
1918 }
1919
1920 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001921}
1922
Christian Konig8e06e2a2013-04-10 08:39:08 +00001923/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00001924static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00001925 switch (Idx) {
1926 default: return 0;
1927 case AMDGPU::sub0: return 0;
1928 case AMDGPU::sub1: return 1;
1929 case AMDGPU::sub2: return 2;
1930 case AMDGPU::sub3: return 3;
1931 }
1932}
1933
1934/// \brief Adjust the writemask of MIMG instructions
1935void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1936 SelectionDAG &DAG) const {
1937 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00001938 unsigned Lane = 0;
1939 unsigned OldDmask = Node->getConstantOperandVal(0);
1940 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001941
1942 // Try to figure out the used register components
1943 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1944 I != E; ++I) {
1945
1946 // Abort if we can't understand the usage
1947 if (!I->isMachineOpcode() ||
1948 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1949 return;
1950
Tom Stellard54774e52013-10-23 02:53:47 +00001951 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1952 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1953 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1954 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00001955 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001956
Tom Stellard54774e52013-10-23 02:53:47 +00001957 // Set which texture component corresponds to the lane.
1958 unsigned Comp;
1959 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1960 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00001961 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00001962 Dmask &= ~(1 << Comp);
1963 }
1964
Christian Konig8e06e2a2013-04-10 08:39:08 +00001965 // Abort if we have more than one user per component
1966 if (Users[Lane])
1967 return;
1968
1969 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00001970 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001971 }
1972
Tom Stellard54774e52013-10-23 02:53:47 +00001973 // Abort if there's no change
1974 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00001975 return;
1976
1977 // Adjust the writemask in the node
1978 std::vector<SDValue> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001979 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001980 Ops.insert(Ops.end(), Node->op_begin() + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00001981 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001982
Christian Konig8b1ed282013-04-10 08:39:16 +00001983 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00001984 // (if NewDmask has only one bit set...)
1985 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001986 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
1987 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00001988 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001989 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00001990 SDValue(Node, 0), RC);
1991 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1992 return;
1993 }
1994
Christian Konig8e06e2a2013-04-10 08:39:08 +00001995 // Update the users of the node with the new indices
1996 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1997
1998 SDNode *User = Users[i];
1999 if (!User)
2000 continue;
2001
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002002 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002003 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
2004
2005 switch (Idx) {
2006 default: break;
2007 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
2008 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
2009 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
2010 }
2011 }
2012}
2013
Tom Stellard3457a842014-10-09 19:06:00 +00002014/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
2015/// with frame index operands.
2016/// LLVM assumes that inputs are to these instructions are registers.
2017void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
2018 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002019
2020 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00002021 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
2022 if (!isa<FrameIndexSDNode>(Node->getOperand(i))) {
2023 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002024 continue;
2025 }
2026
Tom Stellard3457a842014-10-09 19:06:00 +00002027 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002028 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00002029 Node->getOperand(i).getValueType(),
2030 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002031 }
2032
Tom Stellard3457a842014-10-09 19:06:00 +00002033 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002034}
2035
Matt Arsenault08d84942014-06-03 23:06:13 +00002036/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00002037SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
2038 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002039 const SIInstrInfo *TII =
2040 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konig8e06e2a2013-04-10 08:39:08 +00002041
Tom Stellard16a9a202013-08-14 23:24:17 +00002042 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00002043 adjustWritemask(Node, DAG);
2044
Matt Arsenault7d858d82014-11-02 23:46:54 +00002045 if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG ||
2046 Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002047 legalizeTargetIndependentNode(Node, DAG);
2048 return Node;
2049 }
Tom Stellard654d6692015-01-08 15:08:17 +00002050 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002051}
Christian Konig8b1ed282013-04-10 08:39:16 +00002052
2053/// \brief Assign the register class depending on the number of
2054/// bits set in the writemask
2055void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
2056 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002057 const SIInstrInfo *TII =
2058 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002059
Tom Stellarda99ada52014-11-21 22:31:44 +00002060 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00002061 TII->legalizeOperands(MI);
2062
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002063 if (TII->isMIMG(MI->getOpcode())) {
2064 unsigned VReg = MI->getOperand(0).getReg();
2065 unsigned Writemask = MI->getOperand(1).getImm();
2066 unsigned BitsSet = 0;
2067 for (unsigned i = 0; i < 4; ++i)
2068 BitsSet += Writemask & (1 << i) ? 1 : 0;
2069
2070 const TargetRegisterClass *RC;
2071 switch (BitsSet) {
2072 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002073 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002074 case 2: RC = &AMDGPU::VReg_64RegClass; break;
2075 case 3: RC = &AMDGPU::VReg_96RegClass; break;
2076 }
2077
2078 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
2079 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002080 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00002081 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00002082 }
2083
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002084 // Replace unused atomics with the no return version.
2085 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
2086 if (NoRetAtomicOp != -1) {
2087 if (!Node->hasAnyUseOfValue(0)) {
2088 MI->setDesc(TII->get(NoRetAtomicOp));
2089 MI->RemoveOperand(0);
2090 }
2091
2092 return;
2093 }
Christian Konig8b1ed282013-04-10 08:39:16 +00002094}
Tom Stellard0518ff82013-06-03 17:39:58 +00002095
Matt Arsenault485defe2014-11-05 19:01:17 +00002096static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002097 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00002098 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
2099}
2100
2101MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
2102 SDLoc DL,
2103 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002104 const SIInstrInfo *TII =
2105 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00002106#if 1
2107 // XXX - Workaround for moveToVALU not handling different register class
2108 // inserts for REG_SEQUENCE.
2109
2110 // Build the half of the subregister with the constants.
2111 const SDValue Ops0[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002112 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002113 buildSMovImm32(DAG, DL, 0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002114 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Tom Stellard794c8c02014-12-02 17:05:41 +00002115 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002116 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
Matt Arsenault485defe2014-11-05 19:01:17 +00002117 };
2118
2119 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
2120 MVT::v2i32, Ops0), 0);
2121
2122 // Combine the constants and the pointer.
2123 const SDValue Ops1[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002124 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002125 Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002126 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002127 SubRegHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002128 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
Matt Arsenault485defe2014-11-05 19:01:17 +00002129 };
2130
2131 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
2132#else
2133 const SDValue Ops[] = {
2134 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2135 Ptr,
2136 DAG.getTargetConstant(AMDGPU::sub0_sub1, MVT::i32),
2137 buildSMovImm32(DAG, DL, 0),
2138 DAG.getTargetConstant(AMDGPU::sub2, MVT::i32),
Tom Stellard794c8c02014-12-02 17:05:41 +00002139 buildSMovImm32(DAG, DL, TII->getDefaultRsrcFormat() >> 32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002140 DAG.getTargetConstant(AMDGPU::sub3, MVT::i32)
2141 };
2142
2143 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2144
2145#endif
2146}
2147
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002148/// \brief Return a resource descriptor with the 'Add TID' bit enabled
2149/// The TID (Thread ID) is multipled by the stride value (bits [61:48]
2150/// of the resource descriptor) to create an offset, which is added to the
2151/// resource ponter.
2152MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
2153 SDLoc DL,
2154 SDValue Ptr,
2155 uint32_t RsrcDword1,
2156 uint64_t RsrcDword2And3) const {
2157 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
2158 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
2159 if (RsrcDword1) {
2160 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002161 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
2162 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002163 }
2164
2165 SDValue DataLo = buildSMovImm32(DAG, DL,
2166 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
2167 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
2168
2169 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002170 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002171 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002172 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002173 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002174 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002175 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002176 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002177 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002178 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002179 };
2180
2181 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2182}
2183
2184MachineSDNode *SITargetLowering::buildScratchRSRC(SelectionDAG &DAG,
2185 SDLoc DL,
2186 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002187 const SIInstrInfo *TII =
2188 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellard794c8c02014-12-02 17:05:41 +00002189 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() | AMDGPU::RSRC_TID_ENABLE |
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002190 0xffffffff; // Size
2191
2192 return buildRSRC(DAG, DL, Ptr, 0, Rsrc);
2193}
2194
Tom Stellard94593ee2013-06-03 17:40:18 +00002195SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2196 const TargetRegisterClass *RC,
2197 unsigned Reg, EVT VT) const {
2198 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2199
2200 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2201 cast<RegisterSDNode>(VReg)->getReg(), VT);
2202}
Tom Stellardd7e6f132015-04-08 01:09:26 +00002203
2204//===----------------------------------------------------------------------===//
2205// SI Inline Assembly Support
2206//===----------------------------------------------------------------------===//
2207
2208std::pair<unsigned, const TargetRegisterClass *>
2209SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
2210 const std::string &Constraint,
2211 MVT VT) const {
2212 if (Constraint == "r") {
2213 switch(VT.SimpleTy) {
2214 default: llvm_unreachable("Unhandled type for 'r' inline asm constraint");
2215 case MVT::i64:
2216 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
2217 case MVT::i32:
2218 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
2219 }
2220 }
2221
2222 if (Constraint.size() > 1) {
2223 const TargetRegisterClass *RC = nullptr;
2224 if (Constraint[1] == 'v') {
2225 RC = &AMDGPU::VGPR_32RegClass;
2226 } else if (Constraint[1] == 's') {
2227 RC = &AMDGPU::SGPR_32RegClass;
2228 }
2229
2230 if (RC) {
2231 unsigned Idx = std::atoi(Constraint.substr(2).c_str());
2232 if (Idx < RC->getNumRegs())
2233 return std::make_pair(RC->getRegister(Idx), RC);
2234 }
2235 }
2236 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
2237}