blob: 71aec8400d7cca5357a1598ade9a73fb88fbbc04 [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
38SITargetLowering::SITargetLowering(TargetMachine &TM) :
Bill Wendling37e9adb2013-06-07 20:28:55 +000039 AMDGPUTargetLowering(TM) {
Tom Stellard1bd80722014-04-30 15:31:33 +000040 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000041 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000042
Christian Konig2214f142013-03-07 09:03:38 +000043 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
44 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
45
Tom Stellard334b29c2014-04-17 21:00:09 +000046 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000047 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000048
Tom Stellard436780b2014-05-15 14:41:57 +000049 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
50 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
51 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000052
Tom Stellard436780b2014-05-15 14:41:57 +000053 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
54 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000055
Tom Stellardf0a21072014-11-18 20:39:39 +000056 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000057 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
58
Tom Stellardf0a21072014-11-18 20:39:39 +000059 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000060 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000061
62 computeRegisterProperties();
63
Christian Konig2989ffc2013-03-18 11:34:16 +000064 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
65 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
66 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
67 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
68
Tom Stellard75aadc22012-12-11 21:25:42 +000069 setOperationAction(ISD::ADD, MVT::i32, Legal);
Matt Arsenaulte8d21462013-11-18 20:09:40 +000070 setOperationAction(ISD::ADDC, MVT::i32, Legal);
71 setOperationAction(ISD::ADDE, MVT::i32, Legal);
Matt Arsenaultb8b51532014-06-23 18:00:38 +000072 setOperationAction(ISD::SUBC, MVT::i32, Legal);
73 setOperationAction(ISD::SUBE, MVT::i32, Legal);
Aaron Watrydaabb202013-06-25 13:55:52 +000074
Matt Arsenaultad14ce82014-07-19 18:44:39 +000075 setOperationAction(ISD::FSIN, MVT::f32, Custom);
76 setOperationAction(ISD::FCOS, MVT::f32, Custom);
77
Matt Arsenault7c936902014-10-21 23:01:01 +000078 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
79 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
80 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
81 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
82
Tom Stellard35bb18c2013-08-26 15:06:04 +000083 // We need to custom lower vector stores from local memory
Tom Stellard35bb18c2013-08-26 15:06:04 +000084 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000085 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
86 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
87
88 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
89 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000090
Tom Stellard1c8788e2014-03-07 20:12:33 +000091 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000092 setOperationAction(ISD::STORE, MVT::i32, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000093 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
94 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
95
Tom Stellard0ec134f2014-02-04 17:18:40 +000096 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +000097 setOperationAction(ISD::SELECT, MVT::f64, Promote);
98 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +000099
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000100 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
101 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
102 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
103 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000104
Tom Stellard83747202013-07-18 21:43:53 +0000105 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
106 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
107
Matt Arsenaulte306a322014-10-21 16:25:08 +0000108 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
109
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000110 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000111 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
113
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000114 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000115 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
116 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
117
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
121
Matt Arsenault94812212014-11-14 18:18:16 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
124
Tom Stellard94593ee2013-06-03 17:40:18 +0000125 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
127 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
128 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000129
Tom Stellardafcf12f2013-09-12 02:55:14 +0000130 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000131 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000132
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000133 for (MVT VT : MVT::integer_valuetypes()) {
Matt Arsenaultbd223422015-01-14 01:35:17 +0000134 if (VT == MVT::i64)
135 continue;
136
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000137 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000138 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
139 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000140 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000141
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000142 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000143 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
144 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000145 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000146
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000147 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000148 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
149 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000150 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
151 }
152
153 for (MVT VT : MVT::integer_vector_valuetypes()) {
154 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
155 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
156 }
157
158 for (MVT VT : MVT::fp_valuetypes())
159 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000160
Tom Stellarde9373602014-01-22 19:24:14 +0000161 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
162 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000163 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Matt Arsenault6f243792013-09-05 19:41:10 +0000164 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000165 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
166 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000167
Matt Arsenault470acd82014-04-15 22:28:39 +0000168 setOperationAction(ISD::LOAD, MVT::i1, Custom);
169
Tom Stellardfd155822013-08-26 15:05:36 +0000170 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000171 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000172 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000173
Tom Stellard5f337882014-04-29 23:12:43 +0000174 // These should use UDIVREM, so set them to expand
175 setOperationAction(ISD::UDIV, MVT::i64, Expand);
176 setOperationAction(ISD::UREM, MVT::i64, Expand);
177
Tom Stellard967bf582014-02-13 23:34:15 +0000178 // We only support LOAD/STORE and vector manipulation ops for vectors
179 // with > 4 elements.
180 MVT VecTypes[] = {
Tom Stellardd61a1c32014-02-28 21:36:37 +0000181 MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32
Tom Stellard967bf582014-02-13 23:34:15 +0000182 };
183
Matt Arsenault0d89e842014-07-15 21:44:37 +0000184 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
185 setOperationAction(ISD::SELECT, MVT::i1, Promote);
186
Matt Arsenaultd504a742014-05-15 21:44:05 +0000187 for (MVT VT : VecTypes) {
Tom Stellard967bf582014-02-13 23:34:15 +0000188 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
189 switch(Op) {
190 case ISD::LOAD:
191 case ISD::STORE:
192 case ISD::BUILD_VECTOR:
193 case ISD::BITCAST:
194 case ISD::EXTRACT_VECTOR_ELT:
195 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000196 case ISD::INSERT_SUBVECTOR:
197 case ISD::EXTRACT_SUBVECTOR:
198 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000199 case ISD::CONCAT_VECTORS:
200 setOperationAction(Op, VT, Custom);
201 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000202 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000203 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000204 break;
205 }
206 }
207 }
208
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000209 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
210 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
211 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
212 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000213 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000214 }
215
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000216 setOperationAction(ISD::FDIV, MVT::f32, Custom);
217
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000218 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000219 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000220 setTargetDAGCombine(ISD::FMINNUM);
221 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000222 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +0000223 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000224 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000225 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000226 setTargetDAGCombine(ISD::UINT_TO_FP);
227
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000228 // All memory operations. Some folding on the pointer operand is done to help
229 // matching the constant offsets in the addressing modes.
230 setTargetDAGCombine(ISD::LOAD);
231 setTargetDAGCombine(ISD::STORE);
232 setTargetDAGCombine(ISD::ATOMIC_LOAD);
233 setTargetDAGCombine(ISD::ATOMIC_STORE);
234 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
235 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
236 setTargetDAGCombine(ISD::ATOMIC_SWAP);
237 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
238 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
239 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
240 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
241 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
242 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
243 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
244 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
245 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
246 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
247
Christian Konigeecebd02013-03-26 14:04:02 +0000248 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000249}
250
Tom Stellard0125f2a2013-06-25 02:39:35 +0000251//===----------------------------------------------------------------------===//
252// TargetLowering queries
253//===----------------------------------------------------------------------===//
254
Matt Arsenaulte306a322014-10-21 16:25:08 +0000255bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
256 EVT) const {
257 // SI has some legal vector types, but no legal vector operations. Say no
258 // shuffles are legal in order to prefer scalarizing some vector operations.
259 return false;
260}
261
Matt Arsenault5015a892014-08-15 17:17:07 +0000262// FIXME: This really needs an address space argument. The immediate offset
263// size is different for different sets of memory instruction sets.
264
265// The single offset DS instructions have a 16-bit unsigned byte offset.
266//
267// MUBUF / MTBUF have a 12-bit unsigned byte offset, and additionally can do r +
268// r + i with addr64. 32-bit has more addressing mode options. Depending on the
269// resource constant, it can also do (i64 r0) + (i32 r1) * (i14 i).
270//
271// SMRD instructions have an 8-bit, dword offset.
272//
273bool SITargetLowering::isLegalAddressingMode(const AddrMode &AM,
274 Type *Ty) const {
275 // No global is ever allowed as a base.
276 if (AM.BaseGV)
277 return false;
278
279 // Allow a 16-bit unsigned immediate field, since this is what DS instructions
280 // use.
281 if (!isUInt<16>(AM.BaseOffs))
282 return false;
283
284 // Only support r+r,
285 switch (AM.Scale) {
286 case 0: // "r+i" or just "i", depending on HasBaseReg.
287 break;
288 case 1:
289 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
290 return false;
291 // Otherwise we have r+r or r+i.
292 break;
293 case 2:
294 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
295 return false;
296 // Allow 2*r as r+r.
297 break;
298 default: // Don't allow n * r
299 return false;
300 }
301
302 return true;
303}
304
Matt Arsenaulte6986632015-01-14 01:35:22 +0000305bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000306 unsigned AddrSpace,
307 unsigned Align,
308 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000309 if (IsFast)
310 *IsFast = false;
311
Matt Arsenault1018c892014-04-24 17:08:26 +0000312 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
313 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000314 if (!VT.isSimple() || VT == MVT::Other)
315 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000316
317 // XXX - CI changes say "Support for unaligned memory accesses" but I don't
318 // see what for specifically. The wording everywhere else seems to be the
319 // same.
320
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 Stellard0125f2a2013-06-25 02:39:35 +0000336 return VT.bitsGT(MVT::i32);
337}
338
Matt Arsenault46645fa2014-07-28 17:49:26 +0000339EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
340 unsigned SrcAlign, bool IsMemset,
341 bool ZeroMemset,
342 bool MemcpyStrSrc,
343 MachineFunction &MF) const {
344 // FIXME: Should account for address space here.
345
346 // The default fallback uses the private pointer size as a guess for a type to
347 // use. Make sure we switch these to 64-bit accesses.
348
349 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
350 return MVT::v4i32;
351
352 if (Size >= 8 && DstAlign >= 4)
353 return MVT::v2i32;
354
355 // Use the default.
356 return MVT::Other;
357}
358
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000359TargetLoweringBase::LegalizeTypeAction
360SITargetLowering::getPreferredVectorAction(EVT VT) const {
361 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
362 return TypeSplitVector;
363
364 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000365}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000366
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000367bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
368 Type *Ty) const {
Eric Christopherd9134482014-08-04 21:25:23 +0000369 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
370 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000371 return TII->isInlineConstant(Imm);
372}
373
Tom Stellardaf775432013-10-23 00:44:32 +0000374SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000375 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000376 unsigned Offset, bool Signed) const {
Matt Arsenault86033ca2014-07-28 17:31:39 +0000377 const DataLayout *DL = getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000378 MachineFunction &MF = DAG.getMachineFunction();
379 const SIRegisterInfo *TRI =
380 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
381 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000382
Matt Arsenault86033ca2014-07-28 17:31:39 +0000383 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
384
385 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
386 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
387 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000388 MRI.getLiveInVirtReg(InputPtrReg), MVT::i64);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000389 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, BasePtr,
390 DAG.getConstant(Offset, MVT::i64));
391 SDValue PtrOffset = DAG.getUNDEF(getPointerTy(AMDGPUAS::CONSTANT_ADDRESS));
392 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
393
394 return DAG.getLoad(ISD::UNINDEXED, Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD,
395 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
396 false, // isVolatile
397 true, // isNonTemporal
398 true, // isInvariant
399 DL->getABITypeAlignment(Ty)); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000400}
401
Christian Konig2c8f6d52013-03-07 09:03:52 +0000402SDValue SITargetLowering::LowerFormalArguments(
403 SDValue Chain,
404 CallingConv::ID CallConv,
405 bool isVarArg,
406 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000407 SDLoc DL, SelectionDAG &DAG,
Christian Konig2c8f6d52013-03-07 09:03:52 +0000408 SmallVectorImpl<SDValue> &InVals) const {
409
Tom Stellardec2e43c2014-09-22 15:35:29 +0000410 const TargetMachine &TM = getTargetMachine();
411 const SIRegisterInfo *TRI =
412 static_cast<const SIRegisterInfo*>(TM.getSubtargetImpl()->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000413
414 MachineFunction &MF = DAG.getMachineFunction();
415 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000416 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000417
418 assert(CallConv == CallingConv::C);
419
420 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000421 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000422
423 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000424 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000425
426 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000427 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Vincent Lejeuned6236442013-10-13 17:56:16 +0000428 !Arg.Flags.isByVal()) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000429
430 assert((PSInputNum <= 15) && "Too many PS inputs!");
431
432 if (!Arg.Used) {
433 // We can savely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000434 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000435 ++PSInputNum;
436 continue;
437 }
438
439 Info->PSInputAddr |= 1 << PSInputNum++;
440 }
441
442 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000443 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000444 ISD::InputArg NewArg = Arg;
445 NewArg.Flags.setSplit();
446 NewArg.VT = Arg.VT.getVectorElementType();
447
448 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
449 // three or five element vertex only needs three or five registers,
450 // NOT four or eigth.
451 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
452 unsigned NumElements = ParamType->getVectorNumElements();
453
454 for (unsigned j = 0; j != NumElements; ++j) {
455 Splits.push_back(NewArg);
456 NewArg.PartOffset += NewArg.VT.getStoreSize();
457 }
458
Matt Arsenault762af962014-07-13 03:06:39 +0000459 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000460 Splits.push_back(Arg);
461 }
462 }
463
464 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000465 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
466 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000467
Christian Konig99ee0f42013-03-07 09:04:14 +0000468 // At least one interpolation mode must be enabled or else the GPU will hang.
Matt Arsenault762af962014-07-13 03:06:39 +0000469 if (Info->getShaderType() == ShaderType::PIXEL &&
470 (Info->PSInputAddr & 0x7F) == 0) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000471 Info->PSInputAddr |= 1;
472 CCInfo.AllocateReg(AMDGPU::VGPR0);
473 CCInfo.AllocateReg(AMDGPU::VGPR1);
474 }
475
Tom Stellarded882c22013-06-03 17:40:11 +0000476 // The pointer to the list of arguments is stored in SGPR0, SGPR1
Tom Stellardb02094e2014-07-21 15:45:01 +0000477 // The pointer to the scratch buffer is stored in SGPR2, SGPR3
Matt Arsenault762af962014-07-13 03:06:39 +0000478 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardfeab91c2014-12-02 17:41:43 +0000479 if (Subtarget->isAmdHsaOS())
480 Info->NumUserSGPRs = 2; // FIXME: Need to support scratch buffers.
481 else
482 Info->NumUserSGPRs = 4;
Tom Stellardec2e43c2014-09-22 15:35:29 +0000483
484 unsigned InputPtrReg =
485 TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
486 unsigned InputPtrRegLo =
487 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 0);
488 unsigned InputPtrRegHi =
489 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 1);
490
491 unsigned ScratchPtrReg =
492 TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_PTR);
493 unsigned ScratchPtrRegLo =
494 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 0);
495 unsigned ScratchPtrRegHi =
496 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 1);
497
498 CCInfo.AllocateReg(InputPtrRegLo);
499 CCInfo.AllocateReg(InputPtrRegHi);
500 CCInfo.AllocateReg(ScratchPtrRegLo);
501 CCInfo.AllocateReg(ScratchPtrRegHi);
502 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
503 MF.addLiveIn(ScratchPtrReg, &AMDGPU::SReg_64RegClass);
Tom Stellarded882c22013-06-03 17:40:11 +0000504 }
505
Matt Arsenault762af962014-07-13 03:06:39 +0000506 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000507 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
508 Splits);
509 }
510
Christian Konig2c8f6d52013-03-07 09:03:52 +0000511 AnalyzeFormalArguments(CCInfo, Splits);
512
513 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
514
Christian Konigb7be72d2013-05-17 09:46:48 +0000515 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000516 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000517 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000518 continue;
519 }
520
Christian Konig2c8f6d52013-03-07 09:03:52 +0000521 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000522 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000523
524 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000525 VT = Ins[i].VT;
526 EVT MemVT = Splits[i].VT;
Jan Veselye5121f32014-10-14 20:05:26 +0000527 const unsigned Offset = 36 + VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000528 // The first 36 bytes of the input buffer contains information about
529 // thread group and global sizes.
Tom Stellardaf775432013-10-23 00:44:32 +0000530 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
Jan Veselye5121f32014-10-14 20:05:26 +0000531 Offset, Ins[i].Flags.isSExt());
Tom Stellardca7ecf32014-08-22 18:49:31 +0000532
533 const PointerType *ParamTy =
534 dyn_cast<PointerType>(FType->getParamType(Ins[i].OrigArgIndex));
535 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
536 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
537 // On SI local pointers are just offsets into LDS, so they are always
538 // less than 16-bits. On CI and newer they could potentially be
539 // real pointers, so we can't guarantee their size.
540 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
541 DAG.getValueType(MVT::i16));
542 }
543
Tom Stellarded882c22013-06-03 17:40:11 +0000544 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000545 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000546 continue;
547 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000548 assert(VA.isRegLoc() && "Parameter must be in a register!");
549
550 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000551
552 if (VT == MVT::i64) {
553 // For now assume it is a pointer
554 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
555 &AMDGPU::SReg_64RegClass);
556 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
557 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
558 continue;
559 }
560
561 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
562
563 Reg = MF.addLiveIn(Reg, RC);
564 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
565
Christian Konig2c8f6d52013-03-07 09:03:52 +0000566 if (Arg.VT.isVector()) {
567
568 // Build a vector from the registers
569 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
570 unsigned NumElements = ParamType->getVectorNumElements();
571
572 SmallVector<SDValue, 4> Regs;
573 Regs.push_back(Val);
574 for (unsigned j = 1; j != NumElements; ++j) {
575 Reg = ArgLocs[ArgIdx++].getLocReg();
576 Reg = MF.addLiveIn(Reg, RC);
577 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
578 }
579
580 // Fill up the missing vector elements
581 NumElements = Arg.VT.getVectorNumElements() - NumElements;
582 for (unsigned j = 0; j != NumElements; ++j)
583 Regs.push_back(DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000584
Craig Topper48d114b2014-04-26 18:35:24 +0000585 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000586 continue;
587 }
588
589 InVals.push_back(Val);
590 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000591
592 if (Info->getShaderType() != ShaderType::COMPUTE) {
593 unsigned ScratchIdx = CCInfo.getFirstUnallocated(
594 AMDGPU::SGPR_32RegClass.begin(), AMDGPU::SGPR_32RegClass.getNumRegs());
595 Info->ScratchOffsetReg = AMDGPU::SGPR_32RegClass.getRegister(ScratchIdx);
596 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000597 return Chain;
598}
599
Tom Stellard75aadc22012-12-11 21:25:42 +0000600MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
601 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000602
Tom Stellard556d9aa2013-06-03 17:39:37 +0000603 MachineBasicBlock::iterator I = *MI;
Eric Christopherd9134482014-08-04 21:25:23 +0000604 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
605 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Tom Stellard556d9aa2013-06-03 17:39:37 +0000606
Tom Stellard75aadc22012-12-11 21:25:42 +0000607 switch (MI->getOpcode()) {
608 default:
609 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
610 case AMDGPU::BRANCH: return BB;
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000611 case AMDGPU::V_SUB_F64: {
612 unsigned DestReg = MI->getOperand(0).getReg();
613 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64), DestReg)
614 .addImm(0) // SRC0 modifiers
615 .addReg(MI->getOperand(1).getReg())
616 .addImm(1) // SRC1 modifiers
617 .addReg(MI->getOperand(2).getReg())
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000618 .addImm(0) // CLAMP
619 .addImm(0); // OMOD
Tom Stellard2a6a61052013-07-12 18:15:08 +0000620 MI->eraseFromParent();
621 break;
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000622 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000623 case AMDGPU::SI_RegisterStorePseudo: {
624 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard81d871d2013-11-13 23:36:50 +0000625 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
626 MachineInstrBuilder MIB =
627 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
628 Reg);
629 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
630 MIB.addOperand(MI->getOperand(i));
631
632 MI->eraseFromParent();
Vincent Lejeune79a58342014-05-10 19:18:25 +0000633 break;
634 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000635 }
636 return BB;
637}
638
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000639bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
640 // This currently forces unfolding various combinations of fsub into fma with
641 // free fneg'd operands. As long as we have fast FMA (controlled by
642 // isFMAFasterThanFMulAndFAdd), we should perform these.
643
644 // When fma is quarter rate, for f64 where add / sub are at best half rate,
645 // most of these combines appear to be cycle neutral but save on instruction
646 // count / code size.
647 return true;
648}
649
Matt Arsenault8596f712014-11-28 22:51:38 +0000650EVT SITargetLowering::getSetCCResultType(LLVMContext &Ctx, EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +0000651 if (!VT.isVector()) {
652 return MVT::i1;
653 }
Matt Arsenault8596f712014-11-28 22:51:38 +0000654 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +0000655}
656
Christian Konig082a14a2013-03-18 11:34:05 +0000657MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
658 return MVT::i32;
659}
660
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000661// Answering this is somewhat tricky and depends on the specific device which
662// have different rates for fma or all f64 operations.
663//
664// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
665// regardless of which device (although the number of cycles differs between
666// devices), so it is always profitable for f64.
667//
668// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
669// only on full rate devices. Normally, we should prefer selecting v_mad_f32
670// which we can always do even without fused FP ops since it returns the same
671// result as the separate operations and since it is always full
672// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
673// however does not support denormals, so we do report fma as faster if we have
674// a fast fma device and require denormals.
675//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000676bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
677 VT = VT.getScalarType();
678
679 if (!VT.isSimple())
680 return false;
681
682 switch (VT.getSimpleVT().SimpleTy) {
683 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000684 // This is as fast on some subtargets. However, we always have full rate f32
685 // mad available which returns the same result as the separate operations
686 // which we should prefer over fma.
687 return false;
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000688 case MVT::f64:
689 return true;
690 default:
691 break;
692 }
693
694 return false;
695}
696
Tom Stellard75aadc22012-12-11 21:25:42 +0000697//===----------------------------------------------------------------------===//
698// Custom DAG Lowering Operations
699//===----------------------------------------------------------------------===//
700
701SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
702 switch (Op.getOpcode()) {
703 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +0000704 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000705 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000706 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +0000707 SDValue Result = LowerLOAD(Op, DAG);
708 assert((!Result.getNode() ||
709 Result.getNode()->getNumValues() == 2) &&
710 "Load should return a value and a chain");
711 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +0000712 }
Tom Stellardaf775432013-10-23 00:44:32 +0000713
Matt Arsenaultad14ce82014-07-19 18:44:39 +0000714 case ISD::FSIN:
715 case ISD::FCOS:
716 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000717 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000718 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000719 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000720 case ISD::GlobalAddress: {
721 MachineFunction &MF = DAG.getMachineFunction();
722 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
723 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +0000724 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000725 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
726 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000727 }
728 return SDValue();
729}
730
Tom Stellardf8794352012-12-19 22:10:31 +0000731/// \brief Helper function for LowerBRCOND
732static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000733
Tom Stellardf8794352012-12-19 22:10:31 +0000734 SDNode *Parent = Value.getNode();
735 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
736 I != E; ++I) {
737
738 if (I.getUse().get() != Value)
739 continue;
740
741 if (I->getOpcode() == Opcode)
742 return *I;
743 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000744 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000745}
746
Tom Stellardb02094e2014-07-21 15:45:01 +0000747SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
748
Tom Stellardb02094e2014-07-21 15:45:01 +0000749 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
750 unsigned FrameIndex = FINode->getIndex();
751
Tom Stellardb02094e2014-07-21 15:45:01 +0000752 return DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
753}
754
Tom Stellardf8794352012-12-19 22:10:31 +0000755/// This transforms the control flow intrinsics to get the branch destination as
756/// last parameter, also switches branch target with BR if the need arise
757SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
758 SelectionDAG &DAG) const {
759
Andrew Trickef9de2a2013-05-25 02:42:55 +0000760 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000761
762 SDNode *Intr = BRCOND.getOperand(1).getNode();
763 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +0000764 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000765
766 if (Intr->getOpcode() == ISD::SETCC) {
767 // As long as we negate the condition everything is fine
768 SDNode *SetCC = Intr;
769 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000770 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
771 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000772 Intr = SetCC->getOperand(0).getNode();
773
774 } else {
775 // Get the target from BR if we don't negate the condition
776 BR = findUser(BRCOND, ISD::BR);
777 Target = BR->getOperand(1);
778 }
779
780 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
781
782 // Build the result and
783 SmallVector<EVT, 4> Res;
784 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
785 Res.push_back(Intr->getValueType(i));
786
787 // operands of the new intrinsic call
788 SmallVector<SDValue, 4> Ops;
789 Ops.push_back(BRCOND.getOperand(0));
790 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
791 Ops.push_back(Intr->getOperand(i));
792 Ops.push_back(Target);
793
794 // build the new intrinsic call
795 SDNode *Result = DAG.getNode(
796 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +0000797 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000798
799 if (BR) {
800 // Give the branch instruction our target
801 SDValue Ops[] = {
802 BR->getOperand(0),
803 BRCOND.getOperand(2)
804 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000805 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
806 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
807 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000808 }
809
810 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
811
812 // Copy the intrinsic results to registers
813 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
814 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
815 if (!CopyToReg)
816 continue;
817
818 Chain = DAG.getCopyToReg(
819 Chain, DL,
820 CopyToReg->getOperand(1),
821 SDValue(Result, i - 1),
822 SDValue());
823
824 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
825 }
826
827 // Remove the old intrinsic from the chain
828 DAG.ReplaceAllUsesOfValueWith(
829 SDValue(Intr, Intr->getNumValues() - 1),
830 Intr->getOperand(0));
831
832 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000833}
834
Tom Stellard067c8152014-07-21 14:01:14 +0000835SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
836 SDValue Op,
837 SelectionDAG &DAG) const {
838 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
839
840 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
841 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
842
843 SDLoc DL(GSD);
844 const GlobalValue *GV = GSD->getGlobal();
845 MVT PtrVT = getPointerTy(GSD->getAddressSpace());
846
847 SDValue Ptr = DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT);
848 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
849
850 SDValue PtrLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
851 DAG.getConstant(0, MVT::i32));
852 SDValue PtrHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
853 DAG.getConstant(1, MVT::i32));
854
855 SDValue Lo = DAG.getNode(ISD::ADDC, DL, DAG.getVTList(MVT::i32, MVT::Glue),
856 PtrLo, GA);
857 SDValue Hi = DAG.getNode(ISD::ADDE, DL, DAG.getVTList(MVT::i32, MVT::Glue),
858 PtrHi, DAG.getConstant(0, MVT::i32),
859 SDValue(Lo.getNode(), 1));
860 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
861}
862
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000863SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
864 SelectionDAG &DAG) const {
865 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000866 const SIRegisterInfo *TRI =
867 static_cast<const SIRegisterInfo*>(MF.getSubtarget().getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000868
869 EVT VT = Op.getValueType();
870 SDLoc DL(Op);
871 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
872
873 switch (IntrinsicID) {
874 case Intrinsic::r600_read_ngroups_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000875 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
876 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000877 case Intrinsic::r600_read_ngroups_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000878 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
879 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000880 case Intrinsic::r600_read_ngroups_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000881 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
882 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000883 case Intrinsic::r600_read_global_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000884 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
885 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000886 case Intrinsic::r600_read_global_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000887 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
888 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000889 case Intrinsic::r600_read_global_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000890 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
891 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000892 case Intrinsic::r600_read_local_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000893 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
894 SI::KernelInputOffsets::LOCAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000895 case Intrinsic::r600_read_local_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000896 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
897 SI::KernelInputOffsets::LOCAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000898 case Intrinsic::r600_read_local_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000899 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
900 SI::KernelInputOffsets::LOCAL_SIZE_Z, false);
Jan Veselye5121f32014-10-14 20:05:26 +0000901
902 case Intrinsic::AMDGPU_read_workdim:
903 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
904 MF.getInfo<SIMachineFunctionInfo>()->ABIArgOffset,
905 false);
906
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000907 case Intrinsic::r600_read_tgid_x:
908 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000909 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000910 case Intrinsic::r600_read_tgid_y:
911 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000912 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000913 case Intrinsic::r600_read_tgid_z:
914 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000915 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000916 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000917 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000918 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000919 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000920 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000921 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000922 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000923 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000924 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000925 case AMDGPUIntrinsic::SI_load_const: {
926 SDValue Ops[] = {
927 Op.getOperand(1),
928 Op.getOperand(2)
929 };
930
931 MachineMemOperand *MMO = MF.getMachineMemOperand(
932 MachinePointerInfo(),
933 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
934 VT.getStoreSize(), 4);
935 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
936 Op->getVTList(), Ops, VT, MMO);
937 }
938 case AMDGPUIntrinsic::SI_sample:
939 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
940 case AMDGPUIntrinsic::SI_sampleb:
941 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
942 case AMDGPUIntrinsic::SI_sampled:
943 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
944 case AMDGPUIntrinsic::SI_samplel:
945 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
946 case AMDGPUIntrinsic::SI_vs_load_input:
947 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
948 Op.getOperand(1),
949 Op.getOperand(2),
950 Op.getOperand(3));
951 default:
952 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
953 }
954}
955
956SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
957 SelectionDAG &DAG) const {
958 MachineFunction &MF = DAG.getMachineFunction();
959 SDValue Chain = Op.getOperand(0);
960 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
961
962 switch (IntrinsicID) {
963 case AMDGPUIntrinsic::SI_tbuffer_store: {
964 SDLoc DL(Op);
965 SDValue Ops[] = {
966 Chain,
967 Op.getOperand(2),
968 Op.getOperand(3),
969 Op.getOperand(4),
970 Op.getOperand(5),
971 Op.getOperand(6),
972 Op.getOperand(7),
973 Op.getOperand(8),
974 Op.getOperand(9),
975 Op.getOperand(10),
976 Op.getOperand(11),
977 Op.getOperand(12),
978 Op.getOperand(13),
979 Op.getOperand(14)
980 };
981
982 EVT VT = Op.getOperand(3).getValueType();
983
984 MachineMemOperand *MMO = MF.getMachineMemOperand(
985 MachinePointerInfo(),
986 MachineMemOperand::MOStore,
987 VT.getStoreSize(), 4);
988 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
989 Op->getVTList(), Ops, VT, MMO);
990 }
991 default:
992 return SDValue();
993 }
994}
995
Tom Stellard81d871d2013-11-13 23:36:50 +0000996SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
997 SDLoc DL(Op);
998 LoadSDNode *Load = cast<LoadSDNode>(Op);
999
Tom Stellarde812f2f2014-07-21 15:45:06 +00001000 if (Op.getValueType().isVector()) {
1001 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1002 "Custom lowering for non-i32 vectors hasn't been implemented.");
1003 unsigned NumElements = Op.getValueType().getVectorNumElements();
1004 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
1005 switch (Load->getAddressSpace()) {
1006 default: break;
1007 case AMDGPUAS::GLOBAL_ADDRESS:
1008 case AMDGPUAS::PRIVATE_ADDRESS:
1009 // v4 loads are supported for private and global memory.
1010 if (NumElements <= 4)
1011 break;
1012 // fall-through
1013 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenault83e60582014-07-24 17:10:35 +00001014 return ScalarizeVectorLoad(Op, DAG);
Tom Stellarde812f2f2014-07-21 15:45:06 +00001015 }
Tom Stellarde9373602014-01-22 19:24:14 +00001016 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001017
Tom Stellarde812f2f2014-07-21 15:45:06 +00001018 return AMDGPUTargetLowering::LowerLOAD(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001019}
1020
Tom Stellard9fa17912013-08-14 23:24:45 +00001021SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
1022 const SDValue &Op,
1023 SelectionDAG &DAG) const {
1024 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
1025 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +00001026 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +00001027 Op.getOperand(4));
1028}
1029
Tom Stellard0ec134f2014-02-04 17:18:40 +00001030SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1031 if (Op.getValueType() != MVT::i64)
1032 return SDValue();
1033
1034 SDLoc DL(Op);
1035 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001036
1037 SDValue Zero = DAG.getConstant(0, MVT::i32);
1038 SDValue One = DAG.getConstant(1, MVT::i32);
1039
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001040 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1041 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1042
1043 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1044 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001045
1046 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1047
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001048 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1049 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001050
1051 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1052
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001053 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1054 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001055}
1056
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001057// Catch division cases where we can use shortcuts with rcp and rsq
1058// instructions.
1059SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001060 SDLoc SL(Op);
1061 SDValue LHS = Op.getOperand(0);
1062 SDValue RHS = Op.getOperand(1);
1063 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001064 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001065
1066 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001067 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1068 CLHS->isExactlyValue(1.0)) {
1069 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1070 // the CI documentation has a worst case error of 1 ulp.
1071 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1072 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001073
1074 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001075 //
1076 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1077 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001078 if (RHS.getOpcode() == ISD::FSQRT)
1079 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1080
1081 // 1.0 / x -> rcp(x)
1082 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1083 }
1084 }
1085
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001086 if (Unsafe) {
1087 // Turn into multiply by the reciprocal.
1088 // x / y -> x * (1.0 / y)
1089 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1090 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip);
1091 }
1092
1093 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001094}
1095
1096SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001097 SDValue FastLowered = LowerFastFDIV(Op, DAG);
1098 if (FastLowered.getNode())
1099 return FastLowered;
1100
1101 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1102 // selection error for now rather than do something incorrect.
1103 if (Subtarget->hasFP32Denormals())
1104 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001105
1106 SDLoc SL(Op);
1107 SDValue LHS = Op.getOperand(0);
1108 SDValue RHS = Op.getOperand(1);
1109
1110 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1111
1112 const APFloat K0Val(BitsToFloat(0x6f800000));
1113 const SDValue K0 = DAG.getConstantFP(K0Val, MVT::f32);
1114
1115 const APFloat K1Val(BitsToFloat(0x2f800000));
1116 const SDValue K1 = DAG.getConstantFP(K1Val, MVT::f32);
1117
Tom Stellardfb77f002015-01-13 22:59:41 +00001118 const SDValue One = DAG.getConstantFP(1.0, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001119
1120 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f32);
1121
1122 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1123
1124 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1125
1126 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1127
1128 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1129
1130 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1131
1132 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1133}
1134
1135SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
1136 return SDValue();
1137}
1138
1139SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1140 EVT VT = Op.getValueType();
1141
1142 if (VT == MVT::f32)
1143 return LowerFDIV32(Op, DAG);
1144
1145 if (VT == MVT::f64)
1146 return LowerFDIV64(Op, DAG);
1147
1148 llvm_unreachable("Unexpected type for fdiv");
1149}
1150
Tom Stellard81d871d2013-11-13 23:36:50 +00001151SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1152 SDLoc DL(Op);
1153 StoreSDNode *Store = cast<StoreSDNode>(Op);
1154 EVT VT = Store->getMemoryVT();
1155
Tom Stellard9b3816b2014-06-24 23:33:04 +00001156 // These stores are legal.
1157 if (Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
1158 VT.isVector() && VT.getVectorNumElements() == 2 &&
1159 VT.getVectorElementType() == MVT::i32)
1160 return SDValue();
1161
Tom Stellardb02094e2014-07-21 15:45:01 +00001162 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1163 if (VT.isVector() && VT.getVectorNumElements() > 4)
Matt Arsenault83e60582014-07-24 17:10:35 +00001164 return ScalarizeVectorStore(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001165 return SDValue();
1166 }
1167
Tom Stellard81d871d2013-11-13 23:36:50 +00001168 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1169 if (Ret.getNode())
1170 return Ret;
1171
1172 if (VT.isVector() && VT.getVectorNumElements() >= 8)
Matt Arsenault83e60582014-07-24 17:10:35 +00001173 return ScalarizeVectorStore(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001174
Tom Stellard1c8788e2014-03-07 20:12:33 +00001175 if (VT == MVT::i1)
1176 return DAG.getTruncStore(Store->getChain(), DL,
1177 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1178 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1179
Tom Stellarde812f2f2014-07-21 15:45:06 +00001180 return SDValue();
Tom Stellard81d871d2013-11-13 23:36:50 +00001181}
1182
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001183SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
1184 EVT VT = Op.getValueType();
1185 SDValue Arg = Op.getOperand(0);
1186 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
1187 DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
1188 DAG.getConstantFP(0.5 / M_PI, VT)));
1189
1190 switch (Op.getOpcode()) {
1191 case ISD::FCOS:
1192 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1193 case ISD::FSIN:
1194 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1195 default:
1196 llvm_unreachable("Wrong trig opcode");
1197 }
1198}
1199
Tom Stellard75aadc22012-12-11 21:25:42 +00001200//===----------------------------------------------------------------------===//
1201// Custom DAG optimizations
1202//===----------------------------------------------------------------------===//
1203
Matt Arsenault364a6742014-06-11 17:50:44 +00001204SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00001205 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00001206 EVT VT = N->getValueType(0);
1207 EVT ScalarVT = VT.getScalarType();
1208 if (ScalarVT != MVT::f32)
1209 return SDValue();
1210
1211 SelectionDAG &DAG = DCI.DAG;
1212 SDLoc DL(N);
1213
1214 SDValue Src = N->getOperand(0);
1215 EVT SrcVT = Src.getValueType();
1216
1217 // TODO: We could try to match extracting the higher bytes, which would be
1218 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1219 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1220 // about in practice.
1221 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1222 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1223 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1224 DCI.AddToWorklist(Cvt.getNode());
1225 return Cvt;
1226 }
1227 }
1228
1229 // We are primarily trying to catch operations on illegal vector types
1230 // before they are expanded.
1231 // For scalars, we can use the more flexible method of checking masked bits
1232 // after legalization.
1233 if (!DCI.isBeforeLegalize() ||
1234 !SrcVT.isVector() ||
1235 SrcVT.getVectorElementType() != MVT::i8) {
1236 return SDValue();
1237 }
1238
1239 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1240
1241 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1242 // size as 4.
1243 unsigned NElts = SrcVT.getVectorNumElements();
1244 if (!SrcVT.isSimple() && NElts != 3)
1245 return SDValue();
1246
1247 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1248 // prevent a mess from expanding to v4i32 and repacking.
1249 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1250 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1251 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1252 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00001253 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001254
1255 unsigned AS = Load->getAddressSpace();
1256 unsigned Align = Load->getAlignment();
1257 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
1258 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
1259
1260 // Don't try to replace the load if we have to expand it due to alignment
1261 // problems. Otherwise we will end up scalarizing the load, and trying to
1262 // repack into the vector for no real reason.
1263 if (Align < ABIAlignment &&
1264 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
1265 return SDValue();
1266 }
1267
Matt Arsenault364a6742014-06-11 17:50:44 +00001268 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1269 Load->getChain(),
1270 Load->getBasePtr(),
1271 LoadVT,
1272 Load->getMemOperand());
1273
1274 // Make sure successors of the original load stay after it by updating
1275 // them to use the new Chain.
1276 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1277
1278 SmallVector<SDValue, 4> Elts;
1279 if (RegVT.isVector())
1280 DAG.ExtractVectorElements(NewLoad, Elts);
1281 else
1282 Elts.push_back(NewLoad);
1283
1284 SmallVector<SDValue, 4> Ops;
1285
1286 unsigned EltIdx = 0;
1287 for (SDValue Elt : Elts) {
1288 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1289 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1290 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1291 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1292 DCI.AddToWorklist(Cvt.getNode());
1293 Ops.push_back(Cvt);
1294 }
1295
1296 ++EltIdx;
1297 }
1298
1299 assert(Ops.size() == NElts);
1300
1301 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1302 }
1303
1304 return SDValue();
1305}
1306
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001307// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
1308
1309// This is a variant of
1310// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
1311//
1312// The normal DAG combiner will do this, but only if the add has one use since
1313// that would increase the number of instructions.
1314//
1315// This prevents us from seeing a constant offset that can be folded into a
1316// memory instruction's addressing mode. If we know the resulting add offset of
1317// a pointer can be folded into an addressing offset, we can replace the pointer
1318// operand with the add of new constant offset. This eliminates one of the uses,
1319// and may allow the remaining use to also be simplified.
1320//
1321SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
1322 unsigned AddrSpace,
1323 DAGCombinerInfo &DCI) const {
1324 SDValue N0 = N->getOperand(0);
1325 SDValue N1 = N->getOperand(1);
1326
1327 if (N0.getOpcode() != ISD::ADD)
1328 return SDValue();
1329
1330 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
1331 if (!CN1)
1332 return SDValue();
1333
1334 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1335 if (!CAdd)
1336 return SDValue();
1337
1338 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1339 getTargetMachine().getSubtargetImpl()->getInstrInfo());
1340
1341 // If the resulting offset is too large, we can't fold it into the addressing
1342 // mode offset.
1343 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
1344 if (!TII->canFoldOffset(Offset.getZExtValue(), AddrSpace))
1345 return SDValue();
1346
1347 SelectionDAG &DAG = DCI.DAG;
1348 SDLoc SL(N);
1349 EVT VT = N->getValueType(0);
1350
1351 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
1352 SDValue COffset = DAG.getConstant(Offset, MVT::i32);
1353
1354 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
1355}
1356
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001357SDValue SITargetLowering::performAndCombine(SDNode *N,
1358 DAGCombinerInfo &DCI) const {
1359 if (DCI.isBeforeLegalize())
1360 return SDValue();
1361
1362 SelectionDAG &DAG = DCI.DAG;
1363
1364 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
1365 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
1366 SDValue LHS = N->getOperand(0);
1367 SDValue RHS = N->getOperand(1);
1368
1369 if (LHS.getOpcode() == ISD::SETCC &&
1370 RHS.getOpcode() == ISD::SETCC) {
1371 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
1372 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
1373
1374 SDValue X = LHS.getOperand(0);
1375 SDValue Y = RHS.getOperand(0);
1376 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
1377 return SDValue();
1378
1379 if (LCC == ISD::SETO) {
1380 if (X != LHS.getOperand(1))
1381 return SDValue();
1382
1383 if (RCC == ISD::SETUNE) {
1384 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
1385 if (!C1 || !C1->isInfinity() || C1->isNegative())
1386 return SDValue();
1387
1388 const uint32_t Mask = SIInstrFlags::N_NORMAL |
1389 SIInstrFlags::N_SUBNORMAL |
1390 SIInstrFlags::N_ZERO |
1391 SIInstrFlags::P_ZERO |
1392 SIInstrFlags::P_SUBNORMAL |
1393 SIInstrFlags::P_NORMAL;
1394
1395 static_assert(((~(SIInstrFlags::S_NAN |
1396 SIInstrFlags::Q_NAN |
1397 SIInstrFlags::N_INFINITY |
1398 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
1399 "mask not equal");
1400
1401 return DAG.getNode(AMDGPUISD::FP_CLASS, SDLoc(N), MVT::i1,
1402 X, DAG.getConstant(Mask, MVT::i32));
1403 }
1404 }
1405 }
1406
1407 return SDValue();
1408}
1409
Matt Arsenaultf2290332015-01-06 23:00:39 +00001410SDValue SITargetLowering::performOrCombine(SDNode *N,
1411 DAGCombinerInfo &DCI) const {
1412 SelectionDAG &DAG = DCI.DAG;
1413 SDValue LHS = N->getOperand(0);
1414 SDValue RHS = N->getOperand(1);
1415
1416 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
1417 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
1418 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
1419 SDValue Src = LHS.getOperand(0);
1420 if (Src != RHS.getOperand(0))
1421 return SDValue();
1422
1423 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
1424 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
1425 if (!CLHS || !CRHS)
1426 return SDValue();
1427
1428 // Only 10 bits are used.
1429 static const uint32_t MaxMask = 0x3ff;
1430
1431 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
1432 return DAG.getNode(AMDGPUISD::FP_CLASS, SDLoc(N), MVT::i1,
1433 Src, DAG.getConstant(NewMask, MVT::i32));
1434 }
1435
1436 return SDValue();
1437}
1438
1439SDValue SITargetLowering::performClassCombine(SDNode *N,
1440 DAGCombinerInfo &DCI) const {
1441 SelectionDAG &DAG = DCI.DAG;
1442 SDValue Mask = N->getOperand(1);
1443
1444 // fp_class x, 0 -> false
1445 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
1446 if (CMask->isNullValue())
1447 return DAG.getConstant(0, MVT::i1);
1448 }
1449
1450 return SDValue();
1451}
1452
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001453static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
1454 switch (Opc) {
1455 case ISD::FMAXNUM:
1456 return AMDGPUISD::FMAX3;
1457 case AMDGPUISD::SMAX:
1458 return AMDGPUISD::SMAX3;
1459 case AMDGPUISD::UMAX:
1460 return AMDGPUISD::UMAX3;
1461 case ISD::FMINNUM:
1462 return AMDGPUISD::FMIN3;
1463 case AMDGPUISD::SMIN:
1464 return AMDGPUISD::SMIN3;
1465 case AMDGPUISD::UMIN:
1466 return AMDGPUISD::UMIN3;
1467 default:
1468 llvm_unreachable("Not a min/max opcode");
1469 }
1470}
1471
1472SDValue SITargetLowering::performMin3Max3Combine(SDNode *N,
1473 DAGCombinerInfo &DCI) const {
1474 SelectionDAG &DAG = DCI.DAG;
1475
1476 unsigned Opc = N->getOpcode();
1477 SDValue Op0 = N->getOperand(0);
1478 SDValue Op1 = N->getOperand(1);
1479
1480 // Only do this if the inner op has one use since this will just increases
1481 // register pressure for no benefit.
1482
1483 // max(max(a, b), c)
1484 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
1485 SDLoc DL(N);
1486 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1487 DL,
1488 N->getValueType(0),
1489 Op0.getOperand(0),
1490 Op0.getOperand(1),
1491 Op1);
1492 }
1493
1494 // max(a, max(b, c))
1495 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
1496 SDLoc DL(N);
1497 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1498 DL,
1499 N->getValueType(0),
1500 Op0,
1501 Op1.getOperand(0),
1502 Op1.getOperand(1));
1503 }
1504
1505 return SDValue();
1506}
1507
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001508SDValue SITargetLowering::performSetCCCombine(SDNode *N,
1509 DAGCombinerInfo &DCI) const {
1510 SelectionDAG &DAG = DCI.DAG;
1511 SDLoc SL(N);
1512
1513 SDValue LHS = N->getOperand(0);
1514 SDValue RHS = N->getOperand(1);
1515 EVT VT = LHS.getValueType();
1516
1517 if (VT != MVT::f32 && VT != MVT::f64)
1518 return SDValue();
1519
1520 // Match isinf pattern
1521 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
1522 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
1523 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
1524 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
1525 if (!CRHS)
1526 return SDValue();
1527
1528 const APFloat &APF = CRHS->getValueAPF();
1529 if (APF.isInfinity() && !APF.isNegative()) {
1530 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
1531 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1,
1532 LHS.getOperand(0), DAG.getConstant(Mask, MVT::i32));
1533 }
1534 }
1535
1536 return SDValue();
1537}
1538
Tom Stellard75aadc22012-12-11 21:25:42 +00001539SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1540 DAGCombinerInfo &DCI) const {
1541 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001542 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001543
1544 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00001545 default:
1546 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001547 case ISD::SETCC:
1548 return performSetCCCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001549 case ISD::FMAXNUM: // TODO: What about fmax_legacy?
1550 case ISD::FMINNUM:
1551 case AMDGPUISD::SMAX:
1552 case AMDGPUISD::SMIN:
1553 case AMDGPUISD::UMAX:
1554 case AMDGPUISD::UMIN: {
1555 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
1556 getTargetMachine().getOptLevel() > CodeGenOpt::None)
1557 return performMin3Max3Combine(N, DCI);
1558 break;
1559 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001560
1561 case AMDGPUISD::CVT_F32_UBYTE0:
1562 case AMDGPUISD::CVT_F32_UBYTE1:
1563 case AMDGPUISD::CVT_F32_UBYTE2:
1564 case AMDGPUISD::CVT_F32_UBYTE3: {
1565 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
1566
1567 SDValue Src = N->getOperand(0);
1568 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
1569
1570 APInt KnownZero, KnownOne;
1571 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1572 !DCI.isBeforeLegalizeOps());
1573 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1574 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
1575 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
1576 DCI.CommitTargetLoweringOpt(TLO);
1577 }
1578
1579 break;
1580 }
1581
1582 case ISD::UINT_TO_FP: {
1583 return performUCharToFloatCombine(N, DCI);
Matt Arsenault8675db12014-08-29 16:01:14 +00001584
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001585 case ISD::FADD: {
1586 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1587 break;
1588
1589 EVT VT = N->getValueType(0);
1590 if (VT != MVT::f32)
1591 break;
1592
1593 SDValue LHS = N->getOperand(0);
1594 SDValue RHS = N->getOperand(1);
1595
1596 // These should really be instruction patterns, but writing patterns with
1597 // source modiifiers is a pain.
1598
1599 // fadd (fadd (a, a), b) -> mad 2.0, a, b
1600 if (LHS.getOpcode() == ISD::FADD) {
1601 SDValue A = LHS.getOperand(0);
1602 if (A == LHS.getOperand(1)) {
Tom Stellardfb77f002015-01-13 22:59:41 +00001603 const SDValue Two = DAG.getConstantFP(2.0, MVT::f32);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001604 return DAG.getNode(AMDGPUISD::MAD, DL, VT, Two, A, RHS);
1605 }
1606 }
1607
1608 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
1609 if (RHS.getOpcode() == ISD::FADD) {
1610 SDValue A = RHS.getOperand(0);
1611 if (A == RHS.getOperand(1)) {
Tom Stellardfb77f002015-01-13 22:59:41 +00001612 const SDValue Two = DAG.getConstantFP(2.0, MVT::f32);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001613 return DAG.getNode(AMDGPUISD::MAD, DL, VT, Two, A, LHS);
1614 }
1615 }
1616
1617 break;
1618 }
Matt Arsenault8675db12014-08-29 16:01:14 +00001619 case ISD::FSUB: {
1620 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1621 break;
1622
1623 EVT VT = N->getValueType(0);
1624
1625 // Try to get the fneg to fold into the source modifier. This undoes generic
1626 // DAG combines and folds them into the mad.
1627 if (VT == MVT::f32) {
1628 SDValue LHS = N->getOperand(0);
1629 SDValue RHS = N->getOperand(1);
1630
1631 if (LHS.getOpcode() == ISD::FMUL) {
1632 // (fsub (fmul a, b), c) -> mad a, b, (fneg c)
1633
1634 SDValue A = LHS.getOperand(0);
1635 SDValue B = LHS.getOperand(1);
1636 SDValue C = DAG.getNode(ISD::FNEG, DL, VT, RHS);
1637
1638 return DAG.getNode(AMDGPUISD::MAD, DL, VT, A, B, C);
1639 }
1640
1641 if (RHS.getOpcode() == ISD::FMUL) {
1642 // (fsub c, (fmul a, b)) -> mad (fneg a), b, c
1643
1644 SDValue A = DAG.getNode(ISD::FNEG, DL, VT, RHS.getOperand(0));
1645 SDValue B = RHS.getOperand(1);
1646 SDValue C = LHS;
1647
1648 return DAG.getNode(AMDGPUISD::MAD, DL, VT, A, B, C);
1649 }
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001650
1651 if (LHS.getOpcode() == ISD::FADD) {
1652 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
1653
1654 SDValue A = LHS.getOperand(0);
1655 if (A == LHS.getOperand(1)) {
Tom Stellardfb77f002015-01-13 22:59:41 +00001656 const SDValue Two = DAG.getConstantFP(2.0, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001657 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
1658
1659 return DAG.getNode(AMDGPUISD::MAD, DL, VT, Two, A, NegRHS);
1660 }
1661 }
1662
1663 if (RHS.getOpcode() == ISD::FADD) {
1664 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
1665
1666 SDValue A = RHS.getOperand(0);
1667 if (A == RHS.getOperand(1)) {
Tom Stellardfb77f002015-01-13 22:59:41 +00001668 const SDValue NegTwo = DAG.getConstantFP(-2.0, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001669 return DAG.getNode(AMDGPUISD::MAD, DL, VT, NegTwo, A, LHS);
1670 }
1671 }
Matt Arsenault8675db12014-08-29 16:01:14 +00001672 }
1673
1674 break;
1675 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001676 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001677 case ISD::LOAD:
1678 case ISD::STORE:
1679 case ISD::ATOMIC_LOAD:
1680 case ISD::ATOMIC_STORE:
1681 case ISD::ATOMIC_CMP_SWAP:
1682 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
1683 case ISD::ATOMIC_SWAP:
1684 case ISD::ATOMIC_LOAD_ADD:
1685 case ISD::ATOMIC_LOAD_SUB:
1686 case ISD::ATOMIC_LOAD_AND:
1687 case ISD::ATOMIC_LOAD_OR:
1688 case ISD::ATOMIC_LOAD_XOR:
1689 case ISD::ATOMIC_LOAD_NAND:
1690 case ISD::ATOMIC_LOAD_MIN:
1691 case ISD::ATOMIC_LOAD_MAX:
1692 case ISD::ATOMIC_LOAD_UMIN:
1693 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
1694 if (DCI.isBeforeLegalize())
1695 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001696
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001697 MemSDNode *MemNode = cast<MemSDNode>(N);
1698 SDValue Ptr = MemNode->getBasePtr();
1699
1700 // TODO: We could also do this for multiplies.
1701 unsigned AS = MemNode->getAddressSpace();
1702 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
1703 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
1704 if (NewPtr) {
1705 SmallVector<SDValue, 8> NewOps;
Aaron Ballmanf12dc9c2014-08-18 11:51:41 +00001706 for (unsigned I = 0, E = MemNode->getNumOperands(); I != E; ++I)
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001707 NewOps.push_back(MemNode->getOperand(I));
1708
1709 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
1710 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
1711 }
1712 }
1713 break;
1714 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001715 case ISD::AND:
1716 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00001717 case ISD::OR:
1718 return performOrCombine(N, DCI);
1719 case AMDGPUISD::FP_CLASS:
1720 return performClassCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001721 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001722 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001723}
Christian Konigd910b7d2013-02-26 17:52:16 +00001724
Matt Arsenault758659232013-05-18 00:21:46 +00001725/// \brief Test if RegClass is one of the VSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001726static bool isVSrc(unsigned RegClass) {
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001727 switch(RegClass) {
1728 default: return false;
Tom Stellardb6550522015-01-12 19:33:18 +00001729 case AMDGPU::VS_32RegClassID:
1730 case AMDGPU::VS_64RegClassID:
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001731 return true;
1732 }
Christian Konigf82901a2013-02-26 17:52:23 +00001733}
1734
Christian Konigf82901a2013-02-26 17:52:23 +00001735/// \brief Analyze the possible immediate value Op
1736///
1737/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1738/// and the immediate value if it's a literal immediate
1739int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1740
Matt Arsenault303011a2014-12-17 21:04:08 +00001741 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1742 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001743
Tom Stellardedbf1eb2013-04-05 23:31:20 +00001744 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00001745 if (Node->getZExtValue() >> 32)
Tom Stellard7ed0b522014-04-03 20:19:27 +00001746 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001747
Matt Arsenault303011a2014-12-17 21:04:08 +00001748 if (TII->isInlineConstant(Node->getAPIntValue()))
1749 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00001750
Matt Arsenault303011a2014-12-17 21:04:08 +00001751 return Node->getZExtValue();
1752 }
1753
1754 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1755 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
1756 return 0;
1757
1758 if (Node->getValueType(0) == MVT::f32)
1759 return FloatToBits(Node->getValueAPF().convertToFloat());
1760
1761 return -1;
1762 }
1763
1764 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001765}
1766
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001767const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1768 SelectionDAG &DAG, const SDValue &Op) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001769 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1770 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001771 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1772
1773 if (!Op->isMachineOpcode()) {
1774 switch(Op->getOpcode()) {
1775 case ISD::CopyFromReg: {
1776 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1777 unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1778 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1779 return MRI.getRegClass(Reg);
1780 }
1781 return TRI.getPhysRegClass(Reg);
1782 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001783 default: return nullptr;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001784 }
1785 }
1786 const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1787 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1788 if (OpClassID != -1) {
1789 return TRI.getRegClass(OpClassID);
1790 }
1791 switch(Op.getMachineOpcode()) {
1792 case AMDGPU::COPY_TO_REGCLASS:
1793 // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1794 OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1795
1796 // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1797 // class, then the register class for the value could be either a
1798 // VReg or and SReg. In order to get a more accurate
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001799 if (isVSrc(OpClassID))
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001800 return getRegClassForNode(DAG, Op.getOperand(0));
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001801
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001802 return TRI.getRegClass(OpClassID);
1803 case AMDGPU::EXTRACT_SUBREG: {
1804 int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1805 const TargetRegisterClass *SuperClass =
1806 getRegClassForNode(DAG, Op.getOperand(0));
1807 return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1808 }
1809 case AMDGPU::REG_SEQUENCE:
1810 // Operand 0 is the register class id for REG_SEQUENCE instructions.
1811 return TRI.getRegClass(
1812 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1813 default:
1814 return getRegClassFor(Op.getSimpleValueType());
1815 }
1816}
1817
Christian Konigf82901a2013-02-26 17:52:23 +00001818/// \brief Does "Op" fit into register class "RegClass" ?
Tom Stellardb35efba2013-05-20 15:02:01 +00001819bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
Christian Konigf82901a2013-02-26 17:52:23 +00001820 unsigned RegClass) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001821 const TargetRegisterInfo *TRI =
1822 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001823 const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1824 if (!RC) {
Christian Konigf82901a2013-02-26 17:52:23 +00001825 return false;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001826 }
1827 return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
Christian Konigf82901a2013-02-26 17:52:23 +00001828}
1829
Christian Konig8e06e2a2013-04-10 08:39:08 +00001830/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00001831static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00001832 switch (Idx) {
1833 default: return 0;
1834 case AMDGPU::sub0: return 0;
1835 case AMDGPU::sub1: return 1;
1836 case AMDGPU::sub2: return 2;
1837 case AMDGPU::sub3: return 3;
1838 }
1839}
1840
1841/// \brief Adjust the writemask of MIMG instructions
1842void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1843 SelectionDAG &DAG) const {
1844 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00001845 unsigned Lane = 0;
1846 unsigned OldDmask = Node->getConstantOperandVal(0);
1847 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001848
1849 // Try to figure out the used register components
1850 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1851 I != E; ++I) {
1852
1853 // Abort if we can't understand the usage
1854 if (!I->isMachineOpcode() ||
1855 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1856 return;
1857
Tom Stellard54774e52013-10-23 02:53:47 +00001858 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1859 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1860 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1861 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00001862 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001863
Tom Stellard54774e52013-10-23 02:53:47 +00001864 // Set which texture component corresponds to the lane.
1865 unsigned Comp;
1866 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1867 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00001868 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00001869 Dmask &= ~(1 << Comp);
1870 }
1871
Christian Konig8e06e2a2013-04-10 08:39:08 +00001872 // Abort if we have more than one user per component
1873 if (Users[Lane])
1874 return;
1875
1876 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00001877 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001878 }
1879
Tom Stellard54774e52013-10-23 02:53:47 +00001880 // Abort if there's no change
1881 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00001882 return;
1883
1884 // Adjust the writemask in the node
1885 std::vector<SDValue> Ops;
Tom Stellard54774e52013-10-23 02:53:47 +00001886 Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001887 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1888 Ops.push_back(Node->getOperand(i));
Craig Topper8c0b4d02014-04-28 05:57:50 +00001889 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001890
Christian Konig8b1ed282013-04-10 08:39:16 +00001891 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00001892 // (if NewDmask has only one bit set...)
1893 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001894 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00001895 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001896 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00001897 SDValue(Node, 0), RC);
1898 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1899 return;
1900 }
1901
Christian Konig8e06e2a2013-04-10 08:39:08 +00001902 // Update the users of the node with the new indices
1903 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1904
1905 SDNode *User = Users[i];
1906 if (!User)
1907 continue;
1908
1909 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1910 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1911
1912 switch (Idx) {
1913 default: break;
1914 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1915 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1916 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1917 }
1918 }
1919}
1920
Tom Stellard3457a842014-10-09 19:06:00 +00001921/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
1922/// with frame index operands.
1923/// LLVM assumes that inputs are to these instructions are registers.
1924void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
1925 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00001926
1927 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00001928 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
1929 if (!isa<FrameIndexSDNode>(Node->getOperand(i))) {
1930 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00001931 continue;
1932 }
1933
Tom Stellard3457a842014-10-09 19:06:00 +00001934 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00001935 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00001936 Node->getOperand(i).getValueType(),
1937 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00001938 }
1939
Tom Stellard3457a842014-10-09 19:06:00 +00001940 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00001941}
1942
Matt Arsenault08d84942014-06-03 23:06:13 +00001943/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00001944SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1945 SelectionDAG &DAG) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001946 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1947 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Tom Stellard0518ff82013-06-03 17:39:58 +00001948 Node = AdjustRegClass(Node, DAG);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001949
Tom Stellard16a9a202013-08-14 23:24:17 +00001950 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00001951 adjustWritemask(Node, DAG);
1952
Matt Arsenault7d858d82014-11-02 23:46:54 +00001953 if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG ||
1954 Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00001955 legalizeTargetIndependentNode(Node, DAG);
1956 return Node;
1957 }
Tom Stellard654d6692015-01-08 15:08:17 +00001958 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001959}
Christian Konig8b1ed282013-04-10 08:39:16 +00001960
1961/// \brief Assign the register class depending on the number of
1962/// bits set in the writemask
1963void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1964 SDNode *Node) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001965 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1966 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001967
Tom Stellarda99ada52014-11-21 22:31:44 +00001968 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00001969 TII->legalizeOperands(MI);
1970
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001971 if (TII->isMIMG(MI->getOpcode())) {
1972 unsigned VReg = MI->getOperand(0).getReg();
1973 unsigned Writemask = MI->getOperand(1).getImm();
1974 unsigned BitsSet = 0;
1975 for (unsigned i = 0; i < 4; ++i)
1976 BitsSet += Writemask & (1 << i) ? 1 : 0;
1977
1978 const TargetRegisterClass *RC;
1979 switch (BitsSet) {
1980 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001981 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001982 case 2: RC = &AMDGPU::VReg_64RegClass; break;
1983 case 3: RC = &AMDGPU::VReg_96RegClass; break;
1984 }
1985
1986 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1987 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001988 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00001989 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00001990 }
1991
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001992 // Replace unused atomics with the no return version.
1993 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
1994 if (NoRetAtomicOp != -1) {
1995 if (!Node->hasAnyUseOfValue(0)) {
1996 MI->setDesc(TII->get(NoRetAtomicOp));
1997 MI->RemoveOperand(0);
1998 }
1999
2000 return;
2001 }
Christian Konig8b1ed282013-04-10 08:39:16 +00002002}
Tom Stellard0518ff82013-06-03 17:39:58 +00002003
Matt Arsenault485defe2014-11-05 19:01:17 +00002004static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
2005 SDValue K = DAG.getTargetConstant(Val, MVT::i32);
2006 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
2007}
2008
2009MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
2010 SDLoc DL,
2011 SDValue Ptr) const {
Tom Stellard794c8c02014-12-02 17:05:41 +00002012 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
2013 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00002014#if 1
2015 // XXX - Workaround for moveToVALU not handling different register class
2016 // inserts for REG_SEQUENCE.
2017
2018 // Build the half of the subregister with the constants.
2019 const SDValue Ops0[] = {
2020 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, MVT::i32),
2021 buildSMovImm32(DAG, DL, 0),
2022 DAG.getTargetConstant(AMDGPU::sub0, MVT::i32),
Tom Stellard794c8c02014-12-02 17:05:41 +00002023 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002024 DAG.getTargetConstant(AMDGPU::sub1, MVT::i32)
2025 };
2026
2027 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
2028 MVT::v2i32, Ops0), 0);
2029
2030 // Combine the constants and the pointer.
2031 const SDValue Ops1[] = {
2032 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2033 Ptr,
2034 DAG.getTargetConstant(AMDGPU::sub0_sub1, MVT::i32),
2035 SubRegHi,
2036 DAG.getTargetConstant(AMDGPU::sub2_sub3, MVT::i32)
2037 };
2038
2039 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
2040#else
2041 const SDValue Ops[] = {
2042 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2043 Ptr,
2044 DAG.getTargetConstant(AMDGPU::sub0_sub1, MVT::i32),
2045 buildSMovImm32(DAG, DL, 0),
2046 DAG.getTargetConstant(AMDGPU::sub2, MVT::i32),
Tom Stellard794c8c02014-12-02 17:05:41 +00002047 buildSMovImm32(DAG, DL, TII->getDefaultRsrcFormat() >> 32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002048 DAG.getTargetConstant(AMDGPU::sub3, MVT::i32)
2049 };
2050
2051 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2052
2053#endif
2054}
2055
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002056/// \brief Return a resource descriptor with the 'Add TID' bit enabled
2057/// The TID (Thread ID) is multipled by the stride value (bits [61:48]
2058/// of the resource descriptor) to create an offset, which is added to the
2059/// resource ponter.
2060MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
2061 SDLoc DL,
2062 SDValue Ptr,
2063 uint32_t RsrcDword1,
2064 uint64_t RsrcDword2And3) const {
2065 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
2066 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
2067 if (RsrcDword1) {
2068 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
2069 DAG.getConstant(RsrcDword1, MVT::i32)), 0);
2070 }
2071
2072 SDValue DataLo = buildSMovImm32(DAG, DL,
2073 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
2074 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
2075
2076 const SDValue Ops[] = {
2077 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2078 PtrLo,
2079 DAG.getTargetConstant(AMDGPU::sub0, MVT::i32),
2080 PtrHi,
2081 DAG.getTargetConstant(AMDGPU::sub1, MVT::i32),
2082 DataLo,
2083 DAG.getTargetConstant(AMDGPU::sub2, MVT::i32),
2084 DataHi,
2085 DAG.getTargetConstant(AMDGPU::sub3, MVT::i32)
2086 };
2087
2088 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2089}
2090
2091MachineSDNode *SITargetLowering::buildScratchRSRC(SelectionDAG &DAG,
2092 SDLoc DL,
2093 SDValue Ptr) const {
Tom Stellard794c8c02014-12-02 17:05:41 +00002094 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
2095 getTargetMachine().getSubtargetImpl()->getInstrInfo());
2096 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() | AMDGPU::RSRC_TID_ENABLE |
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002097 0xffffffff; // Size
2098
2099 return buildRSRC(DAG, DL, Ptr, 0, Rsrc);
2100}
2101
Tom Stellard0518ff82013-06-03 17:39:58 +00002102MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
2103 SelectionDAG &DAG) const {
2104
2105 SDLoc DL(N);
2106 unsigned NewOpcode = N->getMachineOpcode();
2107
2108 switch (N->getMachineOpcode()) {
2109 default: return N;
Tom Stellard0518ff82013-06-03 17:39:58 +00002110 case AMDGPU::S_LOAD_DWORD_IMM:
2111 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
2112 // Fall-through
2113 case AMDGPU::S_LOAD_DWORDX2_SGPR:
2114 if (NewOpcode == N->getMachineOpcode()) {
2115 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
2116 }
2117 // Fall-through
2118 case AMDGPU::S_LOAD_DWORDX4_IMM:
2119 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
2120 if (NewOpcode == N->getMachineOpcode()) {
2121 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
2122 }
2123 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
2124 return N;
2125 }
2126 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
Matt Arsenault485defe2014-11-05 19:01:17 +00002127
2128 const SDValue Zero64 = DAG.getTargetConstant(0, MVT::i64);
2129 SDValue Ptr(DAG.getMachineNode(AMDGPU::S_MOV_B64, DL, MVT::i64, Zero64), 0);
2130 MachineSDNode *RSrc = wrapAddr64Rsrc(DAG, DL, Ptr);
Matt Arsenault61a528a2014-09-10 23:26:19 +00002131
2132 SmallVector<SDValue, 8> Ops;
2133 Ops.push_back(SDValue(RSrc, 0));
2134 Ops.push_back(N->getOperand(0));
Marek Olsak58f61a82014-12-07 17:17:38 +00002135
2136 // The immediate offset is in dwords on SI and in bytes on VI.
2137 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
Marek Olsak0c056452014-12-10 19:25:31 +00002138 Ops.push_back(DAG.getTargetConstant(Offset->getSExtValue(), MVT::i32));
Marek Olsak58f61a82014-12-07 17:17:38 +00002139 else
Marek Olsak0c056452014-12-10 19:25:31 +00002140 Ops.push_back(DAG.getTargetConstant(Offset->getSExtValue() << 2, MVT::i32));
Matt Arsenault61a528a2014-09-10 23:26:19 +00002141
2142 // Copy remaining operands so we keep any chain and glue nodes that follow
2143 // the normal operands.
2144 for (unsigned I = 2, E = N->getNumOperands(); I != E; ++I)
2145 Ops.push_back(N->getOperand(I));
2146
Tom Stellard0518ff82013-06-03 17:39:58 +00002147 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
2148 }
2149 }
2150}
Tom Stellard94593ee2013-06-03 17:40:18 +00002151
2152SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2153 const TargetRegisterClass *RC,
2154 unsigned Reg, EVT VT) const {
2155 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2156
2157 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2158 cast<RegisterSDNode>(VReg)->getReg(), VT);
2159}