blob: 30b8c4529089620068b08d13acabbeca1800ddc9 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief Custom DAG lowering for SI
12//
13//===----------------------------------------------------------------------===//
14
NAKAMURA Takumi45e0a832014-07-20 11:15:07 +000015#ifdef _MSC_VER
16// Provide M_PI.
17#define _USE_MATH_DEFINES
18#include <cmath>
19#endif
20
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "SIISelLowering.h"
Christian Konig99ee0f42013-03-07 09:04:14 +000022#include "AMDGPU.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000023#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000024#include "AMDGPUSubtarget.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025#include "SIInstrInfo.h"
26#include "SIMachineFunctionInfo.h"
27#include "SIRegisterInfo.h"
Alexey Samsonova253bf92014-08-27 19:36:53 +000028#include "llvm/ADT/BitVector.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000029#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000030#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/MachineRegisterInfo.h"
32#include "llvm/CodeGen/SelectionDAG.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000033#include "llvm/IR/Function.h"
Matt Arsenault364a6742014-06-11 17:50:44 +000034#include "llvm/ADT/SmallString.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000035
36using namespace llvm;
37
Eric Christopher7792e322015-01-30 23:24:40 +000038SITargetLowering::SITargetLowering(TargetMachine &TM,
39 const AMDGPUSubtarget &STI)
40 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000041 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000042 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000043
Christian Konig2214f142013-03-07 09:03:38 +000044 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
45 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
46
Tom Stellard334b29c2014-04-17 21:00:09 +000047 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000048 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000049
Tom Stellard436780b2014-05-15 14:41:57 +000050 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
51 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
52 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000053
Tom Stellard436780b2014-05-15 14:41:57 +000054 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
55 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000056
Tom Stellardf0a21072014-11-18 20:39:39 +000057 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000058 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
59
Tom Stellardf0a21072014-11-18 20:39:39 +000060 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000061 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000062
63 computeRegisterProperties();
64
Christian Konig2989ffc2013-03-18 11:34:16 +000065 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
66 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
67 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
68 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
69
Tom Stellard75aadc22012-12-11 21:25:42 +000070 setOperationAction(ISD::ADD, MVT::i32, Legal);
Matt Arsenaulte8d21462013-11-18 20:09:40 +000071 setOperationAction(ISD::ADDC, MVT::i32, Legal);
72 setOperationAction(ISD::ADDE, MVT::i32, Legal);
Matt Arsenaultb8b51532014-06-23 18:00:38 +000073 setOperationAction(ISD::SUBC, MVT::i32, Legal);
74 setOperationAction(ISD::SUBE, MVT::i32, Legal);
Aaron Watrydaabb202013-06-25 13:55:52 +000075
Matt Arsenaultad14ce82014-07-19 18:44:39 +000076 setOperationAction(ISD::FSIN, MVT::f32, Custom);
77 setOperationAction(ISD::FCOS, MVT::f32, Custom);
78
Matt Arsenault7c936902014-10-21 23:01:01 +000079 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
80 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
81 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
82 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
83
Tom Stellard35bb18c2013-08-26 15:06:04 +000084 // We need to custom lower vector stores from local memory
Tom Stellard35bb18c2013-08-26 15:06:04 +000085 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000086 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
87 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
88
89 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
90 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000091
Tom Stellard1c8788e2014-03-07 20:12:33 +000092 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000093 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
94
Tom Stellard0ec134f2014-02-04 17:18:40 +000095 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +000096 setOperationAction(ISD::SELECT, MVT::f64, Promote);
97 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +000098
Tom Stellard3ca1bfc2014-06-10 16:01:22 +000099 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
100 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
101 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
102 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000103
Tom Stellard83747202013-07-18 21:43:53 +0000104 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
105 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
106
Matt Arsenaulte306a322014-10-21 16:25:08 +0000107 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
108
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000109 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000110 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
111 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
112
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000114 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
115 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
116
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
120
Matt Arsenault94812212014-11-14 18:18:16 +0000121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
123
Tom Stellard94593ee2013-06-03 17:40:18 +0000124 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000125 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
127 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000128
Tom Stellardafcf12f2013-09-12 02:55:14 +0000129 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000130 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000131
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000132 for (MVT VT : MVT::integer_valuetypes()) {
Matt Arsenaultbd223422015-01-14 01:35:17 +0000133 if (VT == MVT::i64)
134 continue;
135
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000136 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000137 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
138 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000139 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000140
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000141 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000142 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
143 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000144 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000145
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000146 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000147 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
148 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000149 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
150 }
151
152 for (MVT VT : MVT::integer_vector_valuetypes()) {
153 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
154 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
155 }
156
157 for (MVT VT : MVT::fp_valuetypes())
158 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000159
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000160 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Matt Arsenault6f243792013-09-05 19:41:10 +0000161 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000162 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
163 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000164
Matt Arsenault470acd82014-04-15 22:28:39 +0000165 setOperationAction(ISD::LOAD, MVT::i1, Custom);
166
Tom Stellardfd155822013-08-26 15:05:36 +0000167 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000168 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000169 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000170
Tom Stellard5f337882014-04-29 23:12:43 +0000171 // These should use UDIVREM, so set them to expand
172 setOperationAction(ISD::UDIV, MVT::i64, Expand);
173 setOperationAction(ISD::UREM, MVT::i64, Expand);
174
Tom Stellard967bf582014-02-13 23:34:15 +0000175 // We only support LOAD/STORE and vector manipulation ops for vectors
176 // with > 4 elements.
177 MVT VecTypes[] = {
Tom Stellardd61a1c32014-02-28 21:36:37 +0000178 MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32
Tom Stellard967bf582014-02-13 23:34:15 +0000179 };
180
Matt Arsenault0d89e842014-07-15 21:44:37 +0000181 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
182 setOperationAction(ISD::SELECT, MVT::i1, Promote);
183
Matt Arsenaultd504a742014-05-15 21:44:05 +0000184 for (MVT VT : VecTypes) {
Tom Stellard967bf582014-02-13 23:34:15 +0000185 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
186 switch(Op) {
187 case ISD::LOAD:
188 case ISD::STORE:
189 case ISD::BUILD_VECTOR:
190 case ISD::BITCAST:
191 case ISD::EXTRACT_VECTOR_ELT:
192 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000193 case ISD::INSERT_SUBVECTOR:
194 case ISD::EXTRACT_SUBVECTOR:
195 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000196 case ISD::CONCAT_VECTORS:
197 setOperationAction(Op, VT, Custom);
198 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000199 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000200 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000201 break;
202 }
203 }
204 }
205
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000206 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
207 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
208 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
209 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000210 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000211 }
212
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000213 setOperationAction(ISD::FDIV, MVT::f32, Custom);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +0000214 setOperationAction(ISD::FDIV, MVT::f64, Custom);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000215
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000216 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000217 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000218 setTargetDAGCombine(ISD::FMINNUM);
219 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000220 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +0000221 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000222 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000223 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000224 setTargetDAGCombine(ISD::UINT_TO_FP);
225
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000226 // All memory operations. Some folding on the pointer operand is done to help
227 // matching the constant offsets in the addressing modes.
228 setTargetDAGCombine(ISD::LOAD);
229 setTargetDAGCombine(ISD::STORE);
230 setTargetDAGCombine(ISD::ATOMIC_LOAD);
231 setTargetDAGCombine(ISD::ATOMIC_STORE);
232 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
233 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
234 setTargetDAGCombine(ISD::ATOMIC_SWAP);
235 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
236 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
237 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
238 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
239 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
240 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
241 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
242 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
243 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
244 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
245
Christian Konigeecebd02013-03-26 14:04:02 +0000246 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000247}
248
Tom Stellard0125f2a2013-06-25 02:39:35 +0000249//===----------------------------------------------------------------------===//
250// TargetLowering queries
251//===----------------------------------------------------------------------===//
252
Matt Arsenaulte306a322014-10-21 16:25:08 +0000253bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
254 EVT) const {
255 // SI has some legal vector types, but no legal vector operations. Say no
256 // shuffles are legal in order to prefer scalarizing some vector operations.
257 return false;
258}
259
Matt Arsenault5015a892014-08-15 17:17:07 +0000260// FIXME: This really needs an address space argument. The immediate offset
261// size is different for different sets of memory instruction sets.
262
263// The single offset DS instructions have a 16-bit unsigned byte offset.
264//
265// MUBUF / MTBUF have a 12-bit unsigned byte offset, and additionally can do r +
266// r + i with addr64. 32-bit has more addressing mode options. Depending on the
267// resource constant, it can also do (i64 r0) + (i32 r1) * (i14 i).
268//
269// SMRD instructions have an 8-bit, dword offset.
270//
271bool SITargetLowering::isLegalAddressingMode(const AddrMode &AM,
272 Type *Ty) const {
273 // No global is ever allowed as a base.
274 if (AM.BaseGV)
275 return false;
276
277 // Allow a 16-bit unsigned immediate field, since this is what DS instructions
278 // use.
279 if (!isUInt<16>(AM.BaseOffs))
280 return false;
281
282 // Only support r+r,
283 switch (AM.Scale) {
284 case 0: // "r+i" or just "i", depending on HasBaseReg.
285 break;
286 case 1:
287 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
288 return false;
289 // Otherwise we have r+r or r+i.
290 break;
291 case 2:
292 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
293 return false;
294 // Allow 2*r as r+r.
295 break;
296 default: // Don't allow n * r
297 return false;
298 }
299
300 return true;
301}
302
Matt Arsenaulte6986632015-01-14 01:35:22 +0000303bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000304 unsigned AddrSpace,
305 unsigned Align,
306 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000307 if (IsFast)
308 *IsFast = false;
309
Matt Arsenault1018c892014-04-24 17:08:26 +0000310 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
311 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000312 if (!VT.isSimple() || VT == MVT::Other)
313 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000314
Tom Stellardc6b299c2015-02-02 18:02:28 +0000315 // TODO - CI+ supports unaligned memory accesses, but this requires driver
316 // support.
Matt Arsenault1018c892014-04-24 17:08:26 +0000317
Matt Arsenault1018c892014-04-24 17:08:26 +0000318 // XXX - The only mention I see of this in the ISA manual is for LDS direct
319 // reads the "byte address and must be dword aligned". Is it also true for the
320 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000321 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
322 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
323 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
324 // with adjacent offsets.
325 return Align % 4 == 0;
326 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000327
Tom Stellard33e64c62015-02-04 20:49:52 +0000328 // Smaller than dword value must be aligned.
329 // FIXME: This should be allowed on CI+
330 if (VT.bitsLT(MVT::i32))
331 return false;
332
Matt Arsenault1018c892014-04-24 17:08:26 +0000333 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
334 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000335 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000336 if (IsFast)
337 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000338
339 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000340}
341
Matt Arsenault46645fa2014-07-28 17:49:26 +0000342EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
343 unsigned SrcAlign, bool IsMemset,
344 bool ZeroMemset,
345 bool MemcpyStrSrc,
346 MachineFunction &MF) const {
347 // FIXME: Should account for address space here.
348
349 // The default fallback uses the private pointer size as a guess for a type to
350 // use. Make sure we switch these to 64-bit accesses.
351
352 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
353 return MVT::v4i32;
354
355 if (Size >= 8 && DstAlign >= 4)
356 return MVT::v2i32;
357
358 // Use the default.
359 return MVT::Other;
360}
361
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000362TargetLoweringBase::LegalizeTypeAction
363SITargetLowering::getPreferredVectorAction(EVT VT) const {
364 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
365 return TypeSplitVector;
366
367 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000368}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000369
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000370bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
371 Type *Ty) const {
Eric Christopher7792e322015-01-30 23:24:40 +0000372 const SIInstrInfo *TII =
373 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000374 return TII->isInlineConstant(Imm);
375}
376
Tom Stellardaf775432013-10-23 00:44:32 +0000377SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000378 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000379 unsigned Offset, bool Signed) const {
Matt Arsenault86033ca2014-07-28 17:31:39 +0000380 const DataLayout *DL = getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000381 MachineFunction &MF = DAG.getMachineFunction();
382 const SIRegisterInfo *TRI =
383 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
384 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000385
Matt Arsenault86033ca2014-07-28 17:31:39 +0000386 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
387
388 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
389 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
390 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000391 MRI.getLiveInVirtReg(InputPtrReg), MVT::i64);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000392 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, BasePtr,
393 DAG.getConstant(Offset, MVT::i64));
394 SDValue PtrOffset = DAG.getUNDEF(getPointerTy(AMDGPUAS::CONSTANT_ADDRESS));
395 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
396
397 return DAG.getLoad(ISD::UNINDEXED, Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD,
398 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
399 false, // isVolatile
400 true, // isNonTemporal
401 true, // isInvariant
402 DL->getABITypeAlignment(Ty)); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000403}
404
Christian Konig2c8f6d52013-03-07 09:03:52 +0000405SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000406 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
407 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
408 SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardec2e43c2014-09-22 15:35:29 +0000409 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000410 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000411
412 MachineFunction &MF = DAG.getMachineFunction();
413 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000414 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000415
416 assert(CallConv == CallingConv::C);
417
418 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000419 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000420
421 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000422 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000423
424 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000425 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Vincent Lejeuned6236442013-10-13 17:56:16 +0000426 !Arg.Flags.isByVal()) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000427
428 assert((PSInputNum <= 15) && "Too many PS inputs!");
429
430 if (!Arg.Used) {
431 // We can savely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000432 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000433 ++PSInputNum;
434 continue;
435 }
436
437 Info->PSInputAddr |= 1 << PSInputNum++;
438 }
439
440 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000441 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000442 ISD::InputArg NewArg = Arg;
443 NewArg.Flags.setSplit();
444 NewArg.VT = Arg.VT.getVectorElementType();
445
446 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
447 // three or five element vertex only needs three or five registers,
448 // NOT four or eigth.
Andrew Trick05938a52015-02-16 18:10:47 +0000449 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000450 unsigned NumElements = ParamType->getVectorNumElements();
451
452 for (unsigned j = 0; j != NumElements; ++j) {
453 Splits.push_back(NewArg);
454 NewArg.PartOffset += NewArg.VT.getStoreSize();
455 }
456
Matt Arsenault762af962014-07-13 03:06:39 +0000457 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000458 Splits.push_back(Arg);
459 }
460 }
461
462 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000463 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
464 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000465
Christian Konig99ee0f42013-03-07 09:04:14 +0000466 // At least one interpolation mode must be enabled or else the GPU will hang.
Matt Arsenault762af962014-07-13 03:06:39 +0000467 if (Info->getShaderType() == ShaderType::PIXEL &&
468 (Info->PSInputAddr & 0x7F) == 0) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000469 Info->PSInputAddr |= 1;
470 CCInfo.AllocateReg(AMDGPU::VGPR0);
471 CCInfo.AllocateReg(AMDGPU::VGPR1);
472 }
473
Tom Stellarded882c22013-06-03 17:40:11 +0000474 // The pointer to the list of arguments is stored in SGPR0, SGPR1
Tom Stellardb02094e2014-07-21 15:45:01 +0000475 // The pointer to the scratch buffer is stored in SGPR2, SGPR3
Matt Arsenault762af962014-07-13 03:06:39 +0000476 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardfeab91c2014-12-02 17:41:43 +0000477 if (Subtarget->isAmdHsaOS())
478 Info->NumUserSGPRs = 2; // FIXME: Need to support scratch buffers.
479 else
480 Info->NumUserSGPRs = 4;
Tom Stellardec2e43c2014-09-22 15:35:29 +0000481
482 unsigned InputPtrReg =
483 TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
484 unsigned InputPtrRegLo =
485 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 0);
486 unsigned InputPtrRegHi =
487 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 1);
488
489 unsigned ScratchPtrReg =
490 TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_PTR);
491 unsigned ScratchPtrRegLo =
492 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 0);
493 unsigned ScratchPtrRegHi =
494 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 1);
495
496 CCInfo.AllocateReg(InputPtrRegLo);
497 CCInfo.AllocateReg(InputPtrRegHi);
498 CCInfo.AllocateReg(ScratchPtrRegLo);
499 CCInfo.AllocateReg(ScratchPtrRegHi);
500 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
501 MF.addLiveIn(ScratchPtrReg, &AMDGPU::SReg_64RegClass);
Tom Stellarded882c22013-06-03 17:40:11 +0000502 }
503
Matt Arsenault762af962014-07-13 03:06:39 +0000504 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000505 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
506 Splits);
507 }
508
Christian Konig2c8f6d52013-03-07 09:03:52 +0000509 AnalyzeFormalArguments(CCInfo, Splits);
510
511 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
512
Christian Konigb7be72d2013-05-17 09:46:48 +0000513 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000514 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000515 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000516 continue;
517 }
518
Christian Konig2c8f6d52013-03-07 09:03:52 +0000519 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000520 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000521
522 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000523 VT = Ins[i].VT;
524 EVT MemVT = Splits[i].VT;
Jan Veselye5121f32014-10-14 20:05:26 +0000525 const unsigned Offset = 36 + VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000526 // The first 36 bytes of the input buffer contains information about
527 // thread group and global sizes.
Tom Stellardaf775432013-10-23 00:44:32 +0000528 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
Jan Veselye5121f32014-10-14 20:05:26 +0000529 Offset, Ins[i].Flags.isSExt());
Tom Stellardca7ecf32014-08-22 18:49:31 +0000530
531 const PointerType *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000532 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000533 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
534 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
535 // On SI local pointers are just offsets into LDS, so they are always
536 // less than 16-bits. On CI and newer they could potentially be
537 // real pointers, so we can't guarantee their size.
538 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
539 DAG.getValueType(MVT::i16));
540 }
541
Tom Stellarded882c22013-06-03 17:40:11 +0000542 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000543 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000544 continue;
545 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000546 assert(VA.isRegLoc() && "Parameter must be in a register!");
547
548 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000549
550 if (VT == MVT::i64) {
551 // For now assume it is a pointer
552 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
553 &AMDGPU::SReg_64RegClass);
554 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
555 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
556 continue;
557 }
558
559 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
560
561 Reg = MF.addLiveIn(Reg, RC);
562 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
563
Christian Konig2c8f6d52013-03-07 09:03:52 +0000564 if (Arg.VT.isVector()) {
565
566 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000567 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000568 unsigned NumElements = ParamType->getVectorNumElements();
569
570 SmallVector<SDValue, 4> Regs;
571 Regs.push_back(Val);
572 for (unsigned j = 1; j != NumElements; ++j) {
573 Reg = ArgLocs[ArgIdx++].getLocReg();
574 Reg = MF.addLiveIn(Reg, RC);
575 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
576 }
577
578 // Fill up the missing vector elements
579 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000580 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000581
Craig Topper48d114b2014-04-26 18:35:24 +0000582 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000583 continue;
584 }
585
586 InVals.push_back(Val);
587 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000588
589 if (Info->getShaderType() != ShaderType::COMPUTE) {
590 unsigned ScratchIdx = CCInfo.getFirstUnallocated(
591 AMDGPU::SGPR_32RegClass.begin(), AMDGPU::SGPR_32RegClass.getNumRegs());
592 Info->ScratchOffsetReg = AMDGPU::SGPR_32RegClass.getRegister(ScratchIdx);
593 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000594 return Chain;
595}
596
Tom Stellard75aadc22012-12-11 21:25:42 +0000597MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
598 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000599
Tom Stellard556d9aa2013-06-03 17:39:37 +0000600 MachineBasicBlock::iterator I = *MI;
Eric Christopher7792e322015-01-30 23:24:40 +0000601 const SIInstrInfo *TII =
602 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellard556d9aa2013-06-03 17:39:37 +0000603
Tom Stellard75aadc22012-12-11 21:25:42 +0000604 switch (MI->getOpcode()) {
605 default:
606 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Matt Arsenault20711b72015-02-20 22:10:45 +0000607 case AMDGPU::BRANCH:
608 return BB;
Tom Stellard81d871d2013-11-13 23:36:50 +0000609 case AMDGPU::SI_RegisterStorePseudo: {
610 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard81d871d2013-11-13 23:36:50 +0000611 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
612 MachineInstrBuilder MIB =
613 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
614 Reg);
615 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
616 MIB.addOperand(MI->getOperand(i));
617
618 MI->eraseFromParent();
Vincent Lejeune79a58342014-05-10 19:18:25 +0000619 break;
620 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000621 }
622 return BB;
623}
624
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000625bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
626 // This currently forces unfolding various combinations of fsub into fma with
627 // free fneg'd operands. As long as we have fast FMA (controlled by
628 // isFMAFasterThanFMulAndFAdd), we should perform these.
629
630 // When fma is quarter rate, for f64 where add / sub are at best half rate,
631 // most of these combines appear to be cycle neutral but save on instruction
632 // count / code size.
633 return true;
634}
635
Matt Arsenault8596f712014-11-28 22:51:38 +0000636EVT SITargetLowering::getSetCCResultType(LLVMContext &Ctx, EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +0000637 if (!VT.isVector()) {
638 return MVT::i1;
639 }
Matt Arsenault8596f712014-11-28 22:51:38 +0000640 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +0000641}
642
Christian Konig082a14a2013-03-18 11:34:05 +0000643MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
644 return MVT::i32;
645}
646
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000647// Answering this is somewhat tricky and depends on the specific device which
648// have different rates for fma or all f64 operations.
649//
650// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
651// regardless of which device (although the number of cycles differs between
652// devices), so it is always profitable for f64.
653//
654// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
655// only on full rate devices. Normally, we should prefer selecting v_mad_f32
656// which we can always do even without fused FP ops since it returns the same
657// result as the separate operations and since it is always full
658// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
659// however does not support denormals, so we do report fma as faster if we have
660// a fast fma device and require denormals.
661//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000662bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
663 VT = VT.getScalarType();
664
665 if (!VT.isSimple())
666 return false;
667
668 switch (VT.getSimpleVT().SimpleTy) {
669 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000670 // This is as fast on some subtargets. However, we always have full rate f32
671 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +0000672 // which we should prefer over fma. We can't use this if we want to support
673 // denormals, so only report this in these cases.
674 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000675 case MVT::f64:
676 return true;
677 default:
678 break;
679 }
680
681 return false;
682}
683
Tom Stellard75aadc22012-12-11 21:25:42 +0000684//===----------------------------------------------------------------------===//
685// Custom DAG Lowering Operations
686//===----------------------------------------------------------------------===//
687
688SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
689 switch (Op.getOpcode()) {
690 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +0000691 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000692 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000693 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +0000694 SDValue Result = LowerLOAD(Op, DAG);
695 assert((!Result.getNode() ||
696 Result.getNode()->getNumValues() == 2) &&
697 "Load should return a value and a chain");
698 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +0000699 }
Tom Stellardaf775432013-10-23 00:44:32 +0000700
Matt Arsenaultad14ce82014-07-19 18:44:39 +0000701 case ISD::FSIN:
702 case ISD::FCOS:
703 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000704 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000705 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000706 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000707 case ISD::GlobalAddress: {
708 MachineFunction &MF = DAG.getMachineFunction();
709 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
710 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +0000711 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000712 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
713 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000714 }
715 return SDValue();
716}
717
Tom Stellardf8794352012-12-19 22:10:31 +0000718/// \brief Helper function for LowerBRCOND
719static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000720
Tom Stellardf8794352012-12-19 22:10:31 +0000721 SDNode *Parent = Value.getNode();
722 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
723 I != E; ++I) {
724
725 if (I.getUse().get() != Value)
726 continue;
727
728 if (I->getOpcode() == Opcode)
729 return *I;
730 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000731 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000732}
733
Tom Stellardb02094e2014-07-21 15:45:01 +0000734SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
735
Tom Stellardb02094e2014-07-21 15:45:01 +0000736 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
737 unsigned FrameIndex = FINode->getIndex();
738
Tom Stellardb02094e2014-07-21 15:45:01 +0000739 return DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
740}
741
Tom Stellardf8794352012-12-19 22:10:31 +0000742/// This transforms the control flow intrinsics to get the branch destination as
743/// last parameter, also switches branch target with BR if the need arise
744SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
745 SelectionDAG &DAG) const {
746
Andrew Trickef9de2a2013-05-25 02:42:55 +0000747 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000748
749 SDNode *Intr = BRCOND.getOperand(1).getNode();
750 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +0000751 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000752
753 if (Intr->getOpcode() == ISD::SETCC) {
754 // As long as we negate the condition everything is fine
755 SDNode *SetCC = Intr;
756 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000757 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
758 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000759 Intr = SetCC->getOperand(0).getNode();
760
761 } else {
762 // Get the target from BR if we don't negate the condition
763 BR = findUser(BRCOND, ISD::BR);
764 Target = BR->getOperand(1);
765 }
766
767 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
768
769 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000770 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +0000771
772 // operands of the new intrinsic call
773 SmallVector<SDValue, 4> Ops;
774 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000775 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +0000776 Ops.push_back(Target);
777
778 // build the new intrinsic call
779 SDNode *Result = DAG.getNode(
780 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +0000781 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000782
783 if (BR) {
784 // Give the branch instruction our target
785 SDValue Ops[] = {
786 BR->getOperand(0),
787 BRCOND.getOperand(2)
788 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000789 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
790 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
791 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000792 }
793
794 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
795
796 // Copy the intrinsic results to registers
797 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
798 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
799 if (!CopyToReg)
800 continue;
801
802 Chain = DAG.getCopyToReg(
803 Chain, DL,
804 CopyToReg->getOperand(1),
805 SDValue(Result, i - 1),
806 SDValue());
807
808 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
809 }
810
811 // Remove the old intrinsic from the chain
812 DAG.ReplaceAllUsesOfValueWith(
813 SDValue(Intr, Intr->getNumValues() - 1),
814 Intr->getOperand(0));
815
816 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000817}
818
Tom Stellard067c8152014-07-21 14:01:14 +0000819SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
820 SDValue Op,
821 SelectionDAG &DAG) const {
822 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
823
824 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
825 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
826
827 SDLoc DL(GSD);
828 const GlobalValue *GV = GSD->getGlobal();
829 MVT PtrVT = getPointerTy(GSD->getAddressSpace());
830
831 SDValue Ptr = DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT);
832 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
833
834 SDValue PtrLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
835 DAG.getConstant(0, MVT::i32));
836 SDValue PtrHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
837 DAG.getConstant(1, MVT::i32));
838
839 SDValue Lo = DAG.getNode(ISD::ADDC, DL, DAG.getVTList(MVT::i32, MVT::Glue),
840 PtrLo, GA);
841 SDValue Hi = DAG.getNode(ISD::ADDE, DL, DAG.getVTList(MVT::i32, MVT::Glue),
842 PtrHi, DAG.getConstant(0, MVT::i32),
843 SDValue(Lo.getNode(), 1));
844 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
845}
846
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000847SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
848 SelectionDAG &DAG) const {
849 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000850 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000851 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000852
853 EVT VT = Op.getValueType();
854 SDLoc DL(Op);
855 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
856
857 switch (IntrinsicID) {
858 case Intrinsic::r600_read_ngroups_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000859 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
860 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000861 case Intrinsic::r600_read_ngroups_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000862 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
863 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000864 case Intrinsic::r600_read_ngroups_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000865 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
866 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000867 case Intrinsic::r600_read_global_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000868 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
869 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000870 case Intrinsic::r600_read_global_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000871 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
872 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000873 case Intrinsic::r600_read_global_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000874 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
875 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000876 case Intrinsic::r600_read_local_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000877 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
878 SI::KernelInputOffsets::LOCAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000879 case Intrinsic::r600_read_local_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000880 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
881 SI::KernelInputOffsets::LOCAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000882 case Intrinsic::r600_read_local_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000883 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
884 SI::KernelInputOffsets::LOCAL_SIZE_Z, false);
Jan Veselye5121f32014-10-14 20:05:26 +0000885
886 case Intrinsic::AMDGPU_read_workdim:
887 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
888 MF.getInfo<SIMachineFunctionInfo>()->ABIArgOffset,
889 false);
890
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000891 case Intrinsic::r600_read_tgid_x:
892 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000893 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000894 case Intrinsic::r600_read_tgid_y:
895 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000896 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000897 case Intrinsic::r600_read_tgid_z:
898 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000899 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000900 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000901 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000902 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000903 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000904 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000905 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000906 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000907 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000908 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000909 case AMDGPUIntrinsic::SI_load_const: {
910 SDValue Ops[] = {
911 Op.getOperand(1),
912 Op.getOperand(2)
913 };
914
915 MachineMemOperand *MMO = MF.getMachineMemOperand(
916 MachinePointerInfo(),
917 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
918 VT.getStoreSize(), 4);
919 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
920 Op->getVTList(), Ops, VT, MMO);
921 }
922 case AMDGPUIntrinsic::SI_sample:
923 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
924 case AMDGPUIntrinsic::SI_sampleb:
925 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
926 case AMDGPUIntrinsic::SI_sampled:
927 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
928 case AMDGPUIntrinsic::SI_samplel:
929 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
930 case AMDGPUIntrinsic::SI_vs_load_input:
931 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
932 Op.getOperand(1),
933 Op.getOperand(2),
934 Op.getOperand(3));
935 default:
936 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
937 }
938}
939
940SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
941 SelectionDAG &DAG) const {
942 MachineFunction &MF = DAG.getMachineFunction();
943 SDValue Chain = Op.getOperand(0);
944 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
945
946 switch (IntrinsicID) {
947 case AMDGPUIntrinsic::SI_tbuffer_store: {
948 SDLoc DL(Op);
949 SDValue Ops[] = {
950 Chain,
951 Op.getOperand(2),
952 Op.getOperand(3),
953 Op.getOperand(4),
954 Op.getOperand(5),
955 Op.getOperand(6),
956 Op.getOperand(7),
957 Op.getOperand(8),
958 Op.getOperand(9),
959 Op.getOperand(10),
960 Op.getOperand(11),
961 Op.getOperand(12),
962 Op.getOperand(13),
963 Op.getOperand(14)
964 };
965
966 EVT VT = Op.getOperand(3).getValueType();
967
968 MachineMemOperand *MMO = MF.getMachineMemOperand(
969 MachinePointerInfo(),
970 MachineMemOperand::MOStore,
971 VT.getStoreSize(), 4);
972 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
973 Op->getVTList(), Ops, VT, MMO);
974 }
975 default:
976 return SDValue();
977 }
978}
979
Tom Stellard81d871d2013-11-13 23:36:50 +0000980SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
981 SDLoc DL(Op);
982 LoadSDNode *Load = cast<LoadSDNode>(Op);
983
Tom Stellarde812f2f2014-07-21 15:45:06 +0000984 if (Op.getValueType().isVector()) {
985 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
986 "Custom lowering for non-i32 vectors hasn't been implemented.");
987 unsigned NumElements = Op.getValueType().getVectorNumElements();
988 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
989 switch (Load->getAddressSpace()) {
990 default: break;
991 case AMDGPUAS::GLOBAL_ADDRESS:
992 case AMDGPUAS::PRIVATE_ADDRESS:
993 // v4 loads are supported for private and global memory.
994 if (NumElements <= 4)
995 break;
996 // fall-through
997 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenault83e60582014-07-24 17:10:35 +0000998 return ScalarizeVectorLoad(Op, DAG);
Tom Stellarde812f2f2014-07-21 15:45:06 +0000999 }
Tom Stellarde9373602014-01-22 19:24:14 +00001000 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001001
Tom Stellarde812f2f2014-07-21 15:45:06 +00001002 return AMDGPUTargetLowering::LowerLOAD(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001003}
1004
Tom Stellard9fa17912013-08-14 23:24:45 +00001005SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
1006 const SDValue &Op,
1007 SelectionDAG &DAG) const {
1008 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
1009 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +00001010 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +00001011 Op.getOperand(4));
1012}
1013
Tom Stellard0ec134f2014-02-04 17:18:40 +00001014SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1015 if (Op.getValueType() != MVT::i64)
1016 return SDValue();
1017
1018 SDLoc DL(Op);
1019 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001020
1021 SDValue Zero = DAG.getConstant(0, MVT::i32);
1022 SDValue One = DAG.getConstant(1, MVT::i32);
1023
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001024 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1025 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1026
1027 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1028 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001029
1030 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1031
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001032 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1033 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001034
1035 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1036
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001037 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1038 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001039}
1040
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001041// Catch division cases where we can use shortcuts with rcp and rsq
1042// instructions.
1043SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001044 SDLoc SL(Op);
1045 SDValue LHS = Op.getOperand(0);
1046 SDValue RHS = Op.getOperand(1);
1047 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001048 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001049
1050 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001051 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1052 CLHS->isExactlyValue(1.0)) {
1053 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1054 // the CI documentation has a worst case error of 1 ulp.
1055 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1056 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001057
1058 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001059 //
1060 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1061 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001062 if (RHS.getOpcode() == ISD::FSQRT)
1063 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1064
1065 // 1.0 / x -> rcp(x)
1066 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1067 }
1068 }
1069
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001070 if (Unsafe) {
1071 // Turn into multiply by the reciprocal.
1072 // x / y -> x * (1.0 / y)
1073 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1074 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip);
1075 }
1076
1077 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001078}
1079
1080SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001081 SDValue FastLowered = LowerFastFDIV(Op, DAG);
1082 if (FastLowered.getNode())
1083 return FastLowered;
1084
1085 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1086 // selection error for now rather than do something incorrect.
1087 if (Subtarget->hasFP32Denormals())
1088 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001089
1090 SDLoc SL(Op);
1091 SDValue LHS = Op.getOperand(0);
1092 SDValue RHS = Op.getOperand(1);
1093
1094 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1095
1096 const APFloat K0Val(BitsToFloat(0x6f800000));
1097 const SDValue K0 = DAG.getConstantFP(K0Val, MVT::f32);
1098
1099 const APFloat K1Val(BitsToFloat(0x2f800000));
1100 const SDValue K1 = DAG.getConstantFP(K1Val, MVT::f32);
1101
Tom Stellardfb77f002015-01-13 22:59:41 +00001102 const SDValue One = DAG.getConstantFP(1.0, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001103
1104 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f32);
1105
1106 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1107
1108 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1109
1110 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1111
1112 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1113
1114 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1115
1116 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1117}
1118
1119SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001120 if (DAG.getTarget().Options.UnsafeFPMath)
1121 return LowerFastFDIV(Op, DAG);
1122
1123 SDLoc SL(Op);
1124 SDValue X = Op.getOperand(0);
1125 SDValue Y = Op.getOperand(1);
1126
1127 const SDValue One = DAG.getConstantFP(1.0, MVT::f64);
1128
1129 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
1130
1131 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
1132
1133 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
1134
1135 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
1136
1137 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
1138
1139 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
1140
1141 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
1142
1143 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
1144
1145 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
1146 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
1147
1148 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
1149 NegDivScale0, Mul, DivScale1);
1150
1151 SDValue Scale;
1152
1153 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1154 // Workaround a hardware bug on SI where the condition output from div_scale
1155 // is not usable.
1156
1157 const SDValue Hi = DAG.getConstant(1, MVT::i32);
1158
1159 // Figure out if the scale to use for div_fmas.
1160 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1161 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
1162 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
1163 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
1164
1165 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
1166 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
1167
1168 SDValue Scale0Hi
1169 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
1170 SDValue Scale1Hi
1171 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
1172
1173 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
1174 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
1175 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
1176 } else {
1177 Scale = DivScale1.getValue(1);
1178 }
1179
1180 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
1181 Fma4, Fma3, Mul, Scale);
1182
1183 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001184}
1185
1186SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1187 EVT VT = Op.getValueType();
1188
1189 if (VT == MVT::f32)
1190 return LowerFDIV32(Op, DAG);
1191
1192 if (VT == MVT::f64)
1193 return LowerFDIV64(Op, DAG);
1194
1195 llvm_unreachable("Unexpected type for fdiv");
1196}
1197
Tom Stellard81d871d2013-11-13 23:36:50 +00001198SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1199 SDLoc DL(Op);
1200 StoreSDNode *Store = cast<StoreSDNode>(Op);
1201 EVT VT = Store->getMemoryVT();
1202
Tom Stellard9b3816b2014-06-24 23:33:04 +00001203 // These stores are legal.
Tom Stellardb02094e2014-07-21 15:45:01 +00001204 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1205 if (VT.isVector() && VT.getVectorNumElements() > 4)
Matt Arsenault83e60582014-07-24 17:10:35 +00001206 return ScalarizeVectorStore(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001207 return SDValue();
1208 }
1209
Tom Stellard81d871d2013-11-13 23:36:50 +00001210 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1211 if (Ret.getNode())
1212 return Ret;
1213
1214 if (VT.isVector() && VT.getVectorNumElements() >= 8)
Matt Arsenault83e60582014-07-24 17:10:35 +00001215 return ScalarizeVectorStore(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001216
Tom Stellard1c8788e2014-03-07 20:12:33 +00001217 if (VT == MVT::i1)
1218 return DAG.getTruncStore(Store->getChain(), DL,
1219 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1220 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1221
Tom Stellarde812f2f2014-07-21 15:45:06 +00001222 return SDValue();
Tom Stellard81d871d2013-11-13 23:36:50 +00001223}
1224
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001225SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
1226 EVT VT = Op.getValueType();
1227 SDValue Arg = Op.getOperand(0);
1228 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
1229 DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
1230 DAG.getConstantFP(0.5 / M_PI, VT)));
1231
1232 switch (Op.getOpcode()) {
1233 case ISD::FCOS:
1234 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1235 case ISD::FSIN:
1236 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1237 default:
1238 llvm_unreachable("Wrong trig opcode");
1239 }
1240}
1241
Tom Stellard75aadc22012-12-11 21:25:42 +00001242//===----------------------------------------------------------------------===//
1243// Custom DAG optimizations
1244//===----------------------------------------------------------------------===//
1245
Matt Arsenault364a6742014-06-11 17:50:44 +00001246SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00001247 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00001248 EVT VT = N->getValueType(0);
1249 EVT ScalarVT = VT.getScalarType();
1250 if (ScalarVT != MVT::f32)
1251 return SDValue();
1252
1253 SelectionDAG &DAG = DCI.DAG;
1254 SDLoc DL(N);
1255
1256 SDValue Src = N->getOperand(0);
1257 EVT SrcVT = Src.getValueType();
1258
1259 // TODO: We could try to match extracting the higher bytes, which would be
1260 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1261 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1262 // about in practice.
1263 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1264 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1265 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1266 DCI.AddToWorklist(Cvt.getNode());
1267 return Cvt;
1268 }
1269 }
1270
1271 // We are primarily trying to catch operations on illegal vector types
1272 // before they are expanded.
1273 // For scalars, we can use the more flexible method of checking masked bits
1274 // after legalization.
1275 if (!DCI.isBeforeLegalize() ||
1276 !SrcVT.isVector() ||
1277 SrcVT.getVectorElementType() != MVT::i8) {
1278 return SDValue();
1279 }
1280
1281 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1282
1283 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1284 // size as 4.
1285 unsigned NElts = SrcVT.getVectorNumElements();
1286 if (!SrcVT.isSimple() && NElts != 3)
1287 return SDValue();
1288
1289 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1290 // prevent a mess from expanding to v4i32 and repacking.
1291 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1292 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1293 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1294 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00001295 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001296
1297 unsigned AS = Load->getAddressSpace();
1298 unsigned Align = Load->getAlignment();
1299 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
1300 unsigned ABIAlignment = getDataLayout()->getABITypeAlignment(Ty);
1301
1302 // Don't try to replace the load if we have to expand it due to alignment
1303 // problems. Otherwise we will end up scalarizing the load, and trying to
1304 // repack into the vector for no real reason.
1305 if (Align < ABIAlignment &&
1306 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
1307 return SDValue();
1308 }
1309
Matt Arsenault364a6742014-06-11 17:50:44 +00001310 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1311 Load->getChain(),
1312 Load->getBasePtr(),
1313 LoadVT,
1314 Load->getMemOperand());
1315
1316 // Make sure successors of the original load stay after it by updating
1317 // them to use the new Chain.
1318 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1319
1320 SmallVector<SDValue, 4> Elts;
1321 if (RegVT.isVector())
1322 DAG.ExtractVectorElements(NewLoad, Elts);
1323 else
1324 Elts.push_back(NewLoad);
1325
1326 SmallVector<SDValue, 4> Ops;
1327
1328 unsigned EltIdx = 0;
1329 for (SDValue Elt : Elts) {
1330 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1331 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1332 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1333 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1334 DCI.AddToWorklist(Cvt.getNode());
1335 Ops.push_back(Cvt);
1336 }
1337
1338 ++EltIdx;
1339 }
1340
1341 assert(Ops.size() == NElts);
1342
1343 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1344 }
1345
1346 return SDValue();
1347}
1348
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001349// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
1350
1351// This is a variant of
1352// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
1353//
1354// The normal DAG combiner will do this, but only if the add has one use since
1355// that would increase the number of instructions.
1356//
1357// This prevents us from seeing a constant offset that can be folded into a
1358// memory instruction's addressing mode. If we know the resulting add offset of
1359// a pointer can be folded into an addressing offset, we can replace the pointer
1360// operand with the add of new constant offset. This eliminates one of the uses,
1361// and may allow the remaining use to also be simplified.
1362//
1363SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
1364 unsigned AddrSpace,
1365 DAGCombinerInfo &DCI) const {
1366 SDValue N0 = N->getOperand(0);
1367 SDValue N1 = N->getOperand(1);
1368
1369 if (N0.getOpcode() != ISD::ADD)
1370 return SDValue();
1371
1372 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
1373 if (!CN1)
1374 return SDValue();
1375
1376 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1377 if (!CAdd)
1378 return SDValue();
1379
Eric Christopher7792e322015-01-30 23:24:40 +00001380 const SIInstrInfo *TII =
1381 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001382
1383 // If the resulting offset is too large, we can't fold it into the addressing
1384 // mode offset.
1385 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
1386 if (!TII->canFoldOffset(Offset.getZExtValue(), AddrSpace))
1387 return SDValue();
1388
1389 SelectionDAG &DAG = DCI.DAG;
1390 SDLoc SL(N);
1391 EVT VT = N->getValueType(0);
1392
1393 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
1394 SDValue COffset = DAG.getConstant(Offset, MVT::i32);
1395
1396 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
1397}
1398
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001399SDValue SITargetLowering::performAndCombine(SDNode *N,
1400 DAGCombinerInfo &DCI) const {
1401 if (DCI.isBeforeLegalize())
1402 return SDValue();
1403
1404 SelectionDAG &DAG = DCI.DAG;
1405
1406 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
1407 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
1408 SDValue LHS = N->getOperand(0);
1409 SDValue RHS = N->getOperand(1);
1410
1411 if (LHS.getOpcode() == ISD::SETCC &&
1412 RHS.getOpcode() == ISD::SETCC) {
1413 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
1414 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
1415
1416 SDValue X = LHS.getOperand(0);
1417 SDValue Y = RHS.getOperand(0);
1418 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
1419 return SDValue();
1420
1421 if (LCC == ISD::SETO) {
1422 if (X != LHS.getOperand(1))
1423 return SDValue();
1424
1425 if (RCC == ISD::SETUNE) {
1426 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
1427 if (!C1 || !C1->isInfinity() || C1->isNegative())
1428 return SDValue();
1429
1430 const uint32_t Mask = SIInstrFlags::N_NORMAL |
1431 SIInstrFlags::N_SUBNORMAL |
1432 SIInstrFlags::N_ZERO |
1433 SIInstrFlags::P_ZERO |
1434 SIInstrFlags::P_SUBNORMAL |
1435 SIInstrFlags::P_NORMAL;
1436
1437 static_assert(((~(SIInstrFlags::S_NAN |
1438 SIInstrFlags::Q_NAN |
1439 SIInstrFlags::N_INFINITY |
1440 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
1441 "mask not equal");
1442
1443 return DAG.getNode(AMDGPUISD::FP_CLASS, SDLoc(N), MVT::i1,
1444 X, DAG.getConstant(Mask, MVT::i32));
1445 }
1446 }
1447 }
1448
1449 return SDValue();
1450}
1451
Matt Arsenaultf2290332015-01-06 23:00:39 +00001452SDValue SITargetLowering::performOrCombine(SDNode *N,
1453 DAGCombinerInfo &DCI) const {
1454 SelectionDAG &DAG = DCI.DAG;
1455 SDValue LHS = N->getOperand(0);
1456 SDValue RHS = N->getOperand(1);
1457
1458 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
1459 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
1460 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
1461 SDValue Src = LHS.getOperand(0);
1462 if (Src != RHS.getOperand(0))
1463 return SDValue();
1464
1465 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
1466 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
1467 if (!CLHS || !CRHS)
1468 return SDValue();
1469
1470 // Only 10 bits are used.
1471 static const uint32_t MaxMask = 0x3ff;
1472
1473 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
1474 return DAG.getNode(AMDGPUISD::FP_CLASS, SDLoc(N), MVT::i1,
1475 Src, DAG.getConstant(NewMask, MVT::i32));
1476 }
1477
1478 return SDValue();
1479}
1480
1481SDValue SITargetLowering::performClassCombine(SDNode *N,
1482 DAGCombinerInfo &DCI) const {
1483 SelectionDAG &DAG = DCI.DAG;
1484 SDValue Mask = N->getOperand(1);
1485
1486 // fp_class x, 0 -> false
1487 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
1488 if (CMask->isNullValue())
1489 return DAG.getConstant(0, MVT::i1);
1490 }
1491
1492 return SDValue();
1493}
1494
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001495static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
1496 switch (Opc) {
1497 case ISD::FMAXNUM:
1498 return AMDGPUISD::FMAX3;
1499 case AMDGPUISD::SMAX:
1500 return AMDGPUISD::SMAX3;
1501 case AMDGPUISD::UMAX:
1502 return AMDGPUISD::UMAX3;
1503 case ISD::FMINNUM:
1504 return AMDGPUISD::FMIN3;
1505 case AMDGPUISD::SMIN:
1506 return AMDGPUISD::SMIN3;
1507 case AMDGPUISD::UMIN:
1508 return AMDGPUISD::UMIN3;
1509 default:
1510 llvm_unreachable("Not a min/max opcode");
1511 }
1512}
1513
1514SDValue SITargetLowering::performMin3Max3Combine(SDNode *N,
1515 DAGCombinerInfo &DCI) const {
1516 SelectionDAG &DAG = DCI.DAG;
1517
1518 unsigned Opc = N->getOpcode();
1519 SDValue Op0 = N->getOperand(0);
1520 SDValue Op1 = N->getOperand(1);
1521
1522 // Only do this if the inner op has one use since this will just increases
1523 // register pressure for no benefit.
1524
1525 // max(max(a, b), c)
1526 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
1527 SDLoc DL(N);
1528 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1529 DL,
1530 N->getValueType(0),
1531 Op0.getOperand(0),
1532 Op0.getOperand(1),
1533 Op1);
1534 }
1535
1536 // max(a, max(b, c))
1537 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
1538 SDLoc DL(N);
1539 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1540 DL,
1541 N->getValueType(0),
1542 Op0,
1543 Op1.getOperand(0),
1544 Op1.getOperand(1));
1545 }
1546
1547 return SDValue();
1548}
1549
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001550SDValue SITargetLowering::performSetCCCombine(SDNode *N,
1551 DAGCombinerInfo &DCI) const {
1552 SelectionDAG &DAG = DCI.DAG;
1553 SDLoc SL(N);
1554
1555 SDValue LHS = N->getOperand(0);
1556 SDValue RHS = N->getOperand(1);
1557 EVT VT = LHS.getValueType();
1558
1559 if (VT != MVT::f32 && VT != MVT::f64)
1560 return SDValue();
1561
1562 // Match isinf pattern
1563 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
1564 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
1565 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
1566 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
1567 if (!CRHS)
1568 return SDValue();
1569
1570 const APFloat &APF = CRHS->getValueAPF();
1571 if (APF.isInfinity() && !APF.isNegative()) {
1572 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
1573 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1,
1574 LHS.getOperand(0), DAG.getConstant(Mask, MVT::i32));
1575 }
1576 }
1577
1578 return SDValue();
1579}
1580
Tom Stellard75aadc22012-12-11 21:25:42 +00001581SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1582 DAGCombinerInfo &DCI) const {
1583 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001584 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001585
1586 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00001587 default:
1588 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001589 case ISD::SETCC:
1590 return performSetCCCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001591 case ISD::FMAXNUM: // TODO: What about fmax_legacy?
1592 case ISD::FMINNUM:
1593 case AMDGPUISD::SMAX:
1594 case AMDGPUISD::SMIN:
1595 case AMDGPUISD::UMAX:
1596 case AMDGPUISD::UMIN: {
1597 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
1598 getTargetMachine().getOptLevel() > CodeGenOpt::None)
1599 return performMin3Max3Combine(N, DCI);
1600 break;
1601 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001602
1603 case AMDGPUISD::CVT_F32_UBYTE0:
1604 case AMDGPUISD::CVT_F32_UBYTE1:
1605 case AMDGPUISD::CVT_F32_UBYTE2:
1606 case AMDGPUISD::CVT_F32_UBYTE3: {
1607 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
1608
1609 SDValue Src = N->getOperand(0);
1610 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
1611
1612 APInt KnownZero, KnownOne;
1613 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1614 !DCI.isBeforeLegalizeOps());
1615 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1616 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
1617 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
1618 DCI.CommitTargetLoweringOpt(TLO);
1619 }
1620
1621 break;
1622 }
1623
1624 case ISD::UINT_TO_FP: {
1625 return performUCharToFloatCombine(N, DCI);
Matt Arsenault8675db12014-08-29 16:01:14 +00001626
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001627 case ISD::FADD: {
1628 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1629 break;
1630
1631 EVT VT = N->getValueType(0);
1632 if (VT != MVT::f32)
1633 break;
1634
Matt Arsenault8d630032015-02-20 22:10:41 +00001635 // Only do this if we are not trying to support denormals. v_mad_f32 does
1636 // not support denormals ever.
1637 if (Subtarget->hasFP32Denormals())
1638 break;
1639
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001640 SDValue LHS = N->getOperand(0);
1641 SDValue RHS = N->getOperand(1);
1642
1643 // These should really be instruction patterns, but writing patterns with
1644 // source modiifiers is a pain.
1645
1646 // fadd (fadd (a, a), b) -> mad 2.0, a, b
1647 if (LHS.getOpcode() == ISD::FADD) {
1648 SDValue A = LHS.getOperand(0);
1649 if (A == LHS.getOperand(1)) {
Tom Stellardfb77f002015-01-13 22:59:41 +00001650 const SDValue Two = DAG.getConstantFP(2.0, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00001651 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001652 }
1653 }
1654
1655 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
1656 if (RHS.getOpcode() == ISD::FADD) {
1657 SDValue A = RHS.getOperand(0);
1658 if (A == RHS.getOperand(1)) {
Tom Stellardfb77f002015-01-13 22:59:41 +00001659 const SDValue Two = DAG.getConstantFP(2.0, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00001660 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001661 }
1662 }
1663
Matt Arsenault8d630032015-02-20 22:10:41 +00001664 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001665 }
Matt Arsenault8675db12014-08-29 16:01:14 +00001666 case ISD::FSUB: {
1667 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1668 break;
1669
1670 EVT VT = N->getValueType(0);
1671
1672 // Try to get the fneg to fold into the source modifier. This undoes generic
1673 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00001674 //
1675 // Only do this if we are not trying to support denormals. v_mad_f32 does
1676 // not support denormals ever.
1677 if (VT == MVT::f32 &&
1678 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00001679 SDValue LHS = N->getOperand(0);
1680 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001681 if (LHS.getOpcode() == ISD::FADD) {
1682 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
1683
1684 SDValue A = LHS.getOperand(0);
1685 if (A == LHS.getOperand(1)) {
Tom Stellardfb77f002015-01-13 22:59:41 +00001686 const SDValue Two = DAG.getConstantFP(2.0, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001687 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
1688
Matt Arsenault8d630032015-02-20 22:10:41 +00001689 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001690 }
1691 }
1692
1693 if (RHS.getOpcode() == ISD::FADD) {
1694 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
1695
1696 SDValue A = RHS.getOperand(0);
1697 if (A == RHS.getOperand(1)) {
Tom Stellardfb77f002015-01-13 22:59:41 +00001698 const SDValue NegTwo = DAG.getConstantFP(-2.0, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00001699 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001700 }
1701 }
Matt Arsenault8d630032015-02-20 22:10:41 +00001702
1703 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00001704 }
1705
1706 break;
1707 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001708 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001709 case ISD::LOAD:
1710 case ISD::STORE:
1711 case ISD::ATOMIC_LOAD:
1712 case ISD::ATOMIC_STORE:
1713 case ISD::ATOMIC_CMP_SWAP:
1714 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
1715 case ISD::ATOMIC_SWAP:
1716 case ISD::ATOMIC_LOAD_ADD:
1717 case ISD::ATOMIC_LOAD_SUB:
1718 case ISD::ATOMIC_LOAD_AND:
1719 case ISD::ATOMIC_LOAD_OR:
1720 case ISD::ATOMIC_LOAD_XOR:
1721 case ISD::ATOMIC_LOAD_NAND:
1722 case ISD::ATOMIC_LOAD_MIN:
1723 case ISD::ATOMIC_LOAD_MAX:
1724 case ISD::ATOMIC_LOAD_UMIN:
1725 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
1726 if (DCI.isBeforeLegalize())
1727 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001728
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001729 MemSDNode *MemNode = cast<MemSDNode>(N);
1730 SDValue Ptr = MemNode->getBasePtr();
1731
1732 // TODO: We could also do this for multiplies.
1733 unsigned AS = MemNode->getAddressSpace();
1734 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
1735 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
1736 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001737 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001738
1739 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
1740 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
1741 }
1742 }
1743 break;
1744 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001745 case ISD::AND:
1746 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00001747 case ISD::OR:
1748 return performOrCombine(N, DCI);
1749 case AMDGPUISD::FP_CLASS:
1750 return performClassCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001751 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001752 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001753}
Christian Konigd910b7d2013-02-26 17:52:16 +00001754
Matt Arsenault758659232013-05-18 00:21:46 +00001755/// \brief Test if RegClass is one of the VSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001756static bool isVSrc(unsigned RegClass) {
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001757 switch(RegClass) {
1758 default: return false;
Tom Stellardb6550522015-01-12 19:33:18 +00001759 case AMDGPU::VS_32RegClassID:
1760 case AMDGPU::VS_64RegClassID:
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001761 return true;
1762 }
Christian Konigf82901a2013-02-26 17:52:23 +00001763}
1764
Christian Konigf82901a2013-02-26 17:52:23 +00001765/// \brief Analyze the possible immediate value Op
1766///
1767/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1768/// and the immediate value if it's a literal immediate
1769int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1770
Eric Christopher7792e322015-01-30 23:24:40 +00001771 const SIInstrInfo *TII =
1772 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001773
Tom Stellardedbf1eb2013-04-05 23:31:20 +00001774 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00001775 if (TII->isInlineConstant(Node->getAPIntValue()))
1776 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00001777
Matt Arsenault11a4d672015-02-13 19:05:03 +00001778 uint64_t Val = Node->getZExtValue();
1779 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00001780 }
1781
1782 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1783 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
1784 return 0;
1785
1786 if (Node->getValueType(0) == MVT::f32)
1787 return FloatToBits(Node->getValueAPF().convertToFloat());
1788
1789 return -1;
1790 }
1791
1792 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001793}
1794
Eric Christopher7792e322015-01-30 23:24:40 +00001795const TargetRegisterClass *
1796SITargetLowering::getRegClassForNode(SelectionDAG &DAG,
1797 const SDValue &Op) const {
1798 const SIInstrInfo *TII =
1799 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001800 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1801
1802 if (!Op->isMachineOpcode()) {
1803 switch(Op->getOpcode()) {
1804 case ISD::CopyFromReg: {
1805 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1806 unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1807 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1808 return MRI.getRegClass(Reg);
1809 }
1810 return TRI.getPhysRegClass(Reg);
1811 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001812 default: return nullptr;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001813 }
1814 }
1815 const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1816 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1817 if (OpClassID != -1) {
1818 return TRI.getRegClass(OpClassID);
1819 }
1820 switch(Op.getMachineOpcode()) {
1821 case AMDGPU::COPY_TO_REGCLASS:
1822 // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1823 OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1824
1825 // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1826 // class, then the register class for the value could be either a
1827 // VReg or and SReg. In order to get a more accurate
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001828 if (isVSrc(OpClassID))
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001829 return getRegClassForNode(DAG, Op.getOperand(0));
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001830
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001831 return TRI.getRegClass(OpClassID);
1832 case AMDGPU::EXTRACT_SUBREG: {
1833 int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1834 const TargetRegisterClass *SuperClass =
1835 getRegClassForNode(DAG, Op.getOperand(0));
1836 return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1837 }
1838 case AMDGPU::REG_SEQUENCE:
1839 // Operand 0 is the register class id for REG_SEQUENCE instructions.
1840 return TRI.getRegClass(
1841 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1842 default:
1843 return getRegClassFor(Op.getSimpleValueType());
1844 }
1845}
1846
Christian Konigf82901a2013-02-26 17:52:23 +00001847/// \brief Does "Op" fit into register class "RegClass" ?
Tom Stellardb35efba2013-05-20 15:02:01 +00001848bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
Christian Konigf82901a2013-02-26 17:52:23 +00001849 unsigned RegClass) const {
Eric Christopher7792e322015-01-30 23:24:40 +00001850 const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001851 const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1852 if (!RC) {
Christian Konigf82901a2013-02-26 17:52:23 +00001853 return false;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001854 }
1855 return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
Christian Konigf82901a2013-02-26 17:52:23 +00001856}
1857
Christian Konig8e06e2a2013-04-10 08:39:08 +00001858/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00001859static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00001860 switch (Idx) {
1861 default: return 0;
1862 case AMDGPU::sub0: return 0;
1863 case AMDGPU::sub1: return 1;
1864 case AMDGPU::sub2: return 2;
1865 case AMDGPU::sub3: return 3;
1866 }
1867}
1868
1869/// \brief Adjust the writemask of MIMG instructions
1870void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1871 SelectionDAG &DAG) const {
1872 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00001873 unsigned Lane = 0;
1874 unsigned OldDmask = Node->getConstantOperandVal(0);
1875 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001876
1877 // Try to figure out the used register components
1878 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1879 I != E; ++I) {
1880
1881 // Abort if we can't understand the usage
1882 if (!I->isMachineOpcode() ||
1883 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1884 return;
1885
Tom Stellard54774e52013-10-23 02:53:47 +00001886 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1887 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1888 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1889 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00001890 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001891
Tom Stellard54774e52013-10-23 02:53:47 +00001892 // Set which texture component corresponds to the lane.
1893 unsigned Comp;
1894 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1895 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00001896 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00001897 Dmask &= ~(1 << Comp);
1898 }
1899
Christian Konig8e06e2a2013-04-10 08:39:08 +00001900 // Abort if we have more than one user per component
1901 if (Users[Lane])
1902 return;
1903
1904 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00001905 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001906 }
1907
Tom Stellard54774e52013-10-23 02:53:47 +00001908 // Abort if there's no change
1909 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00001910 return;
1911
1912 // Adjust the writemask in the node
1913 std::vector<SDValue> Ops;
Tom Stellard54774e52013-10-23 02:53:47 +00001914 Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00001915 Ops.insert(Ops.end(), Node->op_begin() + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00001916 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001917
Christian Konig8b1ed282013-04-10 08:39:16 +00001918 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00001919 // (if NewDmask has only one bit set...)
1920 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001921 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00001922 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001923 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00001924 SDValue(Node, 0), RC);
1925 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1926 return;
1927 }
1928
Christian Konig8e06e2a2013-04-10 08:39:08 +00001929 // Update the users of the node with the new indices
1930 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1931
1932 SDNode *User = Users[i];
1933 if (!User)
1934 continue;
1935
1936 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1937 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1938
1939 switch (Idx) {
1940 default: break;
1941 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1942 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1943 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1944 }
1945 }
1946}
1947
Tom Stellard3457a842014-10-09 19:06:00 +00001948/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
1949/// with frame index operands.
1950/// LLVM assumes that inputs are to these instructions are registers.
1951void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
1952 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00001953
1954 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00001955 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
1956 if (!isa<FrameIndexSDNode>(Node->getOperand(i))) {
1957 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00001958 continue;
1959 }
1960
Tom Stellard3457a842014-10-09 19:06:00 +00001961 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00001962 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00001963 Node->getOperand(i).getValueType(),
1964 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00001965 }
1966
Tom Stellard3457a842014-10-09 19:06:00 +00001967 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00001968}
1969
Matt Arsenault08d84942014-06-03 23:06:13 +00001970/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00001971SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1972 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00001973 const SIInstrInfo *TII =
1974 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellard0518ff82013-06-03 17:39:58 +00001975 Node = AdjustRegClass(Node, DAG);
Christian Konig8e06e2a2013-04-10 08:39:08 +00001976
Tom Stellard16a9a202013-08-14 23:24:17 +00001977 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00001978 adjustWritemask(Node, DAG);
1979
Matt Arsenault7d858d82014-11-02 23:46:54 +00001980 if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG ||
1981 Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00001982 legalizeTargetIndependentNode(Node, DAG);
1983 return Node;
1984 }
Tom Stellard654d6692015-01-08 15:08:17 +00001985 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001986}
Christian Konig8b1ed282013-04-10 08:39:16 +00001987
1988/// \brief Assign the register class depending on the number of
1989/// bits set in the writemask
1990void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1991 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00001992 const SIInstrInfo *TII =
1993 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001994
Tom Stellarda99ada52014-11-21 22:31:44 +00001995 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00001996 TII->legalizeOperands(MI);
1997
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00001998 if (TII->isMIMG(MI->getOpcode())) {
1999 unsigned VReg = MI->getOperand(0).getReg();
2000 unsigned Writemask = MI->getOperand(1).getImm();
2001 unsigned BitsSet = 0;
2002 for (unsigned i = 0; i < 4; ++i)
2003 BitsSet += Writemask & (1 << i) ? 1 : 0;
2004
2005 const TargetRegisterClass *RC;
2006 switch (BitsSet) {
2007 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002008 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002009 case 2: RC = &AMDGPU::VReg_64RegClass; break;
2010 case 3: RC = &AMDGPU::VReg_96RegClass; break;
2011 }
2012
2013 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
2014 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002015 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00002016 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00002017 }
2018
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002019 // Replace unused atomics with the no return version.
2020 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
2021 if (NoRetAtomicOp != -1) {
2022 if (!Node->hasAnyUseOfValue(0)) {
2023 MI->setDesc(TII->get(NoRetAtomicOp));
2024 MI->RemoveOperand(0);
2025 }
2026
2027 return;
2028 }
Christian Konig8b1ed282013-04-10 08:39:16 +00002029}
Tom Stellard0518ff82013-06-03 17:39:58 +00002030
Matt Arsenault485defe2014-11-05 19:01:17 +00002031static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
2032 SDValue K = DAG.getTargetConstant(Val, MVT::i32);
2033 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
2034}
2035
2036MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
2037 SDLoc DL,
2038 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002039 const SIInstrInfo *TII =
2040 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00002041#if 1
2042 // XXX - Workaround for moveToVALU not handling different register class
2043 // inserts for REG_SEQUENCE.
2044
2045 // Build the half of the subregister with the constants.
2046 const SDValue Ops0[] = {
2047 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, MVT::i32),
2048 buildSMovImm32(DAG, DL, 0),
2049 DAG.getTargetConstant(AMDGPU::sub0, MVT::i32),
Tom Stellard794c8c02014-12-02 17:05:41 +00002050 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002051 DAG.getTargetConstant(AMDGPU::sub1, MVT::i32)
2052 };
2053
2054 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
2055 MVT::v2i32, Ops0), 0);
2056
2057 // Combine the constants and the pointer.
2058 const SDValue Ops1[] = {
2059 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2060 Ptr,
2061 DAG.getTargetConstant(AMDGPU::sub0_sub1, MVT::i32),
2062 SubRegHi,
2063 DAG.getTargetConstant(AMDGPU::sub2_sub3, MVT::i32)
2064 };
2065
2066 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
2067#else
2068 const SDValue Ops[] = {
2069 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2070 Ptr,
2071 DAG.getTargetConstant(AMDGPU::sub0_sub1, MVT::i32),
2072 buildSMovImm32(DAG, DL, 0),
2073 DAG.getTargetConstant(AMDGPU::sub2, MVT::i32),
Tom Stellard794c8c02014-12-02 17:05:41 +00002074 buildSMovImm32(DAG, DL, TII->getDefaultRsrcFormat() >> 32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002075 DAG.getTargetConstant(AMDGPU::sub3, MVT::i32)
2076 };
2077
2078 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2079
2080#endif
2081}
2082
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002083/// \brief Return a resource descriptor with the 'Add TID' bit enabled
2084/// The TID (Thread ID) is multipled by the stride value (bits [61:48]
2085/// of the resource descriptor) to create an offset, which is added to the
2086/// resource ponter.
2087MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
2088 SDLoc DL,
2089 SDValue Ptr,
2090 uint32_t RsrcDword1,
2091 uint64_t RsrcDword2And3) const {
2092 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
2093 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
2094 if (RsrcDword1) {
2095 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
2096 DAG.getConstant(RsrcDword1, MVT::i32)), 0);
2097 }
2098
2099 SDValue DataLo = buildSMovImm32(DAG, DL,
2100 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
2101 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
2102
2103 const SDValue Ops[] = {
2104 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2105 PtrLo,
2106 DAG.getTargetConstant(AMDGPU::sub0, MVT::i32),
2107 PtrHi,
2108 DAG.getTargetConstant(AMDGPU::sub1, MVT::i32),
2109 DataLo,
2110 DAG.getTargetConstant(AMDGPU::sub2, MVT::i32),
2111 DataHi,
2112 DAG.getTargetConstant(AMDGPU::sub3, MVT::i32)
2113 };
2114
2115 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2116}
2117
2118MachineSDNode *SITargetLowering::buildScratchRSRC(SelectionDAG &DAG,
2119 SDLoc DL,
2120 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002121 const SIInstrInfo *TII =
2122 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellard794c8c02014-12-02 17:05:41 +00002123 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() | AMDGPU::RSRC_TID_ENABLE |
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002124 0xffffffff; // Size
2125
2126 return buildRSRC(DAG, DL, Ptr, 0, Rsrc);
2127}
2128
Tom Stellard0518ff82013-06-03 17:39:58 +00002129MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
2130 SelectionDAG &DAG) const {
2131
2132 SDLoc DL(N);
2133 unsigned NewOpcode = N->getMachineOpcode();
2134
2135 switch (N->getMachineOpcode()) {
2136 default: return N;
Tom Stellard0518ff82013-06-03 17:39:58 +00002137 case AMDGPU::S_LOAD_DWORD_IMM:
2138 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
2139 // Fall-through
2140 case AMDGPU::S_LOAD_DWORDX2_SGPR:
2141 if (NewOpcode == N->getMachineOpcode()) {
2142 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
2143 }
2144 // Fall-through
2145 case AMDGPU::S_LOAD_DWORDX4_IMM:
2146 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
2147 if (NewOpcode == N->getMachineOpcode()) {
2148 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
2149 }
2150 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
2151 return N;
2152 }
2153 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
Matt Arsenault485defe2014-11-05 19:01:17 +00002154
2155 const SDValue Zero64 = DAG.getTargetConstant(0, MVT::i64);
2156 SDValue Ptr(DAG.getMachineNode(AMDGPU::S_MOV_B64, DL, MVT::i64, Zero64), 0);
2157 MachineSDNode *RSrc = wrapAddr64Rsrc(DAG, DL, Ptr);
Matt Arsenault61a528a2014-09-10 23:26:19 +00002158
2159 SmallVector<SDValue, 8> Ops;
2160 Ops.push_back(SDValue(RSrc, 0));
2161 Ops.push_back(N->getOperand(0));
Tom Stellardc53861a2015-02-11 00:34:32 +00002162 Ops.push_back(DAG.getTargetConstant(0, MVT::i32)); // soffset
Marek Olsak58f61a82014-12-07 17:17:38 +00002163
2164 // The immediate offset is in dwords on SI and in bytes on VI.
2165 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
Marek Olsak0c056452014-12-10 19:25:31 +00002166 Ops.push_back(DAG.getTargetConstant(Offset->getSExtValue(), MVT::i32));
Marek Olsak58f61a82014-12-07 17:17:38 +00002167 else
Marek Olsak0c056452014-12-10 19:25:31 +00002168 Ops.push_back(DAG.getTargetConstant(Offset->getSExtValue() << 2, MVT::i32));
Matt Arsenault61a528a2014-09-10 23:26:19 +00002169
2170 // Copy remaining operands so we keep any chain and glue nodes that follow
2171 // the normal operands.
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002172 Ops.append(N->op_begin() + 2, N->op_end());
Matt Arsenault61a528a2014-09-10 23:26:19 +00002173
Tom Stellard0518ff82013-06-03 17:39:58 +00002174 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
2175 }
2176 }
2177}
Tom Stellard94593ee2013-06-03 17:40:18 +00002178
2179SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2180 const TargetRegisterClass *RC,
2181 unsigned Reg, EVT VT) const {
2182 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2183
2184 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2185 cast<RegisterSDNode>(VReg)->getReg(), VT);
2186}