blob: 6ff17ec1f067bb009c82a36e8e9397c7500b0fb4 [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
63 computeRegisterProperties();
64
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::f32, Legal);
80 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
81 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
82 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
83
Tom Stellard35bb18c2013-08-26 15:06:04 +000084 // We need to custom lower vector stores from local memory
Tom Stellard35bb18c2013-08-26 15:06:04 +000085 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000086 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
87 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
88
89 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
90 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000091
Tom Stellard1c8788e2014-03-07 20:12:33 +000092 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000093 setOperationAction(ISD::STORE, MVT::i32, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000094 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
95 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
96
Tom Stellard0ec134f2014-02-04 17:18:40 +000097 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +000098 setOperationAction(ISD::SELECT, MVT::f64, Promote);
99 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000100
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000101 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
102 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
103 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
104 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000105
Tom Stellard83747202013-07-18 21:43:53 +0000106 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
107 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
108
Matt Arsenaulte306a322014-10-21 16:25:08 +0000109 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
110
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000111 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
114
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000115 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000116 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
118
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
122
Matt Arsenault94812212014-11-14 18:18:16 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
125
Tom Stellard94593ee2013-06-03 17:40:18 +0000126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000127 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
128 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
129 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000130
Tom Stellardafcf12f2013-09-12 02:55:14 +0000131 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000132 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000133
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000134 for (MVT VT : MVT::integer_valuetypes()) {
Matt Arsenaultbd223422015-01-14 01:35:17 +0000135 if (VT == MVT::i64)
136 continue;
137
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000138 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000139 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
140 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000141 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000142
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000143 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000144 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
145 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000146 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000147
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000148 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000149 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
150 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000151 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
152 }
153
154 for (MVT VT : MVT::integer_vector_valuetypes()) {
155 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
156 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
157 }
158
159 for (MVT VT : MVT::fp_valuetypes())
160 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000161
Tom Stellarde9373602014-01-22 19:24:14 +0000162 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
163 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000164 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Matt Arsenault6f243792013-09-05 19:41:10 +0000165 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000166 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
167 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000168
Matt Arsenault470acd82014-04-15 22:28:39 +0000169 setOperationAction(ISD::LOAD, MVT::i1, Custom);
170
Tom Stellardfd155822013-08-26 15:05:36 +0000171 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000172 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000173 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000174
Tom Stellard5f337882014-04-29 23:12:43 +0000175 // These should use UDIVREM, so set them to expand
176 setOperationAction(ISD::UDIV, MVT::i64, Expand);
177 setOperationAction(ISD::UREM, MVT::i64, Expand);
178
Tom Stellard967bf582014-02-13 23:34:15 +0000179 // We only support LOAD/STORE and vector manipulation ops for vectors
180 // with > 4 elements.
181 MVT VecTypes[] = {
Tom Stellardd61a1c32014-02-28 21:36:37 +0000182 MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32
Tom Stellard967bf582014-02-13 23:34:15 +0000183 };
184
Matt Arsenault0d89e842014-07-15 21:44:37 +0000185 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
186 setOperationAction(ISD::SELECT, MVT::i1, Promote);
187
Matt Arsenaultd504a742014-05-15 21:44:05 +0000188 for (MVT VT : VecTypes) {
Tom Stellard967bf582014-02-13 23:34:15 +0000189 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
190 switch(Op) {
191 case ISD::LOAD:
192 case ISD::STORE:
193 case ISD::BUILD_VECTOR:
194 case ISD::BITCAST:
195 case ISD::EXTRACT_VECTOR_ELT:
196 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000197 case ISD::INSERT_SUBVECTOR:
198 case ISD::EXTRACT_SUBVECTOR:
199 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000200 case ISD::CONCAT_VECTORS:
201 setOperationAction(Op, VT, Custom);
202 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000203 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000204 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000205 break;
206 }
207 }
208 }
209
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000210 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
211 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
212 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
213 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000214 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000215 }
216
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000217 setOperationAction(ISD::FDIV, MVT::f32, Custom);
218
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000219 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000220 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000221 setTargetDAGCombine(ISD::FMINNUM);
222 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000223 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +0000224 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000225 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000226 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000227 setTargetDAGCombine(ISD::UINT_TO_FP);
228
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000229 // All memory operations. Some folding on the pointer operand is done to help
230 // matching the constant offsets in the addressing modes.
231 setTargetDAGCombine(ISD::LOAD);
232 setTargetDAGCombine(ISD::STORE);
233 setTargetDAGCombine(ISD::ATOMIC_LOAD);
234 setTargetDAGCombine(ISD::ATOMIC_STORE);
235 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
236 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
237 setTargetDAGCombine(ISD::ATOMIC_SWAP);
238 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
239 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
240 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
241 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
242 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
243 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
244 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
245 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
246 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
247 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
248
Christian Konigeecebd02013-03-26 14:04:02 +0000249 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000250}
251
Tom Stellard0125f2a2013-06-25 02:39:35 +0000252//===----------------------------------------------------------------------===//
253// TargetLowering queries
254//===----------------------------------------------------------------------===//
255
Matt Arsenaulte306a322014-10-21 16:25:08 +0000256bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
257 EVT) const {
258 // SI has some legal vector types, but no legal vector operations. Say no
259 // shuffles are legal in order to prefer scalarizing some vector operations.
260 return false;
261}
262
Matt Arsenault5015a892014-08-15 17:17:07 +0000263// FIXME: This really needs an address space argument. The immediate offset
264// size is different for different sets of memory instruction sets.
265
266// The single offset DS instructions have a 16-bit unsigned byte offset.
267//
268// MUBUF / MTBUF have a 12-bit unsigned byte offset, and additionally can do r +
269// r + i with addr64. 32-bit has more addressing mode options. Depending on the
270// resource constant, it can also do (i64 r0) + (i32 r1) * (i14 i).
271//
272// SMRD instructions have an 8-bit, dword offset.
273//
274bool SITargetLowering::isLegalAddressingMode(const AddrMode &AM,
275 Type *Ty) const {
276 // No global is ever allowed as a base.
277 if (AM.BaseGV)
278 return false;
279
280 // Allow a 16-bit unsigned immediate field, since this is what DS instructions
281 // use.
282 if (!isUInt<16>(AM.BaseOffs))
283 return false;
284
285 // Only support r+r,
286 switch (AM.Scale) {
287 case 0: // "r+i" or just "i", depending on HasBaseReg.
288 break;
289 case 1:
290 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
291 return false;
292 // Otherwise we have r+r or r+i.
293 break;
294 case 2:
295 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
296 return false;
297 // Allow 2*r as r+r.
298 break;
299 default: // Don't allow n * r
300 return false;
301 }
302
303 return true;
304}
305
Matt Arsenaulte6986632015-01-14 01:35:22 +0000306bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000307 unsigned AddrSpace,
308 unsigned Align,
309 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000310 if (IsFast)
311 *IsFast = false;
312
Matt Arsenault1018c892014-04-24 17:08:26 +0000313 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
314 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000315 if (!VT.isSimple() || VT == MVT::Other)
316 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000317
Tom Stellardc6b299c2015-02-02 18:02:28 +0000318 // TODO - CI+ supports unaligned memory accesses, but this requires driver
319 // support.
Matt Arsenault1018c892014-04-24 17:08:26 +0000320
Matt Arsenault1018c892014-04-24 17:08:26 +0000321 // XXX - The only mention I see of this in the ISA manual is for LDS direct
322 // reads the "byte address and must be dword aligned". Is it also true for the
323 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000324 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
325 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
326 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
327 // with adjacent offsets.
328 return Align % 4 == 0;
329 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000330
331 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
332 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000333 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000334 if (IsFast)
335 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000336
337 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000338}
339
Matt Arsenault46645fa2014-07-28 17:49:26 +0000340EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
341 unsigned SrcAlign, bool IsMemset,
342 bool ZeroMemset,
343 bool MemcpyStrSrc,
344 MachineFunction &MF) const {
345 // FIXME: Should account for address space here.
346
347 // The default fallback uses the private pointer size as a guess for a type to
348 // use. Make sure we switch these to 64-bit accesses.
349
350 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
351 return MVT::v4i32;
352
353 if (Size >= 8 && DstAlign >= 4)
354 return MVT::v2i32;
355
356 // Use the default.
357 return MVT::Other;
358}
359
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000360TargetLoweringBase::LegalizeTypeAction
361SITargetLowering::getPreferredVectorAction(EVT VT) const {
362 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
363 return TypeSplitVector;
364
365 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000366}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000367
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000368bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
369 Type *Ty) const {
Eric Christopher7792e322015-01-30 23:24:40 +0000370 const SIInstrInfo *TII =
371 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000372 return TII->isInlineConstant(Imm);
373}
374
Tom Stellardaf775432013-10-23 00:44:32 +0000375SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000376 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000377 unsigned Offset, bool Signed) const {
Matt Arsenault86033ca2014-07-28 17:31:39 +0000378 const DataLayout *DL = getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000379 MachineFunction &MF = DAG.getMachineFunction();
380 const SIRegisterInfo *TRI =
381 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
382 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000383
Matt Arsenault86033ca2014-07-28 17:31:39 +0000384 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
385
386 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
387 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
388 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000389 MRI.getLiveInVirtReg(InputPtrReg), MVT::i64);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000390 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, BasePtr,
391 DAG.getConstant(Offset, MVT::i64));
392 SDValue PtrOffset = DAG.getUNDEF(getPointerTy(AMDGPUAS::CONSTANT_ADDRESS));
393 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
394
395 return DAG.getLoad(ISD::UNINDEXED, Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD,
396 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
397 false, // isVolatile
398 true, // isNonTemporal
399 true, // isInvariant
400 DL->getABITypeAlignment(Ty)); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000401}
402
Christian Konig2c8f6d52013-03-07 09:03:52 +0000403SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000404 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
405 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
406 SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardec2e43c2014-09-22 15:35:29 +0000407 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000408 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000409
410 MachineFunction &MF = DAG.getMachineFunction();
411 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000412 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000413
414 assert(CallConv == CallingConv::C);
415
416 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000417 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000418
419 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000420 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000421
422 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000423 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Vincent Lejeuned6236442013-10-13 17:56:16 +0000424 !Arg.Flags.isByVal()) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000425
426 assert((PSInputNum <= 15) && "Too many PS inputs!");
427
428 if (!Arg.Used) {
429 // We can savely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000430 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000431 ++PSInputNum;
432 continue;
433 }
434
435 Info->PSInputAddr |= 1 << PSInputNum++;
436 }
437
438 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000439 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000440 ISD::InputArg NewArg = Arg;
441 NewArg.Flags.setSplit();
442 NewArg.VT = Arg.VT.getVectorElementType();
443
444 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
445 // three or five element vertex only needs three or five registers,
446 // NOT four or eigth.
447 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
448 unsigned NumElements = ParamType->getVectorNumElements();
449
450 for (unsigned j = 0; j != NumElements; ++j) {
451 Splits.push_back(NewArg);
452 NewArg.PartOffset += NewArg.VT.getStoreSize();
453 }
454
Matt Arsenault762af962014-07-13 03:06:39 +0000455 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000456 Splits.push_back(Arg);
457 }
458 }
459
460 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000461 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
462 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000463
Christian Konig99ee0f42013-03-07 09:04:14 +0000464 // At least one interpolation mode must be enabled or else the GPU will hang.
Matt Arsenault762af962014-07-13 03:06:39 +0000465 if (Info->getShaderType() == ShaderType::PIXEL &&
466 (Info->PSInputAddr & 0x7F) == 0) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000467 Info->PSInputAddr |= 1;
468 CCInfo.AllocateReg(AMDGPU::VGPR0);
469 CCInfo.AllocateReg(AMDGPU::VGPR1);
470 }
471
Tom Stellarded882c22013-06-03 17:40:11 +0000472 // The pointer to the list of arguments is stored in SGPR0, SGPR1
Tom Stellardb02094e2014-07-21 15:45:01 +0000473 // The pointer to the scratch buffer is stored in SGPR2, SGPR3
Matt Arsenault762af962014-07-13 03:06:39 +0000474 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardfeab91c2014-12-02 17:41:43 +0000475 if (Subtarget->isAmdHsaOS())
476 Info->NumUserSGPRs = 2; // FIXME: Need to support scratch buffers.
477 else
478 Info->NumUserSGPRs = 4;
Tom Stellardec2e43c2014-09-22 15:35:29 +0000479
480 unsigned InputPtrReg =
481 TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
482 unsigned InputPtrRegLo =
483 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 0);
484 unsigned InputPtrRegHi =
485 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 1);
486
487 unsigned ScratchPtrReg =
488 TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_PTR);
489 unsigned ScratchPtrRegLo =
490 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 0);
491 unsigned ScratchPtrRegHi =
492 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 1);
493
494 CCInfo.AllocateReg(InputPtrRegLo);
495 CCInfo.AllocateReg(InputPtrRegHi);
496 CCInfo.AllocateReg(ScratchPtrRegLo);
497 CCInfo.AllocateReg(ScratchPtrRegHi);
498 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
499 MF.addLiveIn(ScratchPtrReg, &AMDGPU::SReg_64RegClass);
Tom Stellarded882c22013-06-03 17:40:11 +0000500 }
501
Matt Arsenault762af962014-07-13 03:06:39 +0000502 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000503 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
504 Splits);
505 }
506
Christian Konig2c8f6d52013-03-07 09:03:52 +0000507 AnalyzeFormalArguments(CCInfo, Splits);
508
509 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
510
Christian Konigb7be72d2013-05-17 09:46:48 +0000511 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000512 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000513 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000514 continue;
515 }
516
Christian Konig2c8f6d52013-03-07 09:03:52 +0000517 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000518 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000519
520 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000521 VT = Ins[i].VT;
522 EVT MemVT = Splits[i].VT;
Jan Veselye5121f32014-10-14 20:05:26 +0000523 const unsigned Offset = 36 + VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000524 // The first 36 bytes of the input buffer contains information about
525 // thread group and global sizes.
Tom Stellardaf775432013-10-23 00:44:32 +0000526 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
Jan Veselye5121f32014-10-14 20:05:26 +0000527 Offset, Ins[i].Flags.isSExt());
Tom Stellardca7ecf32014-08-22 18:49:31 +0000528
529 const PointerType *ParamTy =
530 dyn_cast<PointerType>(FType->getParamType(Ins[i].OrigArgIndex));
531 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
532 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
533 // On SI local pointers are just offsets into LDS, so they are always
534 // less than 16-bits. On CI and newer they could potentially be
535 // real pointers, so we can't guarantee their size.
536 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
537 DAG.getValueType(MVT::i16));
538 }
539
Tom Stellarded882c22013-06-03 17:40:11 +0000540 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000541 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000542 continue;
543 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000544 assert(VA.isRegLoc() && "Parameter must be in a register!");
545
546 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000547
548 if (VT == MVT::i64) {
549 // For now assume it is a pointer
550 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
551 &AMDGPU::SReg_64RegClass);
552 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
553 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
554 continue;
555 }
556
557 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
558
559 Reg = MF.addLiveIn(Reg, RC);
560 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
561
Christian Konig2c8f6d52013-03-07 09:03:52 +0000562 if (Arg.VT.isVector()) {
563
564 // Build a vector from the registers
565 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
566 unsigned NumElements = ParamType->getVectorNumElements();
567
568 SmallVector<SDValue, 4> Regs;
569 Regs.push_back(Val);
570 for (unsigned j = 1; j != NumElements; ++j) {
571 Reg = ArgLocs[ArgIdx++].getLocReg();
572 Reg = MF.addLiveIn(Reg, RC);
573 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
574 }
575
576 // Fill up the missing vector elements
577 NumElements = Arg.VT.getVectorNumElements() - NumElements;
578 for (unsigned j = 0; j != NumElements; ++j)
579 Regs.push_back(DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000580
Craig Topper48d114b2014-04-26 18:35:24 +0000581 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000582 continue;
583 }
584
585 InVals.push_back(Val);
586 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000587
588 if (Info->getShaderType() != ShaderType::COMPUTE) {
589 unsigned ScratchIdx = CCInfo.getFirstUnallocated(
590 AMDGPU::SGPR_32RegClass.begin(), AMDGPU::SGPR_32RegClass.getNumRegs());
591 Info->ScratchOffsetReg = AMDGPU::SGPR_32RegClass.getRegister(ScratchIdx);
592 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000593 return Chain;
594}
595
Tom Stellard75aadc22012-12-11 21:25:42 +0000596MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
597 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000598
Tom Stellard556d9aa2013-06-03 17:39:37 +0000599 MachineBasicBlock::iterator I = *MI;
Eric Christopher7792e322015-01-30 23:24:40 +0000600 const SIInstrInfo *TII =
601 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellard556d9aa2013-06-03 17:39:37 +0000602
Tom Stellard75aadc22012-12-11 21:25:42 +0000603 switch (MI->getOpcode()) {
604 default:
605 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
606 case AMDGPU::BRANCH: return BB;
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000607 case AMDGPU::V_SUB_F64: {
608 unsigned DestReg = MI->getOperand(0).getReg();
609 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64), DestReg)
610 .addImm(0) // SRC0 modifiers
611 .addReg(MI->getOperand(1).getReg())
612 .addImm(1) // SRC1 modifiers
613 .addReg(MI->getOperand(2).getReg())
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000614 .addImm(0) // CLAMP
615 .addImm(0); // OMOD
Tom Stellard2a6a61052013-07-12 18:15:08 +0000616 MI->eraseFromParent();
617 break;
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000618 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000619 case AMDGPU::SI_RegisterStorePseudo: {
620 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard81d871d2013-11-13 23:36:50 +0000621 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
622 MachineInstrBuilder MIB =
623 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
624 Reg);
625 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
626 MIB.addOperand(MI->getOperand(i));
627
628 MI->eraseFromParent();
Vincent Lejeune79a58342014-05-10 19:18:25 +0000629 break;
630 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000631 }
632 return BB;
633}
634
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000635bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
636 // This currently forces unfolding various combinations of fsub into fma with
637 // free fneg'd operands. As long as we have fast FMA (controlled by
638 // isFMAFasterThanFMulAndFAdd), we should perform these.
639
640 // When fma is quarter rate, for f64 where add / sub are at best half rate,
641 // most of these combines appear to be cycle neutral but save on instruction
642 // count / code size.
643 return true;
644}
645
Matt Arsenault8596f712014-11-28 22:51:38 +0000646EVT SITargetLowering::getSetCCResultType(LLVMContext &Ctx, EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +0000647 if (!VT.isVector()) {
648 return MVT::i1;
649 }
Matt Arsenault8596f712014-11-28 22:51:38 +0000650 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +0000651}
652
Christian Konig082a14a2013-03-18 11:34:05 +0000653MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
654 return MVT::i32;
655}
656
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000657// Answering this is somewhat tricky and depends on the specific device which
658// have different rates for fma or all f64 operations.
659//
660// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
661// regardless of which device (although the number of cycles differs between
662// devices), so it is always profitable for f64.
663//
664// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
665// only on full rate devices. Normally, we should prefer selecting v_mad_f32
666// which we can always do even without fused FP ops since it returns the same
667// result as the separate operations and since it is always full
668// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
669// however does not support denormals, so we do report fma as faster if we have
670// a fast fma device and require denormals.
671//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000672bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
673 VT = VT.getScalarType();
674
675 if (!VT.isSimple())
676 return false;
677
678 switch (VT.getSimpleVT().SimpleTy) {
679 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000680 // This is as fast on some subtargets. However, we always have full rate f32
681 // mad available which returns the same result as the separate operations
682 // which we should prefer over fma.
683 return false;
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000684 case MVT::f64:
685 return true;
686 default:
687 break;
688 }
689
690 return false;
691}
692
Tom Stellard75aadc22012-12-11 21:25:42 +0000693//===----------------------------------------------------------------------===//
694// Custom DAG Lowering Operations
695//===----------------------------------------------------------------------===//
696
697SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
698 switch (Op.getOpcode()) {
699 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +0000700 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000701 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000702 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +0000703 SDValue Result = LowerLOAD(Op, DAG);
704 assert((!Result.getNode() ||
705 Result.getNode()->getNumValues() == 2) &&
706 "Load should return a value and a chain");
707 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +0000708 }
Tom Stellardaf775432013-10-23 00:44:32 +0000709
Matt Arsenaultad14ce82014-07-19 18:44:39 +0000710 case ISD::FSIN:
711 case ISD::FCOS:
712 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000713 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000714 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000715 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000716 case ISD::GlobalAddress: {
717 MachineFunction &MF = DAG.getMachineFunction();
718 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
719 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +0000720 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000721 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
722 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000723 }
724 return SDValue();
725}
726
Tom Stellardf8794352012-12-19 22:10:31 +0000727/// \brief Helper function for LowerBRCOND
728static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000729
Tom Stellardf8794352012-12-19 22:10:31 +0000730 SDNode *Parent = Value.getNode();
731 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
732 I != E; ++I) {
733
734 if (I.getUse().get() != Value)
735 continue;
736
737 if (I->getOpcode() == Opcode)
738 return *I;
739 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000740 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000741}
742
Tom Stellardb02094e2014-07-21 15:45:01 +0000743SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
744
Tom Stellardb02094e2014-07-21 15:45:01 +0000745 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
746 unsigned FrameIndex = FINode->getIndex();
747
Tom Stellardb02094e2014-07-21 15:45:01 +0000748 return DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
749}
750
Tom Stellardf8794352012-12-19 22:10:31 +0000751/// This transforms the control flow intrinsics to get the branch destination as
752/// last parameter, also switches branch target with BR if the need arise
753SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
754 SelectionDAG &DAG) const {
755
Andrew Trickef9de2a2013-05-25 02:42:55 +0000756 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000757
758 SDNode *Intr = BRCOND.getOperand(1).getNode();
759 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +0000760 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000761
762 if (Intr->getOpcode() == ISD::SETCC) {
763 // As long as we negate the condition everything is fine
764 SDNode *SetCC = Intr;
765 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000766 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
767 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000768 Intr = SetCC->getOperand(0).getNode();
769
770 } else {
771 // Get the target from BR if we don't negate the condition
772 BR = findUser(BRCOND, ISD::BR);
773 Target = BR->getOperand(1);
774 }
775
776 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
777
778 // Build the result and
779 SmallVector<EVT, 4> Res;
780 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
781 Res.push_back(Intr->getValueType(i));
782
783 // operands of the new intrinsic call
784 SmallVector<SDValue, 4> Ops;
785 Ops.push_back(BRCOND.getOperand(0));
786 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
787 Ops.push_back(Intr->getOperand(i));
788 Ops.push_back(Target);
789
790 // build the new intrinsic call
791 SDNode *Result = DAG.getNode(
792 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +0000793 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000794
795 if (BR) {
796 // Give the branch instruction our target
797 SDValue Ops[] = {
798 BR->getOperand(0),
799 BRCOND.getOperand(2)
800 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000801 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
802 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
803 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000804 }
805
806 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
807
808 // Copy the intrinsic results to registers
809 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
810 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
811 if (!CopyToReg)
812 continue;
813
814 Chain = DAG.getCopyToReg(
815 Chain, DL,
816 CopyToReg->getOperand(1),
817 SDValue(Result, i - 1),
818 SDValue());
819
820 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
821 }
822
823 // Remove the old intrinsic from the chain
824 DAG.ReplaceAllUsesOfValueWith(
825 SDValue(Intr, Intr->getNumValues() - 1),
826 Intr->getOperand(0));
827
828 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000829}
830
Tom Stellard067c8152014-07-21 14:01:14 +0000831SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
832 SDValue Op,
833 SelectionDAG &DAG) const {
834 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
835
836 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
837 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
838
839 SDLoc DL(GSD);
840 const GlobalValue *GV = GSD->getGlobal();
841 MVT PtrVT = getPointerTy(GSD->getAddressSpace());
842
843 SDValue Ptr = DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT);
844 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
845
846 SDValue PtrLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
847 DAG.getConstant(0, MVT::i32));
848 SDValue PtrHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
849 DAG.getConstant(1, MVT::i32));
850
851 SDValue Lo = DAG.getNode(ISD::ADDC, DL, DAG.getVTList(MVT::i32, MVT::Glue),
852 PtrLo, GA);
853 SDValue Hi = DAG.getNode(ISD::ADDE, DL, DAG.getVTList(MVT::i32, MVT::Glue),
854 PtrHi, DAG.getConstant(0, MVT::i32),
855 SDValue(Lo.getNode(), 1));
856 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
857}
858
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000859SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
860 SelectionDAG &DAG) const {
861 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000862 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000863 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000864
865 EVT VT = Op.getValueType();
866 SDLoc DL(Op);
867 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
868
869 switch (IntrinsicID) {
870 case Intrinsic::r600_read_ngroups_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000871 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
872 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000873 case Intrinsic::r600_read_ngroups_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000874 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
875 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000876 case Intrinsic::r600_read_ngroups_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000877 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
878 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000879 case Intrinsic::r600_read_global_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000880 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
881 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000882 case Intrinsic::r600_read_global_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000883 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
884 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000885 case Intrinsic::r600_read_global_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000886 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
887 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000888 case Intrinsic::r600_read_local_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000889 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
890 SI::KernelInputOffsets::LOCAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000891 case Intrinsic::r600_read_local_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000892 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
893 SI::KernelInputOffsets::LOCAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000894 case Intrinsic::r600_read_local_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000895 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
896 SI::KernelInputOffsets::LOCAL_SIZE_Z, false);
Jan Veselye5121f32014-10-14 20:05:26 +0000897
898 case Intrinsic::AMDGPU_read_workdim:
899 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
900 MF.getInfo<SIMachineFunctionInfo>()->ABIArgOffset,
901 false);
902
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000903 case Intrinsic::r600_read_tgid_x:
904 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000905 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000906 case Intrinsic::r600_read_tgid_y:
907 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000908 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000909 case Intrinsic::r600_read_tgid_z:
910 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000911 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000912 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000913 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000914 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000915 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000916 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000917 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000918 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000919 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000920 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000921 case AMDGPUIntrinsic::SI_load_const: {
922 SDValue Ops[] = {
923 Op.getOperand(1),
924 Op.getOperand(2)
925 };
926
927 MachineMemOperand *MMO = MF.getMachineMemOperand(
928 MachinePointerInfo(),
929 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
930 VT.getStoreSize(), 4);
931 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
932 Op->getVTList(), Ops, VT, MMO);
933 }
934 case AMDGPUIntrinsic::SI_sample:
935 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
936 case AMDGPUIntrinsic::SI_sampleb:
937 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
938 case AMDGPUIntrinsic::SI_sampled:
939 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
940 case AMDGPUIntrinsic::SI_samplel:
941 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
942 case AMDGPUIntrinsic::SI_vs_load_input:
943 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
944 Op.getOperand(1),
945 Op.getOperand(2),
946 Op.getOperand(3));
947 default:
948 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
949 }
950}
951
952SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
953 SelectionDAG &DAG) const {
954 MachineFunction &MF = DAG.getMachineFunction();
955 SDValue Chain = Op.getOperand(0);
956 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
957
958 switch (IntrinsicID) {
959 case AMDGPUIntrinsic::SI_tbuffer_store: {
960 SDLoc DL(Op);
961 SDValue Ops[] = {
962 Chain,
963 Op.getOperand(2),
964 Op.getOperand(3),
965 Op.getOperand(4),
966 Op.getOperand(5),
967 Op.getOperand(6),
968 Op.getOperand(7),
969 Op.getOperand(8),
970 Op.getOperand(9),
971 Op.getOperand(10),
972 Op.getOperand(11),
973 Op.getOperand(12),
974 Op.getOperand(13),
975 Op.getOperand(14)
976 };
977
978 EVT VT = Op.getOperand(3).getValueType();
979
980 MachineMemOperand *MMO = MF.getMachineMemOperand(
981 MachinePointerInfo(),
982 MachineMemOperand::MOStore,
983 VT.getStoreSize(), 4);
984 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
985 Op->getVTList(), Ops, VT, MMO);
986 }
987 default:
988 return SDValue();
989 }
990}
991
Tom Stellard81d871d2013-11-13 23:36:50 +0000992SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
993 SDLoc DL(Op);
994 LoadSDNode *Load = cast<LoadSDNode>(Op);
995
Tom Stellarde812f2f2014-07-21 15:45:06 +0000996 if (Op.getValueType().isVector()) {
997 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
998 "Custom lowering for non-i32 vectors hasn't been implemented.");
999 unsigned NumElements = Op.getValueType().getVectorNumElements();
1000 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
1001 switch (Load->getAddressSpace()) {
1002 default: break;
1003 case AMDGPUAS::GLOBAL_ADDRESS:
1004 case AMDGPUAS::PRIVATE_ADDRESS:
1005 // v4 loads are supported for private and global memory.
1006 if (NumElements <= 4)
1007 break;
1008 // fall-through
1009 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenault83e60582014-07-24 17:10:35 +00001010 return ScalarizeVectorLoad(Op, DAG);
Tom Stellarde812f2f2014-07-21 15:45:06 +00001011 }
Tom Stellarde9373602014-01-22 19:24:14 +00001012 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001013
Tom Stellarde812f2f2014-07-21 15:45:06 +00001014 return AMDGPUTargetLowering::LowerLOAD(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001015}
1016
Tom Stellard9fa17912013-08-14 23:24:45 +00001017SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
1018 const SDValue &Op,
1019 SelectionDAG &DAG) const {
1020 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
1021 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +00001022 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +00001023 Op.getOperand(4));
1024}
1025
Tom Stellard0ec134f2014-02-04 17:18:40 +00001026SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1027 if (Op.getValueType() != MVT::i64)
1028 return SDValue();
1029
1030 SDLoc DL(Op);
1031 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001032
1033 SDValue Zero = DAG.getConstant(0, MVT::i32);
1034 SDValue One = DAG.getConstant(1, MVT::i32);
1035
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001036 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1037 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1038
1039 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1040 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001041
1042 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1043
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001044 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1045 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001046
1047 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1048
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001049 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1050 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001051}
1052
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001053// Catch division cases where we can use shortcuts with rcp and rsq
1054// instructions.
1055SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001056 SDLoc SL(Op);
1057 SDValue LHS = Op.getOperand(0);
1058 SDValue RHS = Op.getOperand(1);
1059 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001060 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001061
1062 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001063 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1064 CLHS->isExactlyValue(1.0)) {
1065 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1066 // the CI documentation has a worst case error of 1 ulp.
1067 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1068 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001069
1070 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001071 //
1072 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1073 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001074 if (RHS.getOpcode() == ISD::FSQRT)
1075 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1076
1077 // 1.0 / x -> rcp(x)
1078 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1079 }
1080 }
1081
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001082 if (Unsafe) {
1083 // Turn into multiply by the reciprocal.
1084 // x / y -> x * (1.0 / y)
1085 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1086 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip);
1087 }
1088
1089 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001090}
1091
1092SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001093 SDValue FastLowered = LowerFastFDIV(Op, DAG);
1094 if (FastLowered.getNode())
1095 return FastLowered;
1096
1097 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1098 // selection error for now rather than do something incorrect.
1099 if (Subtarget->hasFP32Denormals())
1100 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001101
1102 SDLoc SL(Op);
1103 SDValue LHS = Op.getOperand(0);
1104 SDValue RHS = Op.getOperand(1);
1105
1106 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1107
1108 const APFloat K0Val(BitsToFloat(0x6f800000));
1109 const SDValue K0 = DAG.getConstantFP(K0Val, MVT::f32);
1110
1111 const APFloat K1Val(BitsToFloat(0x2f800000));
1112 const SDValue K1 = DAG.getConstantFP(K1Val, MVT::f32);
1113
Tom Stellardfb77f002015-01-13 22:59:41 +00001114 const SDValue One = DAG.getConstantFP(1.0, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001115
1116 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f32);
1117
1118 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1119
1120 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1121
1122 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1123
1124 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1125
1126 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1127
1128 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1129}
1130
1131SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
1132 return SDValue();
1133}
1134
1135SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1136 EVT VT = Op.getValueType();
1137
1138 if (VT == MVT::f32)
1139 return LowerFDIV32(Op, DAG);
1140
1141 if (VT == MVT::f64)
1142 return LowerFDIV64(Op, DAG);
1143
1144 llvm_unreachable("Unexpected type for fdiv");
1145}
1146
Tom Stellard81d871d2013-11-13 23:36:50 +00001147SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1148 SDLoc DL(Op);
1149 StoreSDNode *Store = cast<StoreSDNode>(Op);
1150 EVT VT = Store->getMemoryVT();
1151
Tom Stellard9b3816b2014-06-24 23:33:04 +00001152 // These stores are legal.
1153 if (Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
1154 VT.isVector() && VT.getVectorNumElements() == 2 &&
1155 VT.getVectorElementType() == MVT::i32)
1156 return SDValue();
1157
Tom Stellardb02094e2014-07-21 15:45:01 +00001158 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1159 if (VT.isVector() && VT.getVectorNumElements() > 4)
Matt Arsenault83e60582014-07-24 17:10:35 +00001160 return ScalarizeVectorStore(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001161 return SDValue();
1162 }
1163
Tom Stellard81d871d2013-11-13 23:36:50 +00001164 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1165 if (Ret.getNode())
1166 return Ret;
1167
1168 if (VT.isVector() && VT.getVectorNumElements() >= 8)
Matt Arsenault83e60582014-07-24 17:10:35 +00001169 return ScalarizeVectorStore(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001170
Tom Stellard1c8788e2014-03-07 20:12:33 +00001171 if (VT == MVT::i1)
1172 return DAG.getTruncStore(Store->getChain(), DL,
1173 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1174 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1175
Tom Stellarde812f2f2014-07-21 15:45:06 +00001176 return SDValue();
Tom Stellard81d871d2013-11-13 23:36:50 +00001177}
1178
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001179SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
1180 EVT VT = Op.getValueType();
1181 SDValue Arg = Op.getOperand(0);
1182 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
1183 DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
1184 DAG.getConstantFP(0.5 / M_PI, VT)));
1185
1186 switch (Op.getOpcode()) {
1187 case ISD::FCOS:
1188 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1189 case ISD::FSIN:
1190 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1191 default:
1192 llvm_unreachable("Wrong trig opcode");
1193 }
1194}
1195
Tom Stellard75aadc22012-12-11 21:25:42 +00001196//===----------------------------------------------------------------------===//
1197// Custom DAG optimizations
1198//===----------------------------------------------------------------------===//
1199
Matt Arsenault364a6742014-06-11 17:50:44 +00001200SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00001201 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00001202 EVT VT = N->getValueType(0);
1203 EVT ScalarVT = VT.getScalarType();
1204 if (ScalarVT != MVT::f32)
1205 return SDValue();
1206
1207 SelectionDAG &DAG = DCI.DAG;
1208 SDLoc DL(N);
1209
1210 SDValue Src = N->getOperand(0);
1211 EVT SrcVT = Src.getValueType();
1212
1213 // TODO: We could try to match extracting the higher bytes, which would be
1214 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1215 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1216 // about in practice.
1217 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1218 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1219 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1220 DCI.AddToWorklist(Cvt.getNode());
1221 return Cvt;
1222 }
1223 }
1224
1225 // We are primarily trying to catch operations on illegal vector types
1226 // before they are expanded.
1227 // For scalars, we can use the more flexible method of checking masked bits
1228 // after legalization.
1229 if (!DCI.isBeforeLegalize() ||
1230 !SrcVT.isVector() ||
1231 SrcVT.getVectorElementType() != MVT::i8) {
1232 return SDValue();
1233 }
1234
1235 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1236
1237 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1238 // size as 4.
1239 unsigned NElts = SrcVT.getVectorNumElements();
1240 if (!SrcVT.isSimple() && NElts != 3)
1241 return SDValue();
1242
1243 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1244 // prevent a mess from expanding to v4i32 and repacking.
1245 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1246 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1247 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1248 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00001249 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001250
1251 unsigned AS = Load->getAddressSpace();
1252 unsigned Align = Load->getAlignment();
1253 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
1254 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
1255
1256 // Don't try to replace the load if we have to expand it due to alignment
1257 // problems. Otherwise we will end up scalarizing the load, and trying to
1258 // repack into the vector for no real reason.
1259 if (Align < ABIAlignment &&
1260 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
1261 return SDValue();
1262 }
1263
Matt Arsenault364a6742014-06-11 17:50:44 +00001264 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1265 Load->getChain(),
1266 Load->getBasePtr(),
1267 LoadVT,
1268 Load->getMemOperand());
1269
1270 // Make sure successors of the original load stay after it by updating
1271 // them to use the new Chain.
1272 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1273
1274 SmallVector<SDValue, 4> Elts;
1275 if (RegVT.isVector())
1276 DAG.ExtractVectorElements(NewLoad, Elts);
1277 else
1278 Elts.push_back(NewLoad);
1279
1280 SmallVector<SDValue, 4> Ops;
1281
1282 unsigned EltIdx = 0;
1283 for (SDValue Elt : Elts) {
1284 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1285 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1286 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1287 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1288 DCI.AddToWorklist(Cvt.getNode());
1289 Ops.push_back(Cvt);
1290 }
1291
1292 ++EltIdx;
1293 }
1294
1295 assert(Ops.size() == NElts);
1296
1297 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1298 }
1299
1300 return SDValue();
1301}
1302
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001303// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
1304
1305// This is a variant of
1306// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
1307//
1308// The normal DAG combiner will do this, but only if the add has one use since
1309// that would increase the number of instructions.
1310//
1311// This prevents us from seeing a constant offset that can be folded into a
1312// memory instruction's addressing mode. If we know the resulting add offset of
1313// a pointer can be folded into an addressing offset, we can replace the pointer
1314// operand with the add of new constant offset. This eliminates one of the uses,
1315// and may allow the remaining use to also be simplified.
1316//
1317SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
1318 unsigned AddrSpace,
1319 DAGCombinerInfo &DCI) const {
1320 SDValue N0 = N->getOperand(0);
1321 SDValue N1 = N->getOperand(1);
1322
1323 if (N0.getOpcode() != ISD::ADD)
1324 return SDValue();
1325
1326 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
1327 if (!CN1)
1328 return SDValue();
1329
1330 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1331 if (!CAdd)
1332 return SDValue();
1333
Eric Christopher7792e322015-01-30 23:24:40 +00001334 const SIInstrInfo *TII =
1335 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001336
1337 // If the resulting offset is too large, we can't fold it into the addressing
1338 // mode offset.
1339 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
1340 if (!TII->canFoldOffset(Offset.getZExtValue(), AddrSpace))
1341 return SDValue();
1342
1343 SelectionDAG &DAG = DCI.DAG;
1344 SDLoc SL(N);
1345 EVT VT = N->getValueType(0);
1346
1347 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
1348 SDValue COffset = DAG.getConstant(Offset, MVT::i32);
1349
1350 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
1351}
1352
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001353SDValue SITargetLowering::performAndCombine(SDNode *N,
1354 DAGCombinerInfo &DCI) const {
1355 if (DCI.isBeforeLegalize())
1356 return SDValue();
1357
1358 SelectionDAG &DAG = DCI.DAG;
1359
1360 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
1361 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
1362 SDValue LHS = N->getOperand(0);
1363 SDValue RHS = N->getOperand(1);
1364
1365 if (LHS.getOpcode() == ISD::SETCC &&
1366 RHS.getOpcode() == ISD::SETCC) {
1367 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
1368 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
1369
1370 SDValue X = LHS.getOperand(0);
1371 SDValue Y = RHS.getOperand(0);
1372 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
1373 return SDValue();
1374
1375 if (LCC == ISD::SETO) {
1376 if (X != LHS.getOperand(1))
1377 return SDValue();
1378
1379 if (RCC == ISD::SETUNE) {
1380 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
1381 if (!C1 || !C1->isInfinity() || C1->isNegative())
1382 return SDValue();
1383
1384 const uint32_t Mask = SIInstrFlags::N_NORMAL |
1385 SIInstrFlags::N_SUBNORMAL |
1386 SIInstrFlags::N_ZERO |
1387 SIInstrFlags::P_ZERO |
1388 SIInstrFlags::P_SUBNORMAL |
1389 SIInstrFlags::P_NORMAL;
1390
1391 static_assert(((~(SIInstrFlags::S_NAN |
1392 SIInstrFlags::Q_NAN |
1393 SIInstrFlags::N_INFINITY |
1394 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
1395 "mask not equal");
1396
1397 return DAG.getNode(AMDGPUISD::FP_CLASS, SDLoc(N), MVT::i1,
1398 X, DAG.getConstant(Mask, MVT::i32));
1399 }
1400 }
1401 }
1402
1403 return SDValue();
1404}
1405
Matt Arsenaultf2290332015-01-06 23:00:39 +00001406SDValue SITargetLowering::performOrCombine(SDNode *N,
1407 DAGCombinerInfo &DCI) const {
1408 SelectionDAG &DAG = DCI.DAG;
1409 SDValue LHS = N->getOperand(0);
1410 SDValue RHS = N->getOperand(1);
1411
1412 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
1413 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
1414 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
1415 SDValue Src = LHS.getOperand(0);
1416 if (Src != RHS.getOperand(0))
1417 return SDValue();
1418
1419 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
1420 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
1421 if (!CLHS || !CRHS)
1422 return SDValue();
1423
1424 // Only 10 bits are used.
1425 static const uint32_t MaxMask = 0x3ff;
1426
1427 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
1428 return DAG.getNode(AMDGPUISD::FP_CLASS, SDLoc(N), MVT::i1,
1429 Src, DAG.getConstant(NewMask, MVT::i32));
1430 }
1431
1432 return SDValue();
1433}
1434
1435SDValue SITargetLowering::performClassCombine(SDNode *N,
1436 DAGCombinerInfo &DCI) const {
1437 SelectionDAG &DAG = DCI.DAG;
1438 SDValue Mask = N->getOperand(1);
1439
1440 // fp_class x, 0 -> false
1441 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
1442 if (CMask->isNullValue())
1443 return DAG.getConstant(0, MVT::i1);
1444 }
1445
1446 return SDValue();
1447}
1448
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001449static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
1450 switch (Opc) {
1451 case ISD::FMAXNUM:
1452 return AMDGPUISD::FMAX3;
1453 case AMDGPUISD::SMAX:
1454 return AMDGPUISD::SMAX3;
1455 case AMDGPUISD::UMAX:
1456 return AMDGPUISD::UMAX3;
1457 case ISD::FMINNUM:
1458 return AMDGPUISD::FMIN3;
1459 case AMDGPUISD::SMIN:
1460 return AMDGPUISD::SMIN3;
1461 case AMDGPUISD::UMIN:
1462 return AMDGPUISD::UMIN3;
1463 default:
1464 llvm_unreachable("Not a min/max opcode");
1465 }
1466}
1467
1468SDValue SITargetLowering::performMin3Max3Combine(SDNode *N,
1469 DAGCombinerInfo &DCI) const {
1470 SelectionDAG &DAG = DCI.DAG;
1471
1472 unsigned Opc = N->getOpcode();
1473 SDValue Op0 = N->getOperand(0);
1474 SDValue Op1 = N->getOperand(1);
1475
1476 // Only do this if the inner op has one use since this will just increases
1477 // register pressure for no benefit.
1478
1479 // max(max(a, b), c)
1480 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
1481 SDLoc DL(N);
1482 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1483 DL,
1484 N->getValueType(0),
1485 Op0.getOperand(0),
1486 Op0.getOperand(1),
1487 Op1);
1488 }
1489
1490 // max(a, max(b, c))
1491 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
1492 SDLoc DL(N);
1493 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1494 DL,
1495 N->getValueType(0),
1496 Op0,
1497 Op1.getOperand(0),
1498 Op1.getOperand(1));
1499 }
1500
1501 return SDValue();
1502}
1503
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001504SDValue SITargetLowering::performSetCCCombine(SDNode *N,
1505 DAGCombinerInfo &DCI) const {
1506 SelectionDAG &DAG = DCI.DAG;
1507 SDLoc SL(N);
1508
1509 SDValue LHS = N->getOperand(0);
1510 SDValue RHS = N->getOperand(1);
1511 EVT VT = LHS.getValueType();
1512
1513 if (VT != MVT::f32 && VT != MVT::f64)
1514 return SDValue();
1515
1516 // Match isinf pattern
1517 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
1518 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
1519 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
1520 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
1521 if (!CRHS)
1522 return SDValue();
1523
1524 const APFloat &APF = CRHS->getValueAPF();
1525 if (APF.isInfinity() && !APF.isNegative()) {
1526 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
1527 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1,
1528 LHS.getOperand(0), DAG.getConstant(Mask, MVT::i32));
1529 }
1530 }
1531
1532 return SDValue();
1533}
1534
Tom Stellard75aadc22012-12-11 21:25:42 +00001535SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1536 DAGCombinerInfo &DCI) const {
1537 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001538 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001539
1540 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00001541 default:
1542 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001543 case ISD::SETCC:
1544 return performSetCCCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001545 case ISD::FMAXNUM: // TODO: What about fmax_legacy?
1546 case ISD::FMINNUM:
1547 case AMDGPUISD::SMAX:
1548 case AMDGPUISD::SMIN:
1549 case AMDGPUISD::UMAX:
1550 case AMDGPUISD::UMIN: {
1551 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
1552 getTargetMachine().getOptLevel() > CodeGenOpt::None)
1553 return performMin3Max3Combine(N, DCI);
1554 break;
1555 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001556
1557 case AMDGPUISD::CVT_F32_UBYTE0:
1558 case AMDGPUISD::CVT_F32_UBYTE1:
1559 case AMDGPUISD::CVT_F32_UBYTE2:
1560 case AMDGPUISD::CVT_F32_UBYTE3: {
1561 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
1562
1563 SDValue Src = N->getOperand(0);
1564 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
1565
1566 APInt KnownZero, KnownOne;
1567 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1568 !DCI.isBeforeLegalizeOps());
1569 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1570 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
1571 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
1572 DCI.CommitTargetLoweringOpt(TLO);
1573 }
1574
1575 break;
1576 }
1577
1578 case ISD::UINT_TO_FP: {
1579 return performUCharToFloatCombine(N, DCI);
Matt Arsenault8675db12014-08-29 16:01:14 +00001580
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001581 case ISD::FADD: {
1582 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1583 break;
1584
1585 EVT VT = N->getValueType(0);
1586 if (VT != MVT::f32)
1587 break;
1588
1589 SDValue LHS = N->getOperand(0);
1590 SDValue RHS = N->getOperand(1);
1591
1592 // These should really be instruction patterns, but writing patterns with
1593 // source modiifiers is a pain.
1594
1595 // fadd (fadd (a, a), b) -> mad 2.0, a, b
1596 if (LHS.getOpcode() == ISD::FADD) {
1597 SDValue A = LHS.getOperand(0);
1598 if (A == LHS.getOperand(1)) {
Tom Stellardfb77f002015-01-13 22:59:41 +00001599 const SDValue Two = DAG.getConstantFP(2.0, MVT::f32);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001600 return DAG.getNode(AMDGPUISD::MAD, DL, VT, Two, A, RHS);
1601 }
1602 }
1603
1604 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
1605 if (RHS.getOpcode() == ISD::FADD) {
1606 SDValue A = RHS.getOperand(0);
1607 if (A == RHS.getOperand(1)) {
Tom Stellardfb77f002015-01-13 22:59:41 +00001608 const SDValue Two = DAG.getConstantFP(2.0, MVT::f32);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001609 return DAG.getNode(AMDGPUISD::MAD, DL, VT, Two, A, LHS);
1610 }
1611 }
1612
1613 break;
1614 }
Matt Arsenault8675db12014-08-29 16:01:14 +00001615 case ISD::FSUB: {
1616 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1617 break;
1618
1619 EVT VT = N->getValueType(0);
1620
1621 // Try to get the fneg to fold into the source modifier. This undoes generic
1622 // DAG combines and folds them into the mad.
1623 if (VT == MVT::f32) {
1624 SDValue LHS = N->getOperand(0);
1625 SDValue RHS = N->getOperand(1);
1626
1627 if (LHS.getOpcode() == ISD::FMUL) {
1628 // (fsub (fmul a, b), c) -> mad a, b, (fneg c)
1629
1630 SDValue A = LHS.getOperand(0);
1631 SDValue B = LHS.getOperand(1);
1632 SDValue C = DAG.getNode(ISD::FNEG, DL, VT, RHS);
1633
1634 return DAG.getNode(AMDGPUISD::MAD, DL, VT, A, B, C);
1635 }
1636
1637 if (RHS.getOpcode() == ISD::FMUL) {
1638 // (fsub c, (fmul a, b)) -> mad (fneg a), b, c
1639
1640 SDValue A = DAG.getNode(ISD::FNEG, DL, VT, RHS.getOperand(0));
1641 SDValue B = RHS.getOperand(1);
1642 SDValue C = LHS;
1643
1644 return DAG.getNode(AMDGPUISD::MAD, DL, VT, A, B, C);
1645 }
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001646
1647 if (LHS.getOpcode() == ISD::FADD) {
1648 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
1649
1650 SDValue A = LHS.getOperand(0);
1651 if (A == LHS.getOperand(1)) {
Tom Stellardfb77f002015-01-13 22:59:41 +00001652 const SDValue Two = DAG.getConstantFP(2.0, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001653 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
1654
1655 return DAG.getNode(AMDGPUISD::MAD, DL, VT, Two, A, NegRHS);
1656 }
1657 }
1658
1659 if (RHS.getOpcode() == ISD::FADD) {
1660 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
1661
1662 SDValue A = RHS.getOperand(0);
1663 if (A == RHS.getOperand(1)) {
Tom Stellardfb77f002015-01-13 22:59:41 +00001664 const SDValue NegTwo = DAG.getConstantFP(-2.0, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001665 return DAG.getNode(AMDGPUISD::MAD, DL, VT, NegTwo, A, LHS);
1666 }
1667 }
Matt Arsenault8675db12014-08-29 16:01:14 +00001668 }
1669
1670 break;
1671 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001672 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001673 case ISD::LOAD:
1674 case ISD::STORE:
1675 case ISD::ATOMIC_LOAD:
1676 case ISD::ATOMIC_STORE:
1677 case ISD::ATOMIC_CMP_SWAP:
1678 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
1679 case ISD::ATOMIC_SWAP:
1680 case ISD::ATOMIC_LOAD_ADD:
1681 case ISD::ATOMIC_LOAD_SUB:
1682 case ISD::ATOMIC_LOAD_AND:
1683 case ISD::ATOMIC_LOAD_OR:
1684 case ISD::ATOMIC_LOAD_XOR:
1685 case ISD::ATOMIC_LOAD_NAND:
1686 case ISD::ATOMIC_LOAD_MIN:
1687 case ISD::ATOMIC_LOAD_MAX:
1688 case ISD::ATOMIC_LOAD_UMIN:
1689 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
1690 if (DCI.isBeforeLegalize())
1691 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001692
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001693 MemSDNode *MemNode = cast<MemSDNode>(N);
1694 SDValue Ptr = MemNode->getBasePtr();
1695
1696 // TODO: We could also do this for multiplies.
1697 unsigned AS = MemNode->getAddressSpace();
1698 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
1699 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
1700 if (NewPtr) {
1701 SmallVector<SDValue, 8> NewOps;
Aaron Ballmanf12dc9c2014-08-18 11:51:41 +00001702 for (unsigned I = 0, E = MemNode->getNumOperands(); I != E; ++I)
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001703 NewOps.push_back(MemNode->getOperand(I));
1704
1705 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
1706 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
1707 }
1708 }
1709 break;
1710 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001711 case ISD::AND:
1712 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00001713 case ISD::OR:
1714 return performOrCombine(N, DCI);
1715 case AMDGPUISD::FP_CLASS:
1716 return performClassCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001717 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001718 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001719}
Christian Konigd910b7d2013-02-26 17:52:16 +00001720
Matt Arsenault758659232013-05-18 00:21:46 +00001721/// \brief Test if RegClass is one of the VSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001722static bool isVSrc(unsigned RegClass) {
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001723 switch(RegClass) {
1724 default: return false;
Tom Stellardb6550522015-01-12 19:33:18 +00001725 case AMDGPU::VS_32RegClassID:
1726 case AMDGPU::VS_64RegClassID:
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001727 return true;
1728 }
Christian Konigf82901a2013-02-26 17:52:23 +00001729}
1730
Christian Konigf82901a2013-02-26 17:52:23 +00001731/// \brief Analyze the possible immediate value Op
1732///
1733/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1734/// and the immediate value if it's a literal immediate
1735int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1736
Eric Christopher7792e322015-01-30 23:24:40 +00001737 const SIInstrInfo *TII =
1738 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001739
Tom Stellardedbf1eb2013-04-05 23:31:20 +00001740 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00001741 if (Node->getZExtValue() >> 32)
Tom Stellard7ed0b522014-04-03 20:19:27 +00001742 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001743
Matt Arsenault303011a2014-12-17 21:04:08 +00001744 if (TII->isInlineConstant(Node->getAPIntValue()))
1745 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00001746
Matt Arsenault303011a2014-12-17 21:04:08 +00001747 return Node->getZExtValue();
1748 }
1749
1750 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1751 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
1752 return 0;
1753
1754 if (Node->getValueType(0) == MVT::f32)
1755 return FloatToBits(Node->getValueAPF().convertToFloat());
1756
1757 return -1;
1758 }
1759
1760 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001761}
1762
Eric Christopher7792e322015-01-30 23:24:40 +00001763const TargetRegisterClass *
1764SITargetLowering::getRegClassForNode(SelectionDAG &DAG,
1765 const SDValue &Op) const {
1766 const SIInstrInfo *TII =
1767 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001768 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1769
1770 if (!Op->isMachineOpcode()) {
1771 switch(Op->getOpcode()) {
1772 case ISD::CopyFromReg: {
1773 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1774 unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1775 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1776 return MRI.getRegClass(Reg);
1777 }
1778 return TRI.getPhysRegClass(Reg);
1779 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001780 default: return nullptr;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001781 }
1782 }
1783 const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1784 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1785 if (OpClassID != -1) {
1786 return TRI.getRegClass(OpClassID);
1787 }
1788 switch(Op.getMachineOpcode()) {
1789 case AMDGPU::COPY_TO_REGCLASS:
1790 // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1791 OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1792
1793 // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1794 // class, then the register class for the value could be either a
1795 // VReg or and SReg. In order to get a more accurate
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001796 if (isVSrc(OpClassID))
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001797 return getRegClassForNode(DAG, Op.getOperand(0));
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001798
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001799 return TRI.getRegClass(OpClassID);
1800 case AMDGPU::EXTRACT_SUBREG: {
1801 int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1802 const TargetRegisterClass *SuperClass =
1803 getRegClassForNode(DAG, Op.getOperand(0));
1804 return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1805 }
1806 case AMDGPU::REG_SEQUENCE:
1807 // Operand 0 is the register class id for REG_SEQUENCE instructions.
1808 return TRI.getRegClass(
1809 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1810 default:
1811 return getRegClassFor(Op.getSimpleValueType());
1812 }
1813}
1814
Christian Konigf82901a2013-02-26 17:52:23 +00001815/// \brief Does "Op" fit into register class "RegClass" ?
Tom Stellardb35efba2013-05-20 15:02:01 +00001816bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
Christian Konigf82901a2013-02-26 17:52:23 +00001817 unsigned RegClass) const {
Eric Christopher7792e322015-01-30 23:24:40 +00001818 const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001819 const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1820 if (!RC) {
Christian Konigf82901a2013-02-26 17:52:23 +00001821 return false;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001822 }
1823 return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
Christian Konigf82901a2013-02-26 17:52:23 +00001824}
1825
Christian Konig8e06e2a2013-04-10 08:39:08 +00001826/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00001827static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00001828 switch (Idx) {
1829 default: return 0;
1830 case AMDGPU::sub0: return 0;
1831 case AMDGPU::sub1: return 1;
1832 case AMDGPU::sub2: return 2;
1833 case AMDGPU::sub3: return 3;
1834 }
1835}
1836
1837/// \brief Adjust the writemask of MIMG instructions
1838void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1839 SelectionDAG &DAG) const {
1840 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00001841 unsigned Lane = 0;
1842 unsigned OldDmask = Node->getConstantOperandVal(0);
1843 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001844
1845 // Try to figure out the used register components
1846 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1847 I != E; ++I) {
1848
1849 // Abort if we can't understand the usage
1850 if (!I->isMachineOpcode() ||
1851 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1852 return;
1853
Tom Stellard54774e52013-10-23 02:53:47 +00001854 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1855 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1856 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1857 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00001858 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001859
Tom Stellard54774e52013-10-23 02:53:47 +00001860 // Set which texture component corresponds to the lane.
1861 unsigned Comp;
1862 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1863 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00001864 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00001865 Dmask &= ~(1 << Comp);
1866 }
1867
Christian Konig8e06e2a2013-04-10 08:39:08 +00001868 // Abort if we have more than one user per component
1869 if (Users[Lane])
1870 return;
1871
1872 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00001873 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001874 }
1875
Tom Stellard54774e52013-10-23 02:53:47 +00001876 // Abort if there's no change
1877 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00001878 return;
1879
1880 // Adjust the writemask in the node
1881 std::vector<SDValue> Ops;
Tom Stellard54774e52013-10-23 02:53:47 +00001882 Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001883 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1884 Ops.push_back(Node->getOperand(i));
Craig Topper8c0b4d02014-04-28 05:57:50 +00001885 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001886
Christian Konig8b1ed282013-04-10 08:39:16 +00001887 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00001888 // (if NewDmask has only one bit set...)
1889 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001890 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00001891 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001892 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00001893 SDValue(Node, 0), RC);
1894 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1895 return;
1896 }
1897
Christian Konig8e06e2a2013-04-10 08:39:08 +00001898 // Update the users of the node with the new indices
1899 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1900
1901 SDNode *User = Users[i];
1902 if (!User)
1903 continue;
1904
1905 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1906 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1907
1908 switch (Idx) {
1909 default: break;
1910 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1911 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1912 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1913 }
1914 }
1915}
1916
Tom Stellard3457a842014-10-09 19:06:00 +00001917/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
1918/// with frame index operands.
1919/// LLVM assumes that inputs are to these instructions are registers.
1920void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
1921 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00001922
1923 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00001924 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
1925 if (!isa<FrameIndexSDNode>(Node->getOperand(i))) {
1926 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00001927 continue;
1928 }
1929
Tom Stellard3457a842014-10-09 19:06:00 +00001930 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00001931 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00001932 Node->getOperand(i).getValueType(),
1933 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00001934 }
1935
Tom Stellard3457a842014-10-09 19:06:00 +00001936 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00001937}
1938
Matt Arsenault08d84942014-06-03 23:06:13 +00001939/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00001940SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1941 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00001942 const SIInstrInfo *TII =
1943 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellard0518ff82013-06-03 17:39:58 +00001944 Node = AdjustRegClass(Node, DAG);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001945
Tom Stellard16a9a202013-08-14 23:24:17 +00001946 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00001947 adjustWritemask(Node, DAG);
1948
Matt Arsenault7d858d82014-11-02 23:46:54 +00001949 if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG ||
1950 Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00001951 legalizeTargetIndependentNode(Node, DAG);
1952 return Node;
1953 }
Tom Stellard654d6692015-01-08 15:08:17 +00001954 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001955}
Christian Konig8b1ed282013-04-10 08:39:16 +00001956
1957/// \brief Assign the register class depending on the number of
1958/// bits set in the writemask
1959void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1960 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00001961 const SIInstrInfo *TII =
1962 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001963
Tom Stellarda99ada52014-11-21 22:31:44 +00001964 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00001965 TII->legalizeOperands(MI);
1966
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001967 if (TII->isMIMG(MI->getOpcode())) {
1968 unsigned VReg = MI->getOperand(0).getReg();
1969 unsigned Writemask = MI->getOperand(1).getImm();
1970 unsigned BitsSet = 0;
1971 for (unsigned i = 0; i < 4; ++i)
1972 BitsSet += Writemask & (1 << i) ? 1 : 0;
1973
1974 const TargetRegisterClass *RC;
1975 switch (BitsSet) {
1976 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001977 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001978 case 2: RC = &AMDGPU::VReg_64RegClass; break;
1979 case 3: RC = &AMDGPU::VReg_96RegClass; break;
1980 }
1981
1982 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1983 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001984 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00001985 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00001986 }
1987
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001988 // Replace unused atomics with the no return version.
1989 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
1990 if (NoRetAtomicOp != -1) {
1991 if (!Node->hasAnyUseOfValue(0)) {
1992 MI->setDesc(TII->get(NoRetAtomicOp));
1993 MI->RemoveOperand(0);
1994 }
1995
1996 return;
1997 }
Christian Konig8b1ed282013-04-10 08:39:16 +00001998}
Tom Stellard0518ff82013-06-03 17:39:58 +00001999
Matt Arsenault485defe2014-11-05 19:01:17 +00002000static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
2001 SDValue K = DAG.getTargetConstant(Val, MVT::i32);
2002 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
2003}
2004
2005MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
2006 SDLoc DL,
2007 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002008 const SIInstrInfo *TII =
2009 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00002010#if 1
2011 // XXX - Workaround for moveToVALU not handling different register class
2012 // inserts for REG_SEQUENCE.
2013
2014 // Build the half of the subregister with the constants.
2015 const SDValue Ops0[] = {
2016 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, MVT::i32),
2017 buildSMovImm32(DAG, DL, 0),
2018 DAG.getTargetConstant(AMDGPU::sub0, MVT::i32),
Tom Stellard794c8c02014-12-02 17:05:41 +00002019 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002020 DAG.getTargetConstant(AMDGPU::sub1, MVT::i32)
2021 };
2022
2023 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
2024 MVT::v2i32, Ops0), 0);
2025
2026 // Combine the constants and the pointer.
2027 const SDValue Ops1[] = {
2028 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2029 Ptr,
2030 DAG.getTargetConstant(AMDGPU::sub0_sub1, MVT::i32),
2031 SubRegHi,
2032 DAG.getTargetConstant(AMDGPU::sub2_sub3, MVT::i32)
2033 };
2034
2035 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
2036#else
2037 const SDValue Ops[] = {
2038 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2039 Ptr,
2040 DAG.getTargetConstant(AMDGPU::sub0_sub1, MVT::i32),
2041 buildSMovImm32(DAG, DL, 0),
2042 DAG.getTargetConstant(AMDGPU::sub2, MVT::i32),
Tom Stellard794c8c02014-12-02 17:05:41 +00002043 buildSMovImm32(DAG, DL, TII->getDefaultRsrcFormat() >> 32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002044 DAG.getTargetConstant(AMDGPU::sub3, MVT::i32)
2045 };
2046
2047 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2048
2049#endif
2050}
2051
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002052/// \brief Return a resource descriptor with the 'Add TID' bit enabled
2053/// The TID (Thread ID) is multipled by the stride value (bits [61:48]
2054/// of the resource descriptor) to create an offset, which is added to the
2055/// resource ponter.
2056MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
2057 SDLoc DL,
2058 SDValue Ptr,
2059 uint32_t RsrcDword1,
2060 uint64_t RsrcDword2And3) const {
2061 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
2062 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
2063 if (RsrcDword1) {
2064 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
2065 DAG.getConstant(RsrcDword1, MVT::i32)), 0);
2066 }
2067
2068 SDValue DataLo = buildSMovImm32(DAG, DL,
2069 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
2070 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
2071
2072 const SDValue Ops[] = {
2073 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2074 PtrLo,
2075 DAG.getTargetConstant(AMDGPU::sub0, MVT::i32),
2076 PtrHi,
2077 DAG.getTargetConstant(AMDGPU::sub1, MVT::i32),
2078 DataLo,
2079 DAG.getTargetConstant(AMDGPU::sub2, MVT::i32),
2080 DataHi,
2081 DAG.getTargetConstant(AMDGPU::sub3, MVT::i32)
2082 };
2083
2084 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2085}
2086
2087MachineSDNode *SITargetLowering::buildScratchRSRC(SelectionDAG &DAG,
2088 SDLoc DL,
2089 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002090 const SIInstrInfo *TII =
2091 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellard794c8c02014-12-02 17:05:41 +00002092 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() | AMDGPU::RSRC_TID_ENABLE |
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002093 0xffffffff; // Size
2094
2095 return buildRSRC(DAG, DL, Ptr, 0, Rsrc);
2096}
2097
Tom Stellard0518ff82013-06-03 17:39:58 +00002098MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
2099 SelectionDAG &DAG) const {
2100
2101 SDLoc DL(N);
2102 unsigned NewOpcode = N->getMachineOpcode();
2103
2104 switch (N->getMachineOpcode()) {
2105 default: return N;
Tom Stellard0518ff82013-06-03 17:39:58 +00002106 case AMDGPU::S_LOAD_DWORD_IMM:
2107 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
2108 // Fall-through
2109 case AMDGPU::S_LOAD_DWORDX2_SGPR:
2110 if (NewOpcode == N->getMachineOpcode()) {
2111 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
2112 }
2113 // Fall-through
2114 case AMDGPU::S_LOAD_DWORDX4_IMM:
2115 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
2116 if (NewOpcode == N->getMachineOpcode()) {
2117 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
2118 }
2119 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
2120 return N;
2121 }
2122 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
Matt Arsenault485defe2014-11-05 19:01:17 +00002123
2124 const SDValue Zero64 = DAG.getTargetConstant(0, MVT::i64);
2125 SDValue Ptr(DAG.getMachineNode(AMDGPU::S_MOV_B64, DL, MVT::i64, Zero64), 0);
2126 MachineSDNode *RSrc = wrapAddr64Rsrc(DAG, DL, Ptr);
Matt Arsenault61a528a2014-09-10 23:26:19 +00002127
2128 SmallVector<SDValue, 8> Ops;
2129 Ops.push_back(SDValue(RSrc, 0));
2130 Ops.push_back(N->getOperand(0));
Marek Olsak58f61a82014-12-07 17:17:38 +00002131
2132 // The immediate offset is in dwords on SI and in bytes on VI.
2133 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
Marek Olsak0c056452014-12-10 19:25:31 +00002134 Ops.push_back(DAG.getTargetConstant(Offset->getSExtValue(), MVT::i32));
Marek Olsak58f61a82014-12-07 17:17:38 +00002135 else
Marek Olsak0c056452014-12-10 19:25:31 +00002136 Ops.push_back(DAG.getTargetConstant(Offset->getSExtValue() << 2, MVT::i32));
Matt Arsenault61a528a2014-09-10 23:26:19 +00002137
2138 // Copy remaining operands so we keep any chain and glue nodes that follow
2139 // the normal operands.
2140 for (unsigned I = 2, E = N->getNumOperands(); I != E; ++I)
2141 Ops.push_back(N->getOperand(I));
2142
Tom Stellard0518ff82013-06-03 17:39:58 +00002143 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
2144 }
2145 }
2146}
Tom Stellard94593ee2013-06-03 17:40:18 +00002147
2148SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2149 const TargetRegisterClass *RC,
2150 unsigned Reg, EVT VT) const {
2151 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2152
2153 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2154 cast<RegisterSDNode>(VReg)->getReg(), VT);
2155}