blob: 56214cd07dcde74fafd70c72ff4effb41327beee [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
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000158 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Matt Arsenault6f243792013-09-05 19:41:10 +0000159 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000160 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
161 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000162
Matt Arsenault470acd82014-04-15 22:28:39 +0000163 setOperationAction(ISD::LOAD, MVT::i1, Custom);
164
Tom Stellardfd155822013-08-26 15:05:36 +0000165 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000166 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000167 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000168
Tom Stellard5f337882014-04-29 23:12:43 +0000169 // These should use UDIVREM, so set them to expand
170 setOperationAction(ISD::UDIV, MVT::i64, Expand);
171 setOperationAction(ISD::UREM, MVT::i64, Expand);
172
Matt Arsenault0d89e842014-07-15 21:44:37 +0000173 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
174 setOperationAction(ISD::SELECT, MVT::i1, Promote);
175
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000176 // We only support LOAD/STORE and vector manipulation ops for vectors
177 // with > 4 elements.
178 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000179 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
180 switch(Op) {
181 case ISD::LOAD:
182 case ISD::STORE:
183 case ISD::BUILD_VECTOR:
184 case ISD::BITCAST:
185 case ISD::EXTRACT_VECTOR_ELT:
186 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000187 case ISD::INSERT_SUBVECTOR:
188 case ISD::EXTRACT_SUBVECTOR:
189 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000190 case ISD::CONCAT_VECTORS:
191 setOperationAction(Op, VT, Custom);
192 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000193 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000194 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000195 break;
196 }
197 }
198 }
199
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000200 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
201 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
202 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000203 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000204 }
205
Marek Olsak7d777282015-03-24 13:40:15 +0000206 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000207 setOperationAction(ISD::FDIV, MVT::f32, Custom);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +0000208 setOperationAction(ISD::FDIV, MVT::f64, Custom);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000209
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000210 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000211 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000212 setTargetDAGCombine(ISD::FMINNUM);
213 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000214 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +0000215 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000216 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000217 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000218 setTargetDAGCombine(ISD::UINT_TO_FP);
219
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000220 // All memory operations. Some folding on the pointer operand is done to help
221 // matching the constant offsets in the addressing modes.
222 setTargetDAGCombine(ISD::LOAD);
223 setTargetDAGCombine(ISD::STORE);
224 setTargetDAGCombine(ISD::ATOMIC_LOAD);
225 setTargetDAGCombine(ISD::ATOMIC_STORE);
226 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
227 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
228 setTargetDAGCombine(ISD::ATOMIC_SWAP);
229 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
230 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
231 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
232 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
233 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
234 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
235 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
236 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
237 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
238 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
239
Christian Konigeecebd02013-03-26 14:04:02 +0000240 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000241}
242
Tom Stellard0125f2a2013-06-25 02:39:35 +0000243//===----------------------------------------------------------------------===//
244// TargetLowering queries
245//===----------------------------------------------------------------------===//
246
Matt Arsenaulte306a322014-10-21 16:25:08 +0000247bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
248 EVT) const {
249 // SI has some legal vector types, but no legal vector operations. Say no
250 // shuffles are legal in order to prefer scalarizing some vector operations.
251 return false;
252}
253
Matt Arsenault5015a892014-08-15 17:17:07 +0000254// FIXME: This really needs an address space argument. The immediate offset
255// size is different for different sets of memory instruction sets.
256
257// The single offset DS instructions have a 16-bit unsigned byte offset.
258//
259// MUBUF / MTBUF have a 12-bit unsigned byte offset, and additionally can do r +
260// r + i with addr64. 32-bit has more addressing mode options. Depending on the
261// resource constant, it can also do (i64 r0) + (i32 r1) * (i14 i).
262//
263// SMRD instructions have an 8-bit, dword offset.
264//
265bool SITargetLowering::isLegalAddressingMode(const AddrMode &AM,
266 Type *Ty) const {
267 // No global is ever allowed as a base.
268 if (AM.BaseGV)
269 return false;
270
271 // Allow a 16-bit unsigned immediate field, since this is what DS instructions
272 // use.
273 if (!isUInt<16>(AM.BaseOffs))
274 return false;
275
276 // Only support r+r,
277 switch (AM.Scale) {
278 case 0: // "r+i" or just "i", depending on HasBaseReg.
279 break;
280 case 1:
281 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
282 return false;
283 // Otherwise we have r+r or r+i.
284 break;
285 case 2:
286 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
287 return false;
288 // Allow 2*r as r+r.
289 break;
290 default: // Don't allow n * r
291 return false;
292 }
293
294 return true;
295}
296
Matt Arsenaulte6986632015-01-14 01:35:22 +0000297bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000298 unsigned AddrSpace,
299 unsigned Align,
300 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000301 if (IsFast)
302 *IsFast = false;
303
Matt Arsenault1018c892014-04-24 17:08:26 +0000304 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
305 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000306 if (!VT.isSimple() || VT == MVT::Other)
307 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000308
Tom Stellardc6b299c2015-02-02 18:02:28 +0000309 // TODO - CI+ supports unaligned memory accesses, but this requires driver
310 // support.
Matt Arsenault1018c892014-04-24 17:08:26 +0000311
Matt Arsenault1018c892014-04-24 17:08:26 +0000312 // XXX - The only mention I see of this in the ISA manual is for LDS direct
313 // reads the "byte address and must be dword aligned". Is it also true for the
314 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000315 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
316 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
317 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
318 // with adjacent offsets.
319 return Align % 4 == 0;
320 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000321
Tom Stellard33e64c62015-02-04 20:49:52 +0000322 // Smaller than dword value must be aligned.
323 // FIXME: This should be allowed on CI+
324 if (VT.bitsLT(MVT::i32))
325 return false;
326
Matt Arsenault1018c892014-04-24 17:08:26 +0000327 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
328 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000329 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000330 if (IsFast)
331 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000332
333 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000334}
335
Matt Arsenault46645fa2014-07-28 17:49:26 +0000336EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
337 unsigned SrcAlign, bool IsMemset,
338 bool ZeroMemset,
339 bool MemcpyStrSrc,
340 MachineFunction &MF) const {
341 // FIXME: Should account for address space here.
342
343 // The default fallback uses the private pointer size as a guess for a type to
344 // use. Make sure we switch these to 64-bit accesses.
345
346 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
347 return MVT::v4i32;
348
349 if (Size >= 8 && DstAlign >= 4)
350 return MVT::v2i32;
351
352 // Use the default.
353 return MVT::Other;
354}
355
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000356TargetLoweringBase::LegalizeTypeAction
357SITargetLowering::getPreferredVectorAction(EVT VT) const {
358 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
359 return TypeSplitVector;
360
361 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000362}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000363
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000364bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
365 Type *Ty) const {
Eric Christopher7792e322015-01-30 23:24:40 +0000366 const SIInstrInfo *TII =
367 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000368 return TII->isInlineConstant(Imm);
369}
370
Tom Stellardaf775432013-10-23 00:44:32 +0000371SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000372 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000373 unsigned Offset, bool Signed) const {
Matt Arsenault86033ca2014-07-28 17:31:39 +0000374 const DataLayout *DL = getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000375 MachineFunction &MF = DAG.getMachineFunction();
376 const SIRegisterInfo *TRI =
377 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
378 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000379
Matt Arsenault86033ca2014-07-28 17:31:39 +0000380 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
381
382 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
383 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
384 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000385 MRI.getLiveInVirtReg(InputPtrReg), MVT::i64);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000386 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000387 DAG.getConstant(Offset, SL, MVT::i64));
Matt Arsenault86033ca2014-07-28 17:31:39 +0000388 SDValue PtrOffset = DAG.getUNDEF(getPointerTy(AMDGPUAS::CONSTANT_ADDRESS));
389 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
390
391 return DAG.getLoad(ISD::UNINDEXED, Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD,
392 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
393 false, // isVolatile
394 true, // isNonTemporal
395 true, // isInvariant
396 DL->getABITypeAlignment(Ty)); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000397}
398
Christian Konig2c8f6d52013-03-07 09:03:52 +0000399SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000400 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
401 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
402 SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardec2e43c2014-09-22 15:35:29 +0000403 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000404 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000405
406 MachineFunction &MF = DAG.getMachineFunction();
407 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000408 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000409
410 assert(CallConv == CallingConv::C);
411
412 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000413 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000414
415 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000416 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000417
418 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000419 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Vincent Lejeuned6236442013-10-13 17:56:16 +0000420 !Arg.Flags.isByVal()) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000421
422 assert((PSInputNum <= 15) && "Too many PS inputs!");
423
424 if (!Arg.Used) {
425 // We can savely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000426 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000427 ++PSInputNum;
428 continue;
429 }
430
431 Info->PSInputAddr |= 1 << PSInputNum++;
432 }
433
434 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000435 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000436 ISD::InputArg NewArg = Arg;
437 NewArg.Flags.setSplit();
438 NewArg.VT = Arg.VT.getVectorElementType();
439
440 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
441 // three or five element vertex only needs three or five registers,
442 // NOT four or eigth.
Andrew Trick05938a52015-02-16 18:10:47 +0000443 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000444 unsigned NumElements = ParamType->getVectorNumElements();
445
446 for (unsigned j = 0; j != NumElements; ++j) {
447 Splits.push_back(NewArg);
448 NewArg.PartOffset += NewArg.VT.getStoreSize();
449 }
450
Matt Arsenault762af962014-07-13 03:06:39 +0000451 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000452 Splits.push_back(Arg);
453 }
454 }
455
456 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000457 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
458 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000459
Christian Konig99ee0f42013-03-07 09:04:14 +0000460 // At least one interpolation mode must be enabled or else the GPU will hang.
Matt Arsenault762af962014-07-13 03:06:39 +0000461 if (Info->getShaderType() == ShaderType::PIXEL &&
462 (Info->PSInputAddr & 0x7F) == 0) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000463 Info->PSInputAddr |= 1;
464 CCInfo.AllocateReg(AMDGPU::VGPR0);
465 CCInfo.AllocateReg(AMDGPU::VGPR1);
466 }
467
Tom Stellarded882c22013-06-03 17:40:11 +0000468 // The pointer to the list of arguments is stored in SGPR0, SGPR1
Tom Stellardb02094e2014-07-21 15:45:01 +0000469 // The pointer to the scratch buffer is stored in SGPR2, SGPR3
Matt Arsenault762af962014-07-13 03:06:39 +0000470 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardfeab91c2014-12-02 17:41:43 +0000471 if (Subtarget->isAmdHsaOS())
472 Info->NumUserSGPRs = 2; // FIXME: Need to support scratch buffers.
473 else
474 Info->NumUserSGPRs = 4;
Tom Stellardec2e43c2014-09-22 15:35:29 +0000475
476 unsigned InputPtrReg =
477 TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
478 unsigned InputPtrRegLo =
479 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 0);
480 unsigned InputPtrRegHi =
481 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 1);
482
483 unsigned ScratchPtrReg =
484 TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_PTR);
485 unsigned ScratchPtrRegLo =
486 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 0);
487 unsigned ScratchPtrRegHi =
488 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 1);
489
490 CCInfo.AllocateReg(InputPtrRegLo);
491 CCInfo.AllocateReg(InputPtrRegHi);
492 CCInfo.AllocateReg(ScratchPtrRegLo);
493 CCInfo.AllocateReg(ScratchPtrRegHi);
494 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
495 MF.addLiveIn(ScratchPtrReg, &AMDGPU::SReg_64RegClass);
Tom Stellarded882c22013-06-03 17:40:11 +0000496 }
497
Matt Arsenault762af962014-07-13 03:06:39 +0000498 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000499 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
500 Splits);
501 }
502
Christian Konig2c8f6d52013-03-07 09:03:52 +0000503 AnalyzeFormalArguments(CCInfo, Splits);
504
505 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
506
Christian Konigb7be72d2013-05-17 09:46:48 +0000507 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000508 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000509 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000510 continue;
511 }
512
Christian Konig2c8f6d52013-03-07 09:03:52 +0000513 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000514 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000515
516 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000517 VT = Ins[i].VT;
518 EVT MemVT = Splits[i].VT;
Jan Veselye5121f32014-10-14 20:05:26 +0000519 const unsigned Offset = 36 + VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000520 // The first 36 bytes of the input buffer contains information about
521 // thread group and global sizes.
Tom Stellardaf775432013-10-23 00:44:32 +0000522 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
Jan Veselye5121f32014-10-14 20:05:26 +0000523 Offset, Ins[i].Flags.isSExt());
Tom Stellardca7ecf32014-08-22 18:49:31 +0000524
525 const PointerType *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000526 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000527 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
528 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
529 // On SI local pointers are just offsets into LDS, so they are always
530 // less than 16-bits. On CI and newer they could potentially be
531 // real pointers, so we can't guarantee their size.
532 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
533 DAG.getValueType(MVT::i16));
534 }
535
Tom Stellarded882c22013-06-03 17:40:11 +0000536 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000537 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000538 continue;
539 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000540 assert(VA.isRegLoc() && "Parameter must be in a register!");
541
542 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000543
544 if (VT == MVT::i64) {
545 // For now assume it is a pointer
546 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
547 &AMDGPU::SReg_64RegClass);
548 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
549 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
550 continue;
551 }
552
553 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
554
555 Reg = MF.addLiveIn(Reg, RC);
556 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
557
Christian Konig2c8f6d52013-03-07 09:03:52 +0000558 if (Arg.VT.isVector()) {
559
560 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000561 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000562 unsigned NumElements = ParamType->getVectorNumElements();
563
564 SmallVector<SDValue, 4> Regs;
565 Regs.push_back(Val);
566 for (unsigned j = 1; j != NumElements; ++j) {
567 Reg = ArgLocs[ArgIdx++].getLocReg();
568 Reg = MF.addLiveIn(Reg, RC);
569 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
570 }
571
572 // Fill up the missing vector elements
573 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000574 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000575
Craig Topper48d114b2014-04-26 18:35:24 +0000576 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000577 continue;
578 }
579
580 InVals.push_back(Val);
581 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000582
583 if (Info->getShaderType() != ShaderType::COMPUTE) {
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000584 unsigned ScratchIdx = CCInfo.getFirstUnallocated(ArrayRef<MCPhysReg>(
585 AMDGPU::SGPR_32RegClass.begin(), AMDGPU::SGPR_32RegClass.getNumRegs()));
Tom Stellarde99fb652015-01-20 19:33:04 +0000586 Info->ScratchOffsetReg = AMDGPU::SGPR_32RegClass.getRegister(ScratchIdx);
587 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000588 return Chain;
589}
590
Tom Stellard75aadc22012-12-11 21:25:42 +0000591MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
592 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000593
Tom Stellard556d9aa2013-06-03 17:39:37 +0000594 MachineBasicBlock::iterator I = *MI;
Eric Christopher7792e322015-01-30 23:24:40 +0000595 const SIInstrInfo *TII =
596 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellard556d9aa2013-06-03 17:39:37 +0000597
Tom Stellard75aadc22012-12-11 21:25:42 +0000598 switch (MI->getOpcode()) {
599 default:
600 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Matt Arsenault20711b72015-02-20 22:10:45 +0000601 case AMDGPU::BRANCH:
602 return BB;
Tom Stellard81d871d2013-11-13 23:36:50 +0000603 case AMDGPU::SI_RegisterStorePseudo: {
604 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard81d871d2013-11-13 23:36:50 +0000605 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
606 MachineInstrBuilder MIB =
607 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
608 Reg);
609 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
610 MIB.addOperand(MI->getOperand(i));
611
612 MI->eraseFromParent();
Vincent Lejeune79a58342014-05-10 19:18:25 +0000613 break;
614 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000615 }
616 return BB;
617}
618
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000619bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
620 // This currently forces unfolding various combinations of fsub into fma with
621 // free fneg'd operands. As long as we have fast FMA (controlled by
622 // isFMAFasterThanFMulAndFAdd), we should perform these.
623
624 // When fma is quarter rate, for f64 where add / sub are at best half rate,
625 // most of these combines appear to be cycle neutral but save on instruction
626 // count / code size.
627 return true;
628}
629
Matt Arsenault8596f712014-11-28 22:51:38 +0000630EVT SITargetLowering::getSetCCResultType(LLVMContext &Ctx, EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +0000631 if (!VT.isVector()) {
632 return MVT::i1;
633 }
Matt Arsenault8596f712014-11-28 22:51:38 +0000634 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +0000635}
636
Christian Konig082a14a2013-03-18 11:34:05 +0000637MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
638 return MVT::i32;
639}
640
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000641// Answering this is somewhat tricky and depends on the specific device which
642// have different rates for fma or all f64 operations.
643//
644// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
645// regardless of which device (although the number of cycles differs between
646// devices), so it is always profitable for f64.
647//
648// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
649// only on full rate devices. Normally, we should prefer selecting v_mad_f32
650// which we can always do even without fused FP ops since it returns the same
651// result as the separate operations and since it is always full
652// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
653// however does not support denormals, so we do report fma as faster if we have
654// a fast fma device and require denormals.
655//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000656bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
657 VT = VT.getScalarType();
658
659 if (!VT.isSimple())
660 return false;
661
662 switch (VT.getSimpleVT().SimpleTy) {
663 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000664 // This is as fast on some subtargets. However, we always have full rate f32
665 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +0000666 // which we should prefer over fma. We can't use this if we want to support
667 // denormals, so only report this in these cases.
668 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000669 case MVT::f64:
670 return true;
671 default:
672 break;
673 }
674
675 return false;
676}
677
Tom Stellard75aadc22012-12-11 21:25:42 +0000678//===----------------------------------------------------------------------===//
679// Custom DAG Lowering Operations
680//===----------------------------------------------------------------------===//
681
682SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
683 switch (Op.getOpcode()) {
684 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +0000685 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000686 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000687 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +0000688 SDValue Result = LowerLOAD(Op, DAG);
689 assert((!Result.getNode() ||
690 Result.getNode()->getNumValues() == 2) &&
691 "Load should return a value and a chain");
692 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +0000693 }
Tom Stellardaf775432013-10-23 00:44:32 +0000694
Matt Arsenaultad14ce82014-07-19 18:44:39 +0000695 case ISD::FSIN:
696 case ISD::FCOS:
697 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000698 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000699 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000700 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000701 case ISD::GlobalAddress: {
702 MachineFunction &MF = DAG.getMachineFunction();
703 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
704 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +0000705 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000706 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
707 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000708 }
709 return SDValue();
710}
711
Tom Stellardf8794352012-12-19 22:10:31 +0000712/// \brief Helper function for LowerBRCOND
713static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000714
Tom Stellardf8794352012-12-19 22:10:31 +0000715 SDNode *Parent = Value.getNode();
716 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
717 I != E; ++I) {
718
719 if (I.getUse().get() != Value)
720 continue;
721
722 if (I->getOpcode() == Opcode)
723 return *I;
724 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000725 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000726}
727
Tom Stellardb02094e2014-07-21 15:45:01 +0000728SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
729
Tom Stellardb02094e2014-07-21 15:45:01 +0000730 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
731 unsigned FrameIndex = FINode->getIndex();
732
Tom Stellardb02094e2014-07-21 15:45:01 +0000733 return DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
734}
735
Tom Stellardf8794352012-12-19 22:10:31 +0000736/// This transforms the control flow intrinsics to get the branch destination as
737/// last parameter, also switches branch target with BR if the need arise
738SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
739 SelectionDAG &DAG) const {
740
Andrew Trickef9de2a2013-05-25 02:42:55 +0000741 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000742
743 SDNode *Intr = BRCOND.getOperand(1).getNode();
744 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +0000745 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000746
747 if (Intr->getOpcode() == ISD::SETCC) {
748 // As long as we negate the condition everything is fine
749 SDNode *SetCC = Intr;
750 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000751 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
752 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000753 Intr = SetCC->getOperand(0).getNode();
754
755 } else {
756 // Get the target from BR if we don't negate the condition
757 BR = findUser(BRCOND, ISD::BR);
758 Target = BR->getOperand(1);
759 }
760
761 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
762
763 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000764 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +0000765
766 // operands of the new intrinsic call
767 SmallVector<SDValue, 4> Ops;
768 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000769 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +0000770 Ops.push_back(Target);
771
772 // build the new intrinsic call
773 SDNode *Result = DAG.getNode(
774 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +0000775 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000776
777 if (BR) {
778 // Give the branch instruction our target
779 SDValue Ops[] = {
780 BR->getOperand(0),
781 BRCOND.getOperand(2)
782 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000783 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
784 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
785 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000786 }
787
788 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
789
790 // Copy the intrinsic results to registers
791 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
792 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
793 if (!CopyToReg)
794 continue;
795
796 Chain = DAG.getCopyToReg(
797 Chain, DL,
798 CopyToReg->getOperand(1),
799 SDValue(Result, i - 1),
800 SDValue());
801
802 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
803 }
804
805 // Remove the old intrinsic from the chain
806 DAG.ReplaceAllUsesOfValueWith(
807 SDValue(Intr, Intr->getNumValues() - 1),
808 Intr->getOperand(0));
809
810 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000811}
812
Tom Stellard067c8152014-07-21 14:01:14 +0000813SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
814 SDValue Op,
815 SelectionDAG &DAG) const {
816 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
817
818 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
819 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
820
821 SDLoc DL(GSD);
822 const GlobalValue *GV = GSD->getGlobal();
823 MVT PtrVT = getPointerTy(GSD->getAddressSpace());
824
825 SDValue Ptr = DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT);
826 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
827
828 SDValue PtrLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000829 DAG.getConstant(0, DL, MVT::i32));
Tom Stellard067c8152014-07-21 14:01:14 +0000830 SDValue PtrHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000831 DAG.getConstant(1, DL, MVT::i32));
Tom Stellard067c8152014-07-21 14:01:14 +0000832
833 SDValue Lo = DAG.getNode(ISD::ADDC, DL, DAG.getVTList(MVT::i32, MVT::Glue),
834 PtrLo, GA);
835 SDValue Hi = DAG.getNode(ISD::ADDE, DL, DAG.getVTList(MVT::i32, MVT::Glue),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000836 PtrHi, DAG.getConstant(0, DL, MVT::i32),
Tom Stellard067c8152014-07-21 14:01:14 +0000837 SDValue(Lo.getNode(), 1));
838 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
839}
840
Tom Stellardfc92e772015-05-12 14:18:14 +0000841SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL,
842 SDValue V) const {
843 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
844 // so we will end up with redundant moves to m0.
845 //
846 // We can't use S_MOV_B32, because there is no way to specify m0 as the
847 // destination register.
848 //
849 // We have to use them both. Machine cse will combine all the S_MOV_B32
850 // instructions and the register coalescer eliminate the extra copies.
851 SDNode *M0 = DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, V.getValueType(), V);
852 return DAG.getCopyToReg(Chain, DL, DAG.getRegister(AMDGPU::M0, MVT::i32),
853 SDValue(M0, 0), SDValue()); // Glue
854 // A Null SDValue creates
855 // a glue result.
856}
857
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000858SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
859 SelectionDAG &DAG) const {
860 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000861 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000862 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000863
864 EVT VT = Op.getValueType();
865 SDLoc DL(Op);
866 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
867
868 switch (IntrinsicID) {
869 case Intrinsic::r600_read_ngroups_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000870 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
871 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000872 case Intrinsic::r600_read_ngroups_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000873 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
874 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000875 case Intrinsic::r600_read_ngroups_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000876 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
877 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000878 case Intrinsic::r600_read_global_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000879 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
880 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000881 case Intrinsic::r600_read_global_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000882 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
883 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000884 case Intrinsic::r600_read_global_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000885 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
886 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000887 case Intrinsic::r600_read_local_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000888 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
889 SI::KernelInputOffsets::LOCAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000890 case Intrinsic::r600_read_local_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000891 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
892 SI::KernelInputOffsets::LOCAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000893 case Intrinsic::r600_read_local_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000894 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
895 SI::KernelInputOffsets::LOCAL_SIZE_Z, false);
Jan Veselye5121f32014-10-14 20:05:26 +0000896
897 case Intrinsic::AMDGPU_read_workdim:
898 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
899 MF.getInfo<SIMachineFunctionInfo>()->ABIArgOffset,
900 false);
901
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000902 case Intrinsic::r600_read_tgid_x:
903 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000904 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000905 case Intrinsic::r600_read_tgid_y:
906 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000907 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000908 case Intrinsic::r600_read_tgid_z:
909 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000910 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000911 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000912 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000913 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000914 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000915 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000916 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000917 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000918 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000919 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000920 case AMDGPUIntrinsic::SI_load_const: {
921 SDValue Ops[] = {
922 Op.getOperand(1),
923 Op.getOperand(2)
924 };
925
926 MachineMemOperand *MMO = MF.getMachineMemOperand(
927 MachinePointerInfo(),
928 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
929 VT.getStoreSize(), 4);
930 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
931 Op->getVTList(), Ops, VT, MMO);
932 }
933 case AMDGPUIntrinsic::SI_sample:
934 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
935 case AMDGPUIntrinsic::SI_sampleb:
936 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
937 case AMDGPUIntrinsic::SI_sampled:
938 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
939 case AMDGPUIntrinsic::SI_samplel:
940 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
941 case AMDGPUIntrinsic::SI_vs_load_input:
942 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
943 Op.getOperand(1),
944 Op.getOperand(2),
945 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +0000946
947 case AMDGPUIntrinsic::AMDGPU_fract:
948 case AMDGPUIntrinsic::AMDIL_fraction: // Legacy name.
949 return DAG.getNode(ISD::FSUB, DL, VT, Op.getOperand(1),
950 DAG.getNode(ISD::FFLOOR, DL, VT, Op.getOperand(1)));
951
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000952 default:
953 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
954 }
955}
956
957SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
958 SelectionDAG &DAG) const {
959 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +0000960 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000961 SDValue Chain = Op.getOperand(0);
962 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
963
964 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +0000965 case AMDGPUIntrinsic::SI_sendmsg: {
966 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
967 SDValue Glue = Chain.getValue(1);
968 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
969 Op.getOperand(2), Glue);
970 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000971 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000972 SDValue Ops[] = {
973 Chain,
974 Op.getOperand(2),
975 Op.getOperand(3),
976 Op.getOperand(4),
977 Op.getOperand(5),
978 Op.getOperand(6),
979 Op.getOperand(7),
980 Op.getOperand(8),
981 Op.getOperand(9),
982 Op.getOperand(10),
983 Op.getOperand(11),
984 Op.getOperand(12),
985 Op.getOperand(13),
986 Op.getOperand(14)
987 };
988
989 EVT VT = Op.getOperand(3).getValueType();
990
991 MachineMemOperand *MMO = MF.getMachineMemOperand(
992 MachinePointerInfo(),
993 MachineMemOperand::MOStore,
994 VT.getStoreSize(), 4);
995 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
996 Op->getVTList(), Ops, VT, MMO);
997 }
998 default:
999 return SDValue();
1000 }
1001}
1002
Tom Stellard81d871d2013-11-13 23:36:50 +00001003SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1004 SDLoc DL(Op);
1005 LoadSDNode *Load = cast<LoadSDNode>(Op);
1006
Tom Stellarde812f2f2014-07-21 15:45:06 +00001007 if (Op.getValueType().isVector()) {
1008 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1009 "Custom lowering for non-i32 vectors hasn't been implemented.");
1010 unsigned NumElements = Op.getValueType().getVectorNumElements();
1011 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
1012 switch (Load->getAddressSpace()) {
1013 default: break;
1014 case AMDGPUAS::GLOBAL_ADDRESS:
1015 case AMDGPUAS::PRIVATE_ADDRESS:
1016 // v4 loads are supported for private and global memory.
1017 if (NumElements <= 4)
1018 break;
1019 // fall-through
1020 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenault83e60582014-07-24 17:10:35 +00001021 return ScalarizeVectorLoad(Op, DAG);
Tom Stellarde812f2f2014-07-21 15:45:06 +00001022 }
Tom Stellarde9373602014-01-22 19:24:14 +00001023 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001024
Tom Stellarde812f2f2014-07-21 15:45:06 +00001025 return AMDGPUTargetLowering::LowerLOAD(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001026}
1027
Tom Stellard9fa17912013-08-14 23:24:45 +00001028SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
1029 const SDValue &Op,
1030 SelectionDAG &DAG) const {
1031 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
1032 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +00001033 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +00001034 Op.getOperand(4));
1035}
1036
Tom Stellard0ec134f2014-02-04 17:18:40 +00001037SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1038 if (Op.getValueType() != MVT::i64)
1039 return SDValue();
1040
1041 SDLoc DL(Op);
1042 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001043
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001044 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1045 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001046
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001047 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1048 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1049
1050 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1051 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001052
1053 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1054
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001055 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1056 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001057
1058 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1059
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001060 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1061 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001062}
1063
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001064// Catch division cases where we can use shortcuts with rcp and rsq
1065// instructions.
1066SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001067 SDLoc SL(Op);
1068 SDValue LHS = Op.getOperand(0);
1069 SDValue RHS = Op.getOperand(1);
1070 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001071 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001072
1073 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001074 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1075 CLHS->isExactlyValue(1.0)) {
1076 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1077 // the CI documentation has a worst case error of 1 ulp.
1078 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1079 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001080
1081 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001082 //
1083 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1084 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001085 if (RHS.getOpcode() == ISD::FSQRT)
1086 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1087
1088 // 1.0 / x -> rcp(x)
1089 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1090 }
1091 }
1092
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001093 if (Unsafe) {
1094 // Turn into multiply by the reciprocal.
1095 // x / y -> x * (1.0 / y)
1096 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1097 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip);
1098 }
1099
1100 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001101}
1102
1103SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001104 SDValue FastLowered = LowerFastFDIV(Op, DAG);
1105 if (FastLowered.getNode())
1106 return FastLowered;
1107
1108 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1109 // selection error for now rather than do something incorrect.
1110 if (Subtarget->hasFP32Denormals())
1111 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001112
1113 SDLoc SL(Op);
1114 SDValue LHS = Op.getOperand(0);
1115 SDValue RHS = Op.getOperand(1);
1116
1117 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1118
1119 const APFloat K0Val(BitsToFloat(0x6f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001120 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001121
1122 const APFloat K1Val(BitsToFloat(0x2f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001123 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001124
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001125 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001126
1127 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f32);
1128
1129 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1130
1131 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1132
1133 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1134
1135 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1136
1137 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1138
1139 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1140}
1141
1142SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001143 if (DAG.getTarget().Options.UnsafeFPMath)
1144 return LowerFastFDIV(Op, DAG);
1145
1146 SDLoc SL(Op);
1147 SDValue X = Op.getOperand(0);
1148 SDValue Y = Op.getOperand(1);
1149
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001150 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001151
1152 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
1153
1154 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
1155
1156 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
1157
1158 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
1159
1160 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
1161
1162 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
1163
1164 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
1165
1166 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
1167
1168 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
1169 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
1170
1171 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
1172 NegDivScale0, Mul, DivScale1);
1173
1174 SDValue Scale;
1175
1176 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1177 // Workaround a hardware bug on SI where the condition output from div_scale
1178 // is not usable.
1179
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001180 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001181
1182 // Figure out if the scale to use for div_fmas.
1183 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1184 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
1185 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
1186 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
1187
1188 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
1189 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
1190
1191 SDValue Scale0Hi
1192 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
1193 SDValue Scale1Hi
1194 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
1195
1196 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
1197 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
1198 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
1199 } else {
1200 Scale = DivScale1.getValue(1);
1201 }
1202
1203 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
1204 Fma4, Fma3, Mul, Scale);
1205
1206 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001207}
1208
1209SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1210 EVT VT = Op.getValueType();
1211
1212 if (VT == MVT::f32)
1213 return LowerFDIV32(Op, DAG);
1214
1215 if (VT == MVT::f64)
1216 return LowerFDIV64(Op, DAG);
1217
1218 llvm_unreachable("Unexpected type for fdiv");
1219}
1220
Tom Stellard81d871d2013-11-13 23:36:50 +00001221SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1222 SDLoc DL(Op);
1223 StoreSDNode *Store = cast<StoreSDNode>(Op);
1224 EVT VT = Store->getMemoryVT();
1225
Tom Stellard9b3816b2014-06-24 23:33:04 +00001226 // These stores are legal.
Tom Stellardb02094e2014-07-21 15:45:01 +00001227 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1228 if (VT.isVector() && VT.getVectorNumElements() > 4)
Matt Arsenault83e60582014-07-24 17:10:35 +00001229 return ScalarizeVectorStore(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001230 return SDValue();
1231 }
1232
Tom Stellard81d871d2013-11-13 23:36:50 +00001233 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1234 if (Ret.getNode())
1235 return Ret;
1236
1237 if (VT.isVector() && VT.getVectorNumElements() >= 8)
Matt Arsenault83e60582014-07-24 17:10:35 +00001238 return ScalarizeVectorStore(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001239
Tom Stellard1c8788e2014-03-07 20:12:33 +00001240 if (VT == MVT::i1)
1241 return DAG.getTruncStore(Store->getChain(), DL,
1242 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1243 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1244
Tom Stellarde812f2f2014-07-21 15:45:06 +00001245 return SDValue();
Tom Stellard81d871d2013-11-13 23:36:50 +00001246}
1247
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001248SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001249 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001250 EVT VT = Op.getValueType();
1251 SDValue Arg = Op.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001252 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
1253 DAG.getNode(ISD::FMUL, DL, VT, Arg,
1254 DAG.getConstantFP(0.5/M_PI, DL,
1255 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001256
1257 switch (Op.getOpcode()) {
1258 case ISD::FCOS:
1259 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1260 case ISD::FSIN:
1261 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1262 default:
1263 llvm_unreachable("Wrong trig opcode");
1264 }
1265}
1266
Tom Stellard75aadc22012-12-11 21:25:42 +00001267//===----------------------------------------------------------------------===//
1268// Custom DAG optimizations
1269//===----------------------------------------------------------------------===//
1270
Matt Arsenault364a6742014-06-11 17:50:44 +00001271SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00001272 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00001273 EVT VT = N->getValueType(0);
1274 EVT ScalarVT = VT.getScalarType();
1275 if (ScalarVT != MVT::f32)
1276 return SDValue();
1277
1278 SelectionDAG &DAG = DCI.DAG;
1279 SDLoc DL(N);
1280
1281 SDValue Src = N->getOperand(0);
1282 EVT SrcVT = Src.getValueType();
1283
1284 // TODO: We could try to match extracting the higher bytes, which would be
1285 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1286 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1287 // about in practice.
1288 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1289 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1290 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1291 DCI.AddToWorklist(Cvt.getNode());
1292 return Cvt;
1293 }
1294 }
1295
1296 // We are primarily trying to catch operations on illegal vector types
1297 // before they are expanded.
1298 // For scalars, we can use the more flexible method of checking masked bits
1299 // after legalization.
1300 if (!DCI.isBeforeLegalize() ||
1301 !SrcVT.isVector() ||
1302 SrcVT.getVectorElementType() != MVT::i8) {
1303 return SDValue();
1304 }
1305
1306 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1307
1308 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1309 // size as 4.
1310 unsigned NElts = SrcVT.getVectorNumElements();
1311 if (!SrcVT.isSimple() && NElts != 3)
1312 return SDValue();
1313
1314 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1315 // prevent a mess from expanding to v4i32 and repacking.
1316 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1317 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1318 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1319 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00001320 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001321
1322 unsigned AS = Load->getAddressSpace();
1323 unsigned Align = Load->getAlignment();
1324 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
1325 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
1326
1327 // Don't try to replace the load if we have to expand it due to alignment
1328 // problems. Otherwise we will end up scalarizing the load, and trying to
1329 // repack into the vector for no real reason.
1330 if (Align < ABIAlignment &&
1331 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
1332 return SDValue();
1333 }
1334
Matt Arsenault364a6742014-06-11 17:50:44 +00001335 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1336 Load->getChain(),
1337 Load->getBasePtr(),
1338 LoadVT,
1339 Load->getMemOperand());
1340
1341 // Make sure successors of the original load stay after it by updating
1342 // them to use the new Chain.
1343 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1344
1345 SmallVector<SDValue, 4> Elts;
1346 if (RegVT.isVector())
1347 DAG.ExtractVectorElements(NewLoad, Elts);
1348 else
1349 Elts.push_back(NewLoad);
1350
1351 SmallVector<SDValue, 4> Ops;
1352
1353 unsigned EltIdx = 0;
1354 for (SDValue Elt : Elts) {
1355 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1356 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1357 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1358 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1359 DCI.AddToWorklist(Cvt.getNode());
1360 Ops.push_back(Cvt);
1361 }
1362
1363 ++EltIdx;
1364 }
1365
1366 assert(Ops.size() == NElts);
1367
1368 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1369 }
1370
1371 return SDValue();
1372}
1373
Eric Christopher6c5b5112015-03-11 18:43:21 +00001374/// \brief Return true if the given offset Size in bytes can be folded into
1375/// the immediate offsets of a memory instruction for the given address space.
1376static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
1377 const AMDGPUSubtarget &STI) {
1378 switch (AS) {
1379 case AMDGPUAS::GLOBAL_ADDRESS: {
1380 // MUBUF instructions a 12-bit offset in bytes.
1381 return isUInt<12>(OffsetSize);
1382 }
1383 case AMDGPUAS::CONSTANT_ADDRESS: {
1384 // SMRD instructions have an 8-bit offset in dwords on SI and
1385 // a 20-bit offset in bytes on VI.
1386 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
1387 return isUInt<20>(OffsetSize);
1388 else
1389 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
1390 }
1391 case AMDGPUAS::LOCAL_ADDRESS:
1392 case AMDGPUAS::REGION_ADDRESS: {
1393 // The single offset versions have a 16-bit offset in bytes.
1394 return isUInt<16>(OffsetSize);
1395 }
1396 case AMDGPUAS::PRIVATE_ADDRESS:
1397 // Indirect register addressing does not use any offsets.
1398 default:
1399 return 0;
1400 }
1401}
1402
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001403// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
1404
1405// This is a variant of
1406// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
1407//
1408// The normal DAG combiner will do this, but only if the add has one use since
1409// that would increase the number of instructions.
1410//
1411// This prevents us from seeing a constant offset that can be folded into a
1412// memory instruction's addressing mode. If we know the resulting add offset of
1413// a pointer can be folded into an addressing offset, we can replace the pointer
1414// operand with the add of new constant offset. This eliminates one of the uses,
1415// and may allow the remaining use to also be simplified.
1416//
1417SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
1418 unsigned AddrSpace,
1419 DAGCombinerInfo &DCI) const {
1420 SDValue N0 = N->getOperand(0);
1421 SDValue N1 = N->getOperand(1);
1422
1423 if (N0.getOpcode() != ISD::ADD)
1424 return SDValue();
1425
1426 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
1427 if (!CN1)
1428 return SDValue();
1429
1430 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1431 if (!CAdd)
1432 return SDValue();
1433
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001434 // If the resulting offset is too large, we can't fold it into the addressing
1435 // mode offset.
1436 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Eric Christopher6c5b5112015-03-11 18:43:21 +00001437 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001438 return SDValue();
1439
1440 SelectionDAG &DAG = DCI.DAG;
1441 SDLoc SL(N);
1442 EVT VT = N->getValueType(0);
1443
1444 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001445 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001446
1447 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
1448}
1449
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001450SDValue SITargetLowering::performAndCombine(SDNode *N,
1451 DAGCombinerInfo &DCI) const {
1452 if (DCI.isBeforeLegalize())
1453 return SDValue();
1454
1455 SelectionDAG &DAG = DCI.DAG;
1456
1457 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
1458 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
1459 SDValue LHS = N->getOperand(0);
1460 SDValue RHS = N->getOperand(1);
1461
1462 if (LHS.getOpcode() == ISD::SETCC &&
1463 RHS.getOpcode() == ISD::SETCC) {
1464 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
1465 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
1466
1467 SDValue X = LHS.getOperand(0);
1468 SDValue Y = RHS.getOperand(0);
1469 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
1470 return SDValue();
1471
1472 if (LCC == ISD::SETO) {
1473 if (X != LHS.getOperand(1))
1474 return SDValue();
1475
1476 if (RCC == ISD::SETUNE) {
1477 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
1478 if (!C1 || !C1->isInfinity() || C1->isNegative())
1479 return SDValue();
1480
1481 const uint32_t Mask = SIInstrFlags::N_NORMAL |
1482 SIInstrFlags::N_SUBNORMAL |
1483 SIInstrFlags::N_ZERO |
1484 SIInstrFlags::P_ZERO |
1485 SIInstrFlags::P_SUBNORMAL |
1486 SIInstrFlags::P_NORMAL;
1487
1488 static_assert(((~(SIInstrFlags::S_NAN |
1489 SIInstrFlags::Q_NAN |
1490 SIInstrFlags::N_INFINITY |
1491 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
1492 "mask not equal");
1493
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001494 SDLoc DL(N);
1495 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
1496 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001497 }
1498 }
1499 }
1500
1501 return SDValue();
1502}
1503
Matt Arsenaultf2290332015-01-06 23:00:39 +00001504SDValue SITargetLowering::performOrCombine(SDNode *N,
1505 DAGCombinerInfo &DCI) const {
1506 SelectionDAG &DAG = DCI.DAG;
1507 SDValue LHS = N->getOperand(0);
1508 SDValue RHS = N->getOperand(1);
1509
1510 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
1511 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
1512 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
1513 SDValue Src = LHS.getOperand(0);
1514 if (Src != RHS.getOperand(0))
1515 return SDValue();
1516
1517 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
1518 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
1519 if (!CLHS || !CRHS)
1520 return SDValue();
1521
1522 // Only 10 bits are used.
1523 static const uint32_t MaxMask = 0x3ff;
1524
1525 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001526 SDLoc DL(N);
1527 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
1528 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00001529 }
1530
1531 return SDValue();
1532}
1533
1534SDValue SITargetLowering::performClassCombine(SDNode *N,
1535 DAGCombinerInfo &DCI) const {
1536 SelectionDAG &DAG = DCI.DAG;
1537 SDValue Mask = N->getOperand(1);
1538
1539 // fp_class x, 0 -> false
1540 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
1541 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001542 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00001543 }
1544
1545 return SDValue();
1546}
1547
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001548static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
1549 switch (Opc) {
1550 case ISD::FMAXNUM:
1551 return AMDGPUISD::FMAX3;
1552 case AMDGPUISD::SMAX:
1553 return AMDGPUISD::SMAX3;
1554 case AMDGPUISD::UMAX:
1555 return AMDGPUISD::UMAX3;
1556 case ISD::FMINNUM:
1557 return AMDGPUISD::FMIN3;
1558 case AMDGPUISD::SMIN:
1559 return AMDGPUISD::SMIN3;
1560 case AMDGPUISD::UMIN:
1561 return AMDGPUISD::UMIN3;
1562 default:
1563 llvm_unreachable("Not a min/max opcode");
1564 }
1565}
1566
1567SDValue SITargetLowering::performMin3Max3Combine(SDNode *N,
1568 DAGCombinerInfo &DCI) const {
1569 SelectionDAG &DAG = DCI.DAG;
1570
1571 unsigned Opc = N->getOpcode();
1572 SDValue Op0 = N->getOperand(0);
1573 SDValue Op1 = N->getOperand(1);
1574
1575 // Only do this if the inner op has one use since this will just increases
1576 // register pressure for no benefit.
1577
1578 // max(max(a, b), c)
1579 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
1580 SDLoc DL(N);
1581 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1582 DL,
1583 N->getValueType(0),
1584 Op0.getOperand(0),
1585 Op0.getOperand(1),
1586 Op1);
1587 }
1588
1589 // max(a, max(b, c))
1590 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
1591 SDLoc DL(N);
1592 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1593 DL,
1594 N->getValueType(0),
1595 Op0,
1596 Op1.getOperand(0),
1597 Op1.getOperand(1));
1598 }
1599
1600 return SDValue();
1601}
1602
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001603SDValue SITargetLowering::performSetCCCombine(SDNode *N,
1604 DAGCombinerInfo &DCI) const {
1605 SelectionDAG &DAG = DCI.DAG;
1606 SDLoc SL(N);
1607
1608 SDValue LHS = N->getOperand(0);
1609 SDValue RHS = N->getOperand(1);
1610 EVT VT = LHS.getValueType();
1611
1612 if (VT != MVT::f32 && VT != MVT::f64)
1613 return SDValue();
1614
1615 // Match isinf pattern
1616 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
1617 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
1618 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
1619 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
1620 if (!CRHS)
1621 return SDValue();
1622
1623 const APFloat &APF = CRHS->getValueAPF();
1624 if (APF.isInfinity() && !APF.isNegative()) {
1625 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001626 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
1627 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001628 }
1629 }
1630
1631 return SDValue();
1632}
1633
Tom Stellard75aadc22012-12-11 21:25:42 +00001634SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1635 DAGCombinerInfo &DCI) const {
1636 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001637 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001638
1639 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00001640 default:
1641 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001642 case ISD::SETCC:
1643 return performSetCCCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001644 case ISD::FMAXNUM: // TODO: What about fmax_legacy?
1645 case ISD::FMINNUM:
1646 case AMDGPUISD::SMAX:
1647 case AMDGPUISD::SMIN:
1648 case AMDGPUISD::UMAX:
1649 case AMDGPUISD::UMIN: {
1650 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00001651 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001652 getTargetMachine().getOptLevel() > CodeGenOpt::None)
1653 return performMin3Max3Combine(N, DCI);
1654 break;
1655 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001656
1657 case AMDGPUISD::CVT_F32_UBYTE0:
1658 case AMDGPUISD::CVT_F32_UBYTE1:
1659 case AMDGPUISD::CVT_F32_UBYTE2:
1660 case AMDGPUISD::CVT_F32_UBYTE3: {
1661 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
1662
1663 SDValue Src = N->getOperand(0);
1664 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
1665
1666 APInt KnownZero, KnownOne;
1667 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1668 !DCI.isBeforeLegalizeOps());
1669 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1670 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
1671 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
1672 DCI.CommitTargetLoweringOpt(TLO);
1673 }
1674
1675 break;
1676 }
1677
1678 case ISD::UINT_TO_FP: {
1679 return performUCharToFloatCombine(N, DCI);
Matt Arsenault8675db12014-08-29 16:01:14 +00001680
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001681 case ISD::FADD: {
1682 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1683 break;
1684
1685 EVT VT = N->getValueType(0);
1686 if (VT != MVT::f32)
1687 break;
1688
Matt Arsenault8d630032015-02-20 22:10:41 +00001689 // Only do this if we are not trying to support denormals. v_mad_f32 does
1690 // not support denormals ever.
1691 if (Subtarget->hasFP32Denormals())
1692 break;
1693
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001694 SDValue LHS = N->getOperand(0);
1695 SDValue RHS = N->getOperand(1);
1696
1697 // These should really be instruction patterns, but writing patterns with
1698 // source modiifiers is a pain.
1699
1700 // fadd (fadd (a, a), b) -> mad 2.0, a, b
1701 if (LHS.getOpcode() == ISD::FADD) {
1702 SDValue A = LHS.getOperand(0);
1703 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001704 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00001705 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001706 }
1707 }
1708
1709 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
1710 if (RHS.getOpcode() == ISD::FADD) {
1711 SDValue A = RHS.getOperand(0);
1712 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001713 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00001714 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001715 }
1716 }
1717
Matt Arsenault8d630032015-02-20 22:10:41 +00001718 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001719 }
Matt Arsenault8675db12014-08-29 16:01:14 +00001720 case ISD::FSUB: {
1721 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1722 break;
1723
1724 EVT VT = N->getValueType(0);
1725
1726 // Try to get the fneg to fold into the source modifier. This undoes generic
1727 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00001728 //
1729 // Only do this if we are not trying to support denormals. v_mad_f32 does
1730 // not support denormals ever.
1731 if (VT == MVT::f32 &&
1732 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00001733 SDValue LHS = N->getOperand(0);
1734 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001735 if (LHS.getOpcode() == ISD::FADD) {
1736 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
1737
1738 SDValue A = LHS.getOperand(0);
1739 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001740 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001741 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
1742
Matt Arsenault8d630032015-02-20 22:10:41 +00001743 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001744 }
1745 }
1746
1747 if (RHS.getOpcode() == ISD::FADD) {
1748 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
1749
1750 SDValue A = RHS.getOperand(0);
1751 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001752 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00001753 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001754 }
1755 }
Matt Arsenault8d630032015-02-20 22:10:41 +00001756
1757 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00001758 }
1759
1760 break;
1761 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001762 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001763 case ISD::LOAD:
1764 case ISD::STORE:
1765 case ISD::ATOMIC_LOAD:
1766 case ISD::ATOMIC_STORE:
1767 case ISD::ATOMIC_CMP_SWAP:
1768 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
1769 case ISD::ATOMIC_SWAP:
1770 case ISD::ATOMIC_LOAD_ADD:
1771 case ISD::ATOMIC_LOAD_SUB:
1772 case ISD::ATOMIC_LOAD_AND:
1773 case ISD::ATOMIC_LOAD_OR:
1774 case ISD::ATOMIC_LOAD_XOR:
1775 case ISD::ATOMIC_LOAD_NAND:
1776 case ISD::ATOMIC_LOAD_MIN:
1777 case ISD::ATOMIC_LOAD_MAX:
1778 case ISD::ATOMIC_LOAD_UMIN:
1779 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
1780 if (DCI.isBeforeLegalize())
1781 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001782
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001783 MemSDNode *MemNode = cast<MemSDNode>(N);
1784 SDValue Ptr = MemNode->getBasePtr();
1785
1786 // TODO: We could also do this for multiplies.
1787 unsigned AS = MemNode->getAddressSpace();
1788 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
1789 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
1790 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001791 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001792
1793 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
1794 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
1795 }
1796 }
1797 break;
1798 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001799 case ISD::AND:
1800 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00001801 case ISD::OR:
1802 return performOrCombine(N, DCI);
1803 case AMDGPUISD::FP_CLASS:
1804 return performClassCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001805 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001806 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001807}
Christian Konigd910b7d2013-02-26 17:52:16 +00001808
Christian Konigf82901a2013-02-26 17:52:23 +00001809/// \brief Analyze the possible immediate value Op
1810///
1811/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1812/// and the immediate value if it's a literal immediate
1813int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1814
Eric Christopher7792e322015-01-30 23:24:40 +00001815 const SIInstrInfo *TII =
1816 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001817
Tom Stellardedbf1eb2013-04-05 23:31:20 +00001818 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00001819 if (TII->isInlineConstant(Node->getAPIntValue()))
1820 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00001821
Matt Arsenault11a4d672015-02-13 19:05:03 +00001822 uint64_t Val = Node->getZExtValue();
1823 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00001824 }
1825
1826 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1827 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
1828 return 0;
1829
1830 if (Node->getValueType(0) == MVT::f32)
1831 return FloatToBits(Node->getValueAPF().convertToFloat());
1832
1833 return -1;
1834 }
1835
1836 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001837}
1838
Christian Konig8e06e2a2013-04-10 08:39:08 +00001839/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00001840static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00001841 switch (Idx) {
1842 default: return 0;
1843 case AMDGPU::sub0: return 0;
1844 case AMDGPU::sub1: return 1;
1845 case AMDGPU::sub2: return 2;
1846 case AMDGPU::sub3: return 3;
1847 }
1848}
1849
1850/// \brief Adjust the writemask of MIMG instructions
1851void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1852 SelectionDAG &DAG) const {
1853 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00001854 unsigned Lane = 0;
1855 unsigned OldDmask = Node->getConstantOperandVal(0);
1856 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001857
1858 // Try to figure out the used register components
1859 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1860 I != E; ++I) {
1861
1862 // Abort if we can't understand the usage
1863 if (!I->isMachineOpcode() ||
1864 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1865 return;
1866
Tom Stellard54774e52013-10-23 02:53:47 +00001867 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1868 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1869 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1870 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00001871 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001872
Tom Stellard54774e52013-10-23 02:53:47 +00001873 // Set which texture component corresponds to the lane.
1874 unsigned Comp;
1875 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1876 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00001877 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00001878 Dmask &= ~(1 << Comp);
1879 }
1880
Christian Konig8e06e2a2013-04-10 08:39:08 +00001881 // Abort if we have more than one user per component
1882 if (Users[Lane])
1883 return;
1884
1885 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00001886 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001887 }
1888
Tom Stellard54774e52013-10-23 02:53:47 +00001889 // Abort if there's no change
1890 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00001891 return;
1892
1893 // Adjust the writemask in the node
1894 std::vector<SDValue> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001895 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001896 Ops.insert(Ops.end(), Node->op_begin() + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00001897 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001898
Christian Konig8b1ed282013-04-10 08:39:16 +00001899 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00001900 // (if NewDmask has only one bit set...)
1901 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001902 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
1903 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00001904 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001905 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00001906 SDValue(Node, 0), RC);
1907 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1908 return;
1909 }
1910
Christian Konig8e06e2a2013-04-10 08:39:08 +00001911 // Update the users of the node with the new indices
1912 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1913
1914 SDNode *User = Users[i];
1915 if (!User)
1916 continue;
1917
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001918 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001919 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1920
1921 switch (Idx) {
1922 default: break;
1923 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1924 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1925 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1926 }
1927 }
1928}
1929
Tom Stellard3457a842014-10-09 19:06:00 +00001930/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
1931/// with frame index operands.
1932/// LLVM assumes that inputs are to these instructions are registers.
1933void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
1934 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00001935
1936 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00001937 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
1938 if (!isa<FrameIndexSDNode>(Node->getOperand(i))) {
1939 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00001940 continue;
1941 }
1942
Tom Stellard3457a842014-10-09 19:06:00 +00001943 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00001944 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00001945 Node->getOperand(i).getValueType(),
1946 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00001947 }
1948
Tom Stellard3457a842014-10-09 19:06:00 +00001949 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00001950}
1951
Matt Arsenault08d84942014-06-03 23:06:13 +00001952/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00001953SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1954 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00001955 const SIInstrInfo *TII =
1956 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konig8e06e2a2013-04-10 08:39:08 +00001957
Tom Stellard16a9a202013-08-14 23:24:17 +00001958 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00001959 adjustWritemask(Node, DAG);
1960
Matt Arsenault7d858d82014-11-02 23:46:54 +00001961 if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG ||
1962 Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00001963 legalizeTargetIndependentNode(Node, DAG);
1964 return Node;
1965 }
Tom Stellard654d6692015-01-08 15:08:17 +00001966 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001967}
Christian Konig8b1ed282013-04-10 08:39:16 +00001968
1969/// \brief Assign the register class depending on the number of
1970/// bits set in the writemask
1971void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1972 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00001973 const SIInstrInfo *TII =
1974 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001975
Tom Stellarda99ada52014-11-21 22:31:44 +00001976 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00001977 TII->legalizeOperands(MI);
1978
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001979 if (TII->isMIMG(MI->getOpcode())) {
1980 unsigned VReg = MI->getOperand(0).getReg();
1981 unsigned Writemask = MI->getOperand(1).getImm();
1982 unsigned BitsSet = 0;
1983 for (unsigned i = 0; i < 4; ++i)
1984 BitsSet += Writemask & (1 << i) ? 1 : 0;
1985
1986 const TargetRegisterClass *RC;
1987 switch (BitsSet) {
1988 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001989 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001990 case 2: RC = &AMDGPU::VReg_64RegClass; break;
1991 case 3: RC = &AMDGPU::VReg_96RegClass; break;
1992 }
1993
1994 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1995 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001996 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00001997 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00001998 }
1999
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002000 // Replace unused atomics with the no return version.
2001 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
2002 if (NoRetAtomicOp != -1) {
2003 if (!Node->hasAnyUseOfValue(0)) {
2004 MI->setDesc(TII->get(NoRetAtomicOp));
2005 MI->RemoveOperand(0);
2006 }
2007
2008 return;
2009 }
Christian Konig8b1ed282013-04-10 08:39:16 +00002010}
Tom Stellard0518ff82013-06-03 17:39:58 +00002011
Matt Arsenault485defe2014-11-05 19:01:17 +00002012static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002013 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00002014 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
2015}
2016
2017MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
2018 SDLoc DL,
2019 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002020 const SIInstrInfo *TII =
2021 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00002022#if 1
2023 // XXX - Workaround for moveToVALU not handling different register class
2024 // inserts for REG_SEQUENCE.
2025
2026 // Build the half of the subregister with the constants.
2027 const SDValue Ops0[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002028 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002029 buildSMovImm32(DAG, DL, 0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002030 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Tom Stellard794c8c02014-12-02 17:05:41 +00002031 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002032 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
Matt Arsenault485defe2014-11-05 19:01:17 +00002033 };
2034
2035 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
2036 MVT::v2i32, Ops0), 0);
2037
2038 // Combine the constants and the pointer.
2039 const SDValue Ops1[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002040 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002041 Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002042 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002043 SubRegHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002044 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
Matt Arsenault485defe2014-11-05 19:01:17 +00002045 };
2046
2047 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
2048#else
2049 const SDValue Ops[] = {
2050 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2051 Ptr,
2052 DAG.getTargetConstant(AMDGPU::sub0_sub1, MVT::i32),
2053 buildSMovImm32(DAG, DL, 0),
2054 DAG.getTargetConstant(AMDGPU::sub2, MVT::i32),
Tom Stellard794c8c02014-12-02 17:05:41 +00002055 buildSMovImm32(DAG, DL, TII->getDefaultRsrcFormat() >> 32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002056 DAG.getTargetConstant(AMDGPU::sub3, MVT::i32)
2057 };
2058
2059 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2060
2061#endif
2062}
2063
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002064/// \brief Return a resource descriptor with the 'Add TID' bit enabled
2065/// The TID (Thread ID) is multipled by the stride value (bits [61:48]
2066/// of the resource descriptor) to create an offset, which is added to the
2067/// resource ponter.
2068MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
2069 SDLoc DL,
2070 SDValue Ptr,
2071 uint32_t RsrcDword1,
2072 uint64_t RsrcDword2And3) const {
2073 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
2074 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
2075 if (RsrcDword1) {
2076 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002077 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
2078 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002079 }
2080
2081 SDValue DataLo = buildSMovImm32(DAG, DL,
2082 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
2083 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
2084
2085 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002086 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002087 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002088 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002089 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002090 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002091 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002092 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002093 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002094 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002095 };
2096
2097 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2098}
2099
2100MachineSDNode *SITargetLowering::buildScratchRSRC(SelectionDAG &DAG,
2101 SDLoc DL,
2102 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002103 const SIInstrInfo *TII =
2104 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellard794c8c02014-12-02 17:05:41 +00002105 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() | AMDGPU::RSRC_TID_ENABLE |
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002106 0xffffffff; // Size
2107
2108 return buildRSRC(DAG, DL, Ptr, 0, Rsrc);
2109}
2110
Tom Stellard94593ee2013-06-03 17:40:18 +00002111SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2112 const TargetRegisterClass *RC,
2113 unsigned Reg, EVT VT) const {
2114 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2115
2116 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2117 cast<RegisterSDNode>(VReg)->getReg(), VT);
2118}
Tom Stellardd7e6f132015-04-08 01:09:26 +00002119
2120//===----------------------------------------------------------------------===//
2121// SI Inline Assembly Support
2122//===----------------------------------------------------------------------===//
2123
2124std::pair<unsigned, const TargetRegisterClass *>
2125SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
2126 const std::string &Constraint,
2127 MVT VT) const {
2128 if (Constraint == "r") {
2129 switch(VT.SimpleTy) {
2130 default: llvm_unreachable("Unhandled type for 'r' inline asm constraint");
2131 case MVT::i64:
2132 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
2133 case MVT::i32:
2134 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
2135 }
2136 }
2137
2138 if (Constraint.size() > 1) {
2139 const TargetRegisterClass *RC = nullptr;
2140 if (Constraint[1] == 'v') {
2141 RC = &AMDGPU::VGPR_32RegClass;
2142 } else if (Constraint[1] == 's') {
2143 RC = &AMDGPU::SGPR_32RegClass;
2144 }
2145
2146 if (RC) {
2147 unsigned Idx = std::atoi(Constraint.substr(2).c_str());
2148 if (Idx < RC->getNumRegs())
2149 return std::make_pair(RC->getRegister(Idx), RC);
2150 }
2151 }
2152 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
2153}