blob: de2cdd85ff349f1d9c208ebd2da21ec7380974b6 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11/// \brief Custom DAG lowering for SI
12//
13//===----------------------------------------------------------------------===//
14
NAKAMURA Takumi45e0a832014-07-20 11:15:07 +000015#ifdef _MSC_VER
16// Provide M_PI.
17#define _USE_MATH_DEFINES
18#include <cmath>
19#endif
20
Tom Stellard75aadc22012-12-11 21:25:42 +000021#include "SIISelLowering.h"
Christian Konig99ee0f42013-03-07 09:04:14 +000022#include "AMDGPU.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000023#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000024#include "AMDGPUSubtarget.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000025#include "SIInstrInfo.h"
26#include "SIMachineFunctionInfo.h"
27#include "SIRegisterInfo.h"
Alexey Samsonova253bf92014-08-27 19:36:53 +000028#include "llvm/ADT/BitVector.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000029#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000030#include "llvm/CodeGen/MachineInstrBuilder.h"
31#include "llvm/CodeGen/MachineRegisterInfo.h"
32#include "llvm/CodeGen/SelectionDAG.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000033#include "llvm/IR/Function.h"
Matt Arsenault364a6742014-06-11 17:50:44 +000034#include "llvm/ADT/SmallString.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000035
36using namespace llvm;
37
38SITargetLowering::SITargetLowering(TargetMachine &TM) :
Bill Wendling37e9adb2013-06-07 20:28:55 +000039 AMDGPUTargetLowering(TM) {
Tom Stellard1bd80722014-04-30 15:31:33 +000040 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000041 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000042
Christian Konig2214f142013-03-07 09:03:38 +000043 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
44 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
45
Tom Stellard334b29c2014-04-17 21:00:09 +000046 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000047 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000048
Tom Stellard436780b2014-05-15 14:41:57 +000049 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
50 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
51 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000052
Tom Stellard436780b2014-05-15 14:41:57 +000053 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
54 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000055
Tom Stellardf0a21072014-11-18 20:39:39 +000056 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000057 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
58
Tom Stellardf0a21072014-11-18 20:39:39 +000059 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000060 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000061
62 computeRegisterProperties();
63
Christian Konig2989ffc2013-03-18 11:34:16 +000064 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
65 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
66 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
67 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
68
Tom Stellard75aadc22012-12-11 21:25:42 +000069 setOperationAction(ISD::ADD, MVT::i32, Legal);
Matt Arsenaulte8d21462013-11-18 20:09:40 +000070 setOperationAction(ISD::ADDC, MVT::i32, Legal);
71 setOperationAction(ISD::ADDE, MVT::i32, Legal);
Matt Arsenaultb8b51532014-06-23 18:00:38 +000072 setOperationAction(ISD::SUBC, MVT::i32, Legal);
73 setOperationAction(ISD::SUBE, MVT::i32, Legal);
Aaron Watrydaabb202013-06-25 13:55:52 +000074
Matt Arsenaultad14ce82014-07-19 18:44:39 +000075 setOperationAction(ISD::FSIN, MVT::f32, Custom);
76 setOperationAction(ISD::FCOS, MVT::f32, Custom);
77
Matt Arsenault7c936902014-10-21 23:01:01 +000078 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
79 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
80 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
81 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
82
Tom Stellard35bb18c2013-08-26 15:06:04 +000083 // We need to custom lower vector stores from local memory
Tom Stellard35bb18c2013-08-26 15:06:04 +000084 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000085 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
86 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
87
88 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
89 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000090
Tom Stellard1c8788e2014-03-07 20:12:33 +000091 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000092 setOperationAction(ISD::STORE, MVT::i32, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000093 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
94 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
95
Tom Stellard0ec134f2014-02-04 17:18:40 +000096 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +000097 setOperationAction(ISD::SELECT, MVT::f64, Promote);
98 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +000099
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000100 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
101 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
102 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
103 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000104
Tom Stellard83747202013-07-18 21:43:53 +0000105 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
106 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
107
Matt Arsenaulte306a322014-10-21 16:25:08 +0000108 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
109
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000110 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000111 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
113
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000114 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000115 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
116 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
117
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
121
Matt Arsenault94812212014-11-14 18:18:16 +0000122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
124
Tom Stellard94593ee2013-06-03 17:40:18 +0000125 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
127 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
128 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000129
Tom Stellardafcf12f2013-09-12 02:55:14 +0000130 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000131 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000132
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000133 for (MVT VT : MVT::integer_valuetypes()) {
134 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
135 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Custom);
136 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Custom);
137 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000138
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000139 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
140 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Custom);
141 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Custom);
142 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000143
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000144 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
145 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Custom);
146 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Custom);
147 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
148 }
149
150 for (MVT VT : MVT::integer_vector_valuetypes()) {
151 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
152 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
153 }
154
155 for (MVT VT : MVT::fp_valuetypes())
156 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000157
Tom Stellarde9373602014-01-22 19:24:14 +0000158 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
159 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
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 for (int I = MVT::v1f64; I <= MVT::v8f64; ++I) {
207 MVT::SimpleValueType VT = static_cast<MVT::SimpleValueType>(I);
Matt Arsenaulta81aee82014-02-24 21:16:50 +0000208 setOperationAction(ISD::FTRUNC, VT, Expand);
209 setOperationAction(ISD::FCEIL, VT, Expand);
210 setOperationAction(ISD::FFLOOR, VT, Expand);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000211 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000212
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000213 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
214 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
215 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
216 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000217 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000218 }
219
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000220 setOperationAction(ISD::FDIV, MVT::f32, Custom);
221
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000222 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000223 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000224 setTargetDAGCombine(ISD::FMINNUM);
225 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000226 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +0000227 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000228 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000229 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000230 setTargetDAGCombine(ISD::UINT_TO_FP);
231
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000232 // All memory operations. Some folding on the pointer operand is done to help
233 // matching the constant offsets in the addressing modes.
234 setTargetDAGCombine(ISD::LOAD);
235 setTargetDAGCombine(ISD::STORE);
236 setTargetDAGCombine(ISD::ATOMIC_LOAD);
237 setTargetDAGCombine(ISD::ATOMIC_STORE);
238 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
239 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
240 setTargetDAGCombine(ISD::ATOMIC_SWAP);
241 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
242 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
243 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
244 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
245 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
246 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
247 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
248 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
249 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
250 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
251
Christian Konigeecebd02013-03-26 14:04:02 +0000252 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000253}
254
Tom Stellard0125f2a2013-06-25 02:39:35 +0000255//===----------------------------------------------------------------------===//
256// TargetLowering queries
257//===----------------------------------------------------------------------===//
258
Matt Arsenaulte306a322014-10-21 16:25:08 +0000259bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
260 EVT) const {
261 // SI has some legal vector types, but no legal vector operations. Say no
262 // shuffles are legal in order to prefer scalarizing some vector operations.
263 return false;
264}
265
Matt Arsenault5015a892014-08-15 17:17:07 +0000266// FIXME: This really needs an address space argument. The immediate offset
267// size is different for different sets of memory instruction sets.
268
269// The single offset DS instructions have a 16-bit unsigned byte offset.
270//
271// MUBUF / MTBUF have a 12-bit unsigned byte offset, and additionally can do r +
272// r + i with addr64. 32-bit has more addressing mode options. Depending on the
273// resource constant, it can also do (i64 r0) + (i32 r1) * (i14 i).
274//
275// SMRD instructions have an 8-bit, dword offset.
276//
277bool SITargetLowering::isLegalAddressingMode(const AddrMode &AM,
278 Type *Ty) const {
279 // No global is ever allowed as a base.
280 if (AM.BaseGV)
281 return false;
282
283 // Allow a 16-bit unsigned immediate field, since this is what DS instructions
284 // use.
285 if (!isUInt<16>(AM.BaseOffs))
286 return false;
287
288 // Only support r+r,
289 switch (AM.Scale) {
290 case 0: // "r+i" or just "i", depending on HasBaseReg.
291 break;
292 case 1:
293 if (AM.HasBaseReg && AM.BaseOffs) // "r+r+i" is not allowed.
294 return false;
295 // Otherwise we have r+r or r+i.
296 break;
297 case 2:
298 if (AM.HasBaseReg || AM.BaseOffs) // 2*r+r or 2*r+i is not allowed.
299 return false;
300 // Allow 2*r as r+r.
301 break;
302 default: // Don't allow n * r
303 return false;
304 }
305
306 return true;
307}
308
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000309bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
310 unsigned AddrSpace,
311 unsigned Align,
312 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000313 if (IsFast)
314 *IsFast = false;
315
Matt Arsenault1018c892014-04-24 17:08:26 +0000316 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
317 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000318 if (!VT.isSimple() || VT == MVT::Other)
319 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000320
321 // XXX - CI changes say "Support for unaligned memory accesses" but I don't
322 // see what for specifically. The wording everywhere else seems to be the
323 // same.
324
Matt Arsenault1018c892014-04-24 17:08:26 +0000325 // XXX - The only mention I see of this in the ISA manual is for LDS direct
326 // reads the "byte address and must be dword aligned". Is it also true for the
327 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000328 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
329 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
330 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
331 // with adjacent offsets.
332 return Align % 4 == 0;
333 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000334
335 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
336 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000337 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000338 if (IsFast)
339 *IsFast = true;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000340 return VT.bitsGT(MVT::i32);
341}
342
Matt Arsenault46645fa2014-07-28 17:49:26 +0000343EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
344 unsigned SrcAlign, bool IsMemset,
345 bool ZeroMemset,
346 bool MemcpyStrSrc,
347 MachineFunction &MF) const {
348 // FIXME: Should account for address space here.
349
350 // The default fallback uses the private pointer size as a guess for a type to
351 // use. Make sure we switch these to 64-bit accesses.
352
353 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
354 return MVT::v4i32;
355
356 if (Size >= 8 && DstAlign >= 4)
357 return MVT::v2i32;
358
359 // Use the default.
360 return MVT::Other;
361}
362
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000363TargetLoweringBase::LegalizeTypeAction
364SITargetLowering::getPreferredVectorAction(EVT VT) const {
365 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
366 return TypeSplitVector;
367
368 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000369}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000370
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000371bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
372 Type *Ty) const {
Eric Christopherd9134482014-08-04 21:25:23 +0000373 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
374 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000375 return TII->isInlineConstant(Imm);
376}
377
Tom Stellardaf775432013-10-23 00:44:32 +0000378SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000379 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000380 unsigned Offset, bool Signed) const {
Matt Arsenault86033ca2014-07-28 17:31:39 +0000381 const DataLayout *DL = getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000382 MachineFunction &MF = DAG.getMachineFunction();
383 const SIRegisterInfo *TRI =
384 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
385 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000386
Matt Arsenault86033ca2014-07-28 17:31:39 +0000387 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
388
389 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
390 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
391 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000392 MRI.getLiveInVirtReg(InputPtrReg), MVT::i64);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000393 SDValue Ptr = DAG.getNode(ISD::ADD, SL, MVT::i64, BasePtr,
394 DAG.getConstant(Offset, MVT::i64));
395 SDValue PtrOffset = DAG.getUNDEF(getPointerTy(AMDGPUAS::CONSTANT_ADDRESS));
396 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
397
398 return DAG.getLoad(ISD::UNINDEXED, Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD,
399 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
400 false, // isVolatile
401 true, // isNonTemporal
402 true, // isInvariant
403 DL->getABITypeAlignment(Ty)); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000404}
405
Christian Konig2c8f6d52013-03-07 09:03:52 +0000406SDValue SITargetLowering::LowerFormalArguments(
407 SDValue Chain,
408 CallingConv::ID CallConv,
409 bool isVarArg,
410 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000411 SDLoc DL, SelectionDAG &DAG,
Christian Konig2c8f6d52013-03-07 09:03:52 +0000412 SmallVectorImpl<SDValue> &InVals) const {
413
Tom Stellardec2e43c2014-09-22 15:35:29 +0000414 const TargetMachine &TM = getTargetMachine();
415 const SIRegisterInfo *TRI =
416 static_cast<const SIRegisterInfo*>(TM.getSubtargetImpl()->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000417
418 MachineFunction &MF = DAG.getMachineFunction();
419 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000420 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000421
422 assert(CallConv == CallingConv::C);
423
424 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000425 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000426
427 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000428 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000429
430 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000431 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Vincent Lejeuned6236442013-10-13 17:56:16 +0000432 !Arg.Flags.isByVal()) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000433
434 assert((PSInputNum <= 15) && "Too many PS inputs!");
435
436 if (!Arg.Used) {
437 // We can savely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000438 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000439 ++PSInputNum;
440 continue;
441 }
442
443 Info->PSInputAddr |= 1 << PSInputNum++;
444 }
445
446 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000447 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000448 ISD::InputArg NewArg = Arg;
449 NewArg.Flags.setSplit();
450 NewArg.VT = Arg.VT.getVectorElementType();
451
452 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
453 // three or five element vertex only needs three or five registers,
454 // NOT four or eigth.
455 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
456 unsigned NumElements = ParamType->getVectorNumElements();
457
458 for (unsigned j = 0; j != NumElements; ++j) {
459 Splits.push_back(NewArg);
460 NewArg.PartOffset += NewArg.VT.getStoreSize();
461 }
462
Matt Arsenault762af962014-07-13 03:06:39 +0000463 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000464 Splits.push_back(Arg);
465 }
466 }
467
468 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000469 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
470 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000471
Christian Konig99ee0f42013-03-07 09:04:14 +0000472 // At least one interpolation mode must be enabled or else the GPU will hang.
Matt Arsenault762af962014-07-13 03:06:39 +0000473 if (Info->getShaderType() == ShaderType::PIXEL &&
474 (Info->PSInputAddr & 0x7F) == 0) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000475 Info->PSInputAddr |= 1;
476 CCInfo.AllocateReg(AMDGPU::VGPR0);
477 CCInfo.AllocateReg(AMDGPU::VGPR1);
478 }
479
Tom Stellarded882c22013-06-03 17:40:11 +0000480 // The pointer to the list of arguments is stored in SGPR0, SGPR1
Tom Stellardb02094e2014-07-21 15:45:01 +0000481 // The pointer to the scratch buffer is stored in SGPR2, SGPR3
Matt Arsenault762af962014-07-13 03:06:39 +0000482 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardfeab91c2014-12-02 17:41:43 +0000483 if (Subtarget->isAmdHsaOS())
484 Info->NumUserSGPRs = 2; // FIXME: Need to support scratch buffers.
485 else
486 Info->NumUserSGPRs = 4;
Tom Stellardec2e43c2014-09-22 15:35:29 +0000487
488 unsigned InputPtrReg =
489 TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
490 unsigned InputPtrRegLo =
491 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 0);
492 unsigned InputPtrRegHi =
493 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 1);
494
495 unsigned ScratchPtrReg =
496 TRI->getPreloadedValue(MF, SIRegisterInfo::SCRATCH_PTR);
497 unsigned ScratchPtrRegLo =
498 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 0);
499 unsigned ScratchPtrRegHi =
500 TRI->getPhysRegSubReg(ScratchPtrReg, &AMDGPU::SReg_32RegClass, 1);
501
502 CCInfo.AllocateReg(InputPtrRegLo);
503 CCInfo.AllocateReg(InputPtrRegHi);
504 CCInfo.AllocateReg(ScratchPtrRegLo);
505 CCInfo.AllocateReg(ScratchPtrRegHi);
506 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
507 MF.addLiveIn(ScratchPtrReg, &AMDGPU::SReg_64RegClass);
Tom Stellarded882c22013-06-03 17:40:11 +0000508 }
509
Matt Arsenault762af962014-07-13 03:06:39 +0000510 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000511 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
512 Splits);
513 }
514
Christian Konig2c8f6d52013-03-07 09:03:52 +0000515 AnalyzeFormalArguments(CCInfo, Splits);
516
517 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
518
Christian Konigb7be72d2013-05-17 09:46:48 +0000519 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000520 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000521 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000522 continue;
523 }
524
Christian Konig2c8f6d52013-03-07 09:03:52 +0000525 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000526 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000527
528 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000529 VT = Ins[i].VT;
530 EVT MemVT = Splits[i].VT;
Jan Veselye5121f32014-10-14 20:05:26 +0000531 const unsigned Offset = 36 + VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000532 // The first 36 bytes of the input buffer contains information about
533 // thread group and global sizes.
Tom Stellardaf775432013-10-23 00:44:32 +0000534 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
Jan Veselye5121f32014-10-14 20:05:26 +0000535 Offset, Ins[i].Flags.isSExt());
Tom Stellardca7ecf32014-08-22 18:49:31 +0000536
537 const PointerType *ParamTy =
538 dyn_cast<PointerType>(FType->getParamType(Ins[i].OrigArgIndex));
539 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
540 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
541 // On SI local pointers are just offsets into LDS, so they are always
542 // less than 16-bits. On CI and newer they could potentially be
543 // real pointers, so we can't guarantee their size.
544 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
545 DAG.getValueType(MVT::i16));
546 }
547
Tom Stellarded882c22013-06-03 17:40:11 +0000548 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000549 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000550 continue;
551 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000552 assert(VA.isRegLoc() && "Parameter must be in a register!");
553
554 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000555
556 if (VT == MVT::i64) {
557 // For now assume it is a pointer
558 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
559 &AMDGPU::SReg_64RegClass);
560 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
561 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
562 continue;
563 }
564
565 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
566
567 Reg = MF.addLiveIn(Reg, RC);
568 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
569
Christian Konig2c8f6d52013-03-07 09:03:52 +0000570 if (Arg.VT.isVector()) {
571
572 // Build a vector from the registers
573 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
574 unsigned NumElements = ParamType->getVectorNumElements();
575
576 SmallVector<SDValue, 4> Regs;
577 Regs.push_back(Val);
578 for (unsigned j = 1; j != NumElements; ++j) {
579 Reg = ArgLocs[ArgIdx++].getLocReg();
580 Reg = MF.addLiveIn(Reg, RC);
581 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
582 }
583
584 // Fill up the missing vector elements
585 NumElements = Arg.VT.getVectorNumElements() - NumElements;
586 for (unsigned j = 0; j != NumElements; ++j)
587 Regs.push_back(DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000588
Craig Topper48d114b2014-04-26 18:35:24 +0000589 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000590 continue;
591 }
592
593 InVals.push_back(Val);
594 }
595 return Chain;
596}
597
Tom Stellard75aadc22012-12-11 21:25:42 +0000598MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
599 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000600
Tom Stellard556d9aa2013-06-03 17:39:37 +0000601 MachineBasicBlock::iterator I = *MI;
Eric Christopherd9134482014-08-04 21:25:23 +0000602 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
603 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Tom Stellard556d9aa2013-06-03 17:39:37 +0000604
Tom Stellard75aadc22012-12-11 21:25:42 +0000605 switch (MI->getOpcode()) {
606 default:
607 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
608 case AMDGPU::BRANCH: return BB;
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000609 case AMDGPU::V_SUB_F64: {
610 unsigned DestReg = MI->getOperand(0).getReg();
611 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64), DestReg)
612 .addImm(0) // SRC0 modifiers
613 .addReg(MI->getOperand(1).getReg())
614 .addImm(1) // SRC1 modifiers
615 .addReg(MI->getOperand(2).getReg())
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000616 .addImm(0) // CLAMP
617 .addImm(0); // OMOD
Tom Stellard2a6a61052013-07-12 18:15:08 +0000618 MI->eraseFromParent();
619 break;
Matt Arsenaultdbc9aae2014-06-18 17:13:51 +0000620 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000621 case AMDGPU::SI_RegisterStorePseudo: {
622 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard81d871d2013-11-13 23:36:50 +0000623 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
624 MachineInstrBuilder MIB =
625 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
626 Reg);
627 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
628 MIB.addOperand(MI->getOperand(i));
629
630 MI->eraseFromParent();
Vincent Lejeune79a58342014-05-10 19:18:25 +0000631 break;
632 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000633 }
634 return BB;
635}
636
Matt Arsenault8596f712014-11-28 22:51:38 +0000637EVT SITargetLowering::getSetCCResultType(LLVMContext &Ctx, EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +0000638 if (!VT.isVector()) {
639 return MVT::i1;
640 }
Matt Arsenault8596f712014-11-28 22:51:38 +0000641 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +0000642}
643
Christian Konig082a14a2013-03-18 11:34:05 +0000644MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
645 return MVT::i32;
646}
647
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000648bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
649 VT = VT.getScalarType();
650
651 if (!VT.isSimple())
652 return false;
653
654 switch (VT.getSimpleVT().SimpleTy) {
655 case MVT::f32:
656 return false; /* There is V_MAD_F32 for f32 */
657 case MVT::f64:
658 return true;
659 default:
660 break;
661 }
662
663 return false;
664}
665
Tom Stellard75aadc22012-12-11 21:25:42 +0000666//===----------------------------------------------------------------------===//
667// Custom DAG Lowering Operations
668//===----------------------------------------------------------------------===//
669
670SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
671 switch (Op.getOpcode()) {
672 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +0000673 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000674 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000675 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +0000676 SDValue Result = LowerLOAD(Op, DAG);
677 assert((!Result.getNode() ||
678 Result.getNode()->getNumValues() == 2) &&
679 "Load should return a value and a chain");
680 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +0000681 }
Tom Stellardaf775432013-10-23 00:44:32 +0000682
Matt Arsenaultad14ce82014-07-19 18:44:39 +0000683 case ISD::FSIN:
684 case ISD::FCOS:
685 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000686 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000687 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000688 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000689 case ISD::GlobalAddress: {
690 MachineFunction &MF = DAG.getMachineFunction();
691 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
692 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +0000693 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000694 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
695 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000696 }
697 return SDValue();
698}
699
Tom Stellardf8794352012-12-19 22:10:31 +0000700/// \brief Helper function for LowerBRCOND
701static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000702
Tom Stellardf8794352012-12-19 22:10:31 +0000703 SDNode *Parent = Value.getNode();
704 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
705 I != E; ++I) {
706
707 if (I.getUse().get() != Value)
708 continue;
709
710 if (I->getOpcode() == Opcode)
711 return *I;
712 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000713 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000714}
715
Tom Stellardb02094e2014-07-21 15:45:01 +0000716SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
717
Tom Stellardb02094e2014-07-21 15:45:01 +0000718 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
719 unsigned FrameIndex = FINode->getIndex();
720
Tom Stellardb02094e2014-07-21 15:45:01 +0000721 return DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
722}
723
Tom Stellardf8794352012-12-19 22:10:31 +0000724/// This transforms the control flow intrinsics to get the branch destination as
725/// last parameter, also switches branch target with BR if the need arise
726SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
727 SelectionDAG &DAG) const {
728
Andrew Trickef9de2a2013-05-25 02:42:55 +0000729 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000730
731 SDNode *Intr = BRCOND.getOperand(1).getNode();
732 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +0000733 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000734
735 if (Intr->getOpcode() == ISD::SETCC) {
736 // As long as we negate the condition everything is fine
737 SDNode *SetCC = Intr;
738 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000739 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
740 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000741 Intr = SetCC->getOperand(0).getNode();
742
743 } else {
744 // Get the target from BR if we don't negate the condition
745 BR = findUser(BRCOND, ISD::BR);
746 Target = BR->getOperand(1);
747 }
748
749 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
750
751 // Build the result and
752 SmallVector<EVT, 4> Res;
753 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
754 Res.push_back(Intr->getValueType(i));
755
756 // operands of the new intrinsic call
757 SmallVector<SDValue, 4> Ops;
758 Ops.push_back(BRCOND.getOperand(0));
759 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
760 Ops.push_back(Intr->getOperand(i));
761 Ops.push_back(Target);
762
763 // build the new intrinsic call
764 SDNode *Result = DAG.getNode(
765 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +0000766 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000767
768 if (BR) {
769 // Give the branch instruction our target
770 SDValue Ops[] = {
771 BR->getOperand(0),
772 BRCOND.getOperand(2)
773 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000774 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
775 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
776 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000777 }
778
779 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
780
781 // Copy the intrinsic results to registers
782 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
783 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
784 if (!CopyToReg)
785 continue;
786
787 Chain = DAG.getCopyToReg(
788 Chain, DL,
789 CopyToReg->getOperand(1),
790 SDValue(Result, i - 1),
791 SDValue());
792
793 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
794 }
795
796 // Remove the old intrinsic from the chain
797 DAG.ReplaceAllUsesOfValueWith(
798 SDValue(Intr, Intr->getNumValues() - 1),
799 Intr->getOperand(0));
800
801 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000802}
803
Tom Stellard067c8152014-07-21 14:01:14 +0000804SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
805 SDValue Op,
806 SelectionDAG &DAG) const {
807 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
808
809 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
810 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
811
812 SDLoc DL(GSD);
813 const GlobalValue *GV = GSD->getGlobal();
814 MVT PtrVT = getPointerTy(GSD->getAddressSpace());
815
816 SDValue Ptr = DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT);
817 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
818
819 SDValue PtrLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
820 DAG.getConstant(0, MVT::i32));
821 SDValue PtrHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
822 DAG.getConstant(1, MVT::i32));
823
824 SDValue Lo = DAG.getNode(ISD::ADDC, DL, DAG.getVTList(MVT::i32, MVT::Glue),
825 PtrLo, GA);
826 SDValue Hi = DAG.getNode(ISD::ADDE, DL, DAG.getVTList(MVT::i32, MVT::Glue),
827 PtrHi, DAG.getConstant(0, MVT::i32),
828 SDValue(Lo.getNode(), 1));
829 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
830}
831
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000832SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
833 SelectionDAG &DAG) const {
834 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000835 const SIRegisterInfo *TRI =
836 static_cast<const SIRegisterInfo*>(MF.getSubtarget().getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000837
838 EVT VT = Op.getValueType();
839 SDLoc DL(Op);
840 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
841
842 switch (IntrinsicID) {
843 case Intrinsic::r600_read_ngroups_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000844 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
845 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000846 case Intrinsic::r600_read_ngroups_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000847 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
848 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000849 case Intrinsic::r600_read_ngroups_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000850 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
851 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000852 case Intrinsic::r600_read_global_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000853 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
854 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000855 case Intrinsic::r600_read_global_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000856 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
857 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000858 case Intrinsic::r600_read_global_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000859 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
860 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000861 case Intrinsic::r600_read_local_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000862 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
863 SI::KernelInputOffsets::LOCAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000864 case Intrinsic::r600_read_local_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000865 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
866 SI::KernelInputOffsets::LOCAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000867 case Intrinsic::r600_read_local_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +0000868 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
869 SI::KernelInputOffsets::LOCAL_SIZE_Z, false);
Jan Veselye5121f32014-10-14 20:05:26 +0000870
871 case Intrinsic::AMDGPU_read_workdim:
872 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
873 MF.getInfo<SIMachineFunctionInfo>()->ABIArgOffset,
874 false);
875
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000876 case Intrinsic::r600_read_tgid_x:
877 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000878 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000879 case Intrinsic::r600_read_tgid_y:
880 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000881 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000882 case Intrinsic::r600_read_tgid_z:
883 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000884 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000885 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000886 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000887 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000888 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000889 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000890 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000891 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +0000892 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +0000893 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000894 case AMDGPUIntrinsic::SI_load_const: {
895 SDValue Ops[] = {
896 Op.getOperand(1),
897 Op.getOperand(2)
898 };
899
900 MachineMemOperand *MMO = MF.getMachineMemOperand(
901 MachinePointerInfo(),
902 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
903 VT.getStoreSize(), 4);
904 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
905 Op->getVTList(), Ops, VT, MMO);
906 }
907 case AMDGPUIntrinsic::SI_sample:
908 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
909 case AMDGPUIntrinsic::SI_sampleb:
910 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
911 case AMDGPUIntrinsic::SI_sampled:
912 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
913 case AMDGPUIntrinsic::SI_samplel:
914 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
915 case AMDGPUIntrinsic::SI_vs_load_input:
916 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
917 Op.getOperand(1),
918 Op.getOperand(2),
919 Op.getOperand(3));
920 default:
921 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
922 }
923}
924
925SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
926 SelectionDAG &DAG) const {
927 MachineFunction &MF = DAG.getMachineFunction();
928 SDValue Chain = Op.getOperand(0);
929 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
930
931 switch (IntrinsicID) {
932 case AMDGPUIntrinsic::SI_tbuffer_store: {
933 SDLoc DL(Op);
934 SDValue Ops[] = {
935 Chain,
936 Op.getOperand(2),
937 Op.getOperand(3),
938 Op.getOperand(4),
939 Op.getOperand(5),
940 Op.getOperand(6),
941 Op.getOperand(7),
942 Op.getOperand(8),
943 Op.getOperand(9),
944 Op.getOperand(10),
945 Op.getOperand(11),
946 Op.getOperand(12),
947 Op.getOperand(13),
948 Op.getOperand(14)
949 };
950
951 EVT VT = Op.getOperand(3).getValueType();
952
953 MachineMemOperand *MMO = MF.getMachineMemOperand(
954 MachinePointerInfo(),
955 MachineMemOperand::MOStore,
956 VT.getStoreSize(), 4);
957 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
958 Op->getVTList(), Ops, VT, MMO);
959 }
960 default:
961 return SDValue();
962 }
963}
964
Tom Stellard81d871d2013-11-13 23:36:50 +0000965SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
966 SDLoc DL(Op);
967 LoadSDNode *Load = cast<LoadSDNode>(Op);
968
Tom Stellarde812f2f2014-07-21 15:45:06 +0000969 if (Op.getValueType().isVector()) {
970 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
971 "Custom lowering for non-i32 vectors hasn't been implemented.");
972 unsigned NumElements = Op.getValueType().getVectorNumElements();
973 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
974 switch (Load->getAddressSpace()) {
975 default: break;
976 case AMDGPUAS::GLOBAL_ADDRESS:
977 case AMDGPUAS::PRIVATE_ADDRESS:
978 // v4 loads are supported for private and global memory.
979 if (NumElements <= 4)
980 break;
981 // fall-through
982 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenault83e60582014-07-24 17:10:35 +0000983 return ScalarizeVectorLoad(Op, DAG);
Tom Stellarde812f2f2014-07-21 15:45:06 +0000984 }
Tom Stellarde9373602014-01-22 19:24:14 +0000985 }
Tom Stellard81d871d2013-11-13 23:36:50 +0000986
Tom Stellarde812f2f2014-07-21 15:45:06 +0000987 return AMDGPUTargetLowering::LowerLOAD(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000988}
989
Tom Stellard9fa17912013-08-14 23:24:45 +0000990SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
991 const SDValue &Op,
992 SelectionDAG &DAG) const {
993 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
994 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +0000995 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +0000996 Op.getOperand(4));
997}
998
Tom Stellard0ec134f2014-02-04 17:18:40 +0000999SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1000 if (Op.getValueType() != MVT::i64)
1001 return SDValue();
1002
1003 SDLoc DL(Op);
1004 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001005
1006 SDValue Zero = DAG.getConstant(0, MVT::i32);
1007 SDValue One = DAG.getConstant(1, MVT::i32);
1008
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001009 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1010 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1011
1012 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1013 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001014
1015 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1016
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001017 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1018 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001019
1020 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1021
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001022 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1023 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001024}
1025
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001026// Catch division cases where we can use shortcuts with rcp and rsq
1027// instructions.
1028SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001029 SDLoc SL(Op);
1030 SDValue LHS = Op.getOperand(0);
1031 SDValue RHS = Op.getOperand(1);
1032 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001033 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001034
1035 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001036 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1037 CLHS->isExactlyValue(1.0)) {
1038 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1039 // the CI documentation has a worst case error of 1 ulp.
1040 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1041 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001042
1043 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001044 //
1045 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1046 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001047 if (RHS.getOpcode() == ISD::FSQRT)
1048 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1049
1050 // 1.0 / x -> rcp(x)
1051 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1052 }
1053 }
1054
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001055 if (Unsafe) {
1056 // Turn into multiply by the reciprocal.
1057 // x / y -> x * (1.0 / y)
1058 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1059 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip);
1060 }
1061
1062 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001063}
1064
1065SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001066 SDValue FastLowered = LowerFastFDIV(Op, DAG);
1067 if (FastLowered.getNode())
1068 return FastLowered;
1069
1070 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1071 // selection error for now rather than do something incorrect.
1072 if (Subtarget->hasFP32Denormals())
1073 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001074
1075 SDLoc SL(Op);
1076 SDValue LHS = Op.getOperand(0);
1077 SDValue RHS = Op.getOperand(1);
1078
1079 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1080
1081 const APFloat K0Val(BitsToFloat(0x6f800000));
1082 const SDValue K0 = DAG.getConstantFP(K0Val, MVT::f32);
1083
1084 const APFloat K1Val(BitsToFloat(0x2f800000));
1085 const SDValue K1 = DAG.getConstantFP(K1Val, MVT::f32);
1086
1087 const SDValue One = DAG.getTargetConstantFP(1.0, MVT::f32);
1088
1089 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f32);
1090
1091 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1092
1093 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1094
1095 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1096
1097 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1098
1099 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1100
1101 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1102}
1103
1104SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
1105 return SDValue();
1106}
1107
1108SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1109 EVT VT = Op.getValueType();
1110
1111 if (VT == MVT::f32)
1112 return LowerFDIV32(Op, DAG);
1113
1114 if (VT == MVT::f64)
1115 return LowerFDIV64(Op, DAG);
1116
1117 llvm_unreachable("Unexpected type for fdiv");
1118}
1119
Tom Stellard81d871d2013-11-13 23:36:50 +00001120SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1121 SDLoc DL(Op);
1122 StoreSDNode *Store = cast<StoreSDNode>(Op);
1123 EVT VT = Store->getMemoryVT();
1124
Tom Stellard9b3816b2014-06-24 23:33:04 +00001125 // These stores are legal.
1126 if (Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
1127 VT.isVector() && VT.getVectorNumElements() == 2 &&
1128 VT.getVectorElementType() == MVT::i32)
1129 return SDValue();
1130
Tom Stellardb02094e2014-07-21 15:45:01 +00001131 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1132 if (VT.isVector() && VT.getVectorNumElements() > 4)
Matt Arsenault83e60582014-07-24 17:10:35 +00001133 return ScalarizeVectorStore(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001134 return SDValue();
1135 }
1136
Tom Stellard81d871d2013-11-13 23:36:50 +00001137 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1138 if (Ret.getNode())
1139 return Ret;
1140
1141 if (VT.isVector() && VT.getVectorNumElements() >= 8)
Matt Arsenault83e60582014-07-24 17:10:35 +00001142 return ScalarizeVectorStore(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001143
Tom Stellard1c8788e2014-03-07 20:12:33 +00001144 if (VT == MVT::i1)
1145 return DAG.getTruncStore(Store->getChain(), DL,
1146 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1147 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1148
Tom Stellarde812f2f2014-07-21 15:45:06 +00001149 return SDValue();
Tom Stellard81d871d2013-11-13 23:36:50 +00001150}
1151
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001152SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
1153 EVT VT = Op.getValueType();
1154 SDValue Arg = Op.getOperand(0);
1155 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
1156 DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
1157 DAG.getConstantFP(0.5 / M_PI, VT)));
1158
1159 switch (Op.getOpcode()) {
1160 case ISD::FCOS:
1161 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1162 case ISD::FSIN:
1163 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1164 default:
1165 llvm_unreachable("Wrong trig opcode");
1166 }
1167}
1168
Tom Stellard75aadc22012-12-11 21:25:42 +00001169//===----------------------------------------------------------------------===//
1170// Custom DAG optimizations
1171//===----------------------------------------------------------------------===//
1172
Matt Arsenault364a6742014-06-11 17:50:44 +00001173SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
1174 DAGCombinerInfo &DCI) {
1175 EVT VT = N->getValueType(0);
1176 EVT ScalarVT = VT.getScalarType();
1177 if (ScalarVT != MVT::f32)
1178 return SDValue();
1179
1180 SelectionDAG &DAG = DCI.DAG;
1181 SDLoc DL(N);
1182
1183 SDValue Src = N->getOperand(0);
1184 EVT SrcVT = Src.getValueType();
1185
1186 // TODO: We could try to match extracting the higher bytes, which would be
1187 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1188 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1189 // about in practice.
1190 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1191 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1192 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1193 DCI.AddToWorklist(Cvt.getNode());
1194 return Cvt;
1195 }
1196 }
1197
1198 // We are primarily trying to catch operations on illegal vector types
1199 // before they are expanded.
1200 // For scalars, we can use the more flexible method of checking masked bits
1201 // after legalization.
1202 if (!DCI.isBeforeLegalize() ||
1203 !SrcVT.isVector() ||
1204 SrcVT.getVectorElementType() != MVT::i8) {
1205 return SDValue();
1206 }
1207
1208 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1209
1210 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1211 // size as 4.
1212 unsigned NElts = SrcVT.getVectorNumElements();
1213 if (!SrcVT.isSimple() && NElts != 3)
1214 return SDValue();
1215
1216 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1217 // prevent a mess from expanding to v4i32 and repacking.
1218 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1219 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1220 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1221 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
1222
1223 LoadSDNode *Load = cast<LoadSDNode>(Src);
1224 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1225 Load->getChain(),
1226 Load->getBasePtr(),
1227 LoadVT,
1228 Load->getMemOperand());
1229
1230 // Make sure successors of the original load stay after it by updating
1231 // them to use the new Chain.
1232 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1233
1234 SmallVector<SDValue, 4> Elts;
1235 if (RegVT.isVector())
1236 DAG.ExtractVectorElements(NewLoad, Elts);
1237 else
1238 Elts.push_back(NewLoad);
1239
1240 SmallVector<SDValue, 4> Ops;
1241
1242 unsigned EltIdx = 0;
1243 for (SDValue Elt : Elts) {
1244 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1245 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1246 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1247 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1248 DCI.AddToWorklist(Cvt.getNode());
1249 Ops.push_back(Cvt);
1250 }
1251
1252 ++EltIdx;
1253 }
1254
1255 assert(Ops.size() == NElts);
1256
1257 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1258 }
1259
1260 return SDValue();
1261}
1262
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001263// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
1264
1265// This is a variant of
1266// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
1267//
1268// The normal DAG combiner will do this, but only if the add has one use since
1269// that would increase the number of instructions.
1270//
1271// This prevents us from seeing a constant offset that can be folded into a
1272// memory instruction's addressing mode. If we know the resulting add offset of
1273// a pointer can be folded into an addressing offset, we can replace the pointer
1274// operand with the add of new constant offset. This eliminates one of the uses,
1275// and may allow the remaining use to also be simplified.
1276//
1277SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
1278 unsigned AddrSpace,
1279 DAGCombinerInfo &DCI) const {
1280 SDValue N0 = N->getOperand(0);
1281 SDValue N1 = N->getOperand(1);
1282
1283 if (N0.getOpcode() != ISD::ADD)
1284 return SDValue();
1285
1286 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
1287 if (!CN1)
1288 return SDValue();
1289
1290 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1291 if (!CAdd)
1292 return SDValue();
1293
1294 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1295 getTargetMachine().getSubtargetImpl()->getInstrInfo());
1296
1297 // If the resulting offset is too large, we can't fold it into the addressing
1298 // mode offset.
1299 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
1300 if (!TII->canFoldOffset(Offset.getZExtValue(), AddrSpace))
1301 return SDValue();
1302
1303 SelectionDAG &DAG = DCI.DAG;
1304 SDLoc SL(N);
1305 EVT VT = N->getValueType(0);
1306
1307 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
1308 SDValue COffset = DAG.getConstant(Offset, MVT::i32);
1309
1310 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
1311}
1312
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001313SDValue SITargetLowering::performAndCombine(SDNode *N,
1314 DAGCombinerInfo &DCI) const {
1315 if (DCI.isBeforeLegalize())
1316 return SDValue();
1317
1318 SelectionDAG &DAG = DCI.DAG;
1319
1320 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
1321 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
1322 SDValue LHS = N->getOperand(0);
1323 SDValue RHS = N->getOperand(1);
1324
1325 if (LHS.getOpcode() == ISD::SETCC &&
1326 RHS.getOpcode() == ISD::SETCC) {
1327 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
1328 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
1329
1330 SDValue X = LHS.getOperand(0);
1331 SDValue Y = RHS.getOperand(0);
1332 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
1333 return SDValue();
1334
1335 if (LCC == ISD::SETO) {
1336 if (X != LHS.getOperand(1))
1337 return SDValue();
1338
1339 if (RCC == ISD::SETUNE) {
1340 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
1341 if (!C1 || !C1->isInfinity() || C1->isNegative())
1342 return SDValue();
1343
1344 const uint32_t Mask = SIInstrFlags::N_NORMAL |
1345 SIInstrFlags::N_SUBNORMAL |
1346 SIInstrFlags::N_ZERO |
1347 SIInstrFlags::P_ZERO |
1348 SIInstrFlags::P_SUBNORMAL |
1349 SIInstrFlags::P_NORMAL;
1350
1351 static_assert(((~(SIInstrFlags::S_NAN |
1352 SIInstrFlags::Q_NAN |
1353 SIInstrFlags::N_INFINITY |
1354 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
1355 "mask not equal");
1356
1357 return DAG.getNode(AMDGPUISD::FP_CLASS, SDLoc(N), MVT::i1,
1358 X, DAG.getConstant(Mask, MVT::i32));
1359 }
1360 }
1361 }
1362
1363 return SDValue();
1364}
1365
Matt Arsenaultf2290332015-01-06 23:00:39 +00001366SDValue SITargetLowering::performOrCombine(SDNode *N,
1367 DAGCombinerInfo &DCI) const {
1368 SelectionDAG &DAG = DCI.DAG;
1369 SDValue LHS = N->getOperand(0);
1370 SDValue RHS = N->getOperand(1);
1371
1372 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
1373 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
1374 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
1375 SDValue Src = LHS.getOperand(0);
1376 if (Src != RHS.getOperand(0))
1377 return SDValue();
1378
1379 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
1380 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
1381 if (!CLHS || !CRHS)
1382 return SDValue();
1383
1384 // Only 10 bits are used.
1385 static const uint32_t MaxMask = 0x3ff;
1386
1387 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
1388 return DAG.getNode(AMDGPUISD::FP_CLASS, SDLoc(N), MVT::i1,
1389 Src, DAG.getConstant(NewMask, MVT::i32));
1390 }
1391
1392 return SDValue();
1393}
1394
1395SDValue SITargetLowering::performClassCombine(SDNode *N,
1396 DAGCombinerInfo &DCI) const {
1397 SelectionDAG &DAG = DCI.DAG;
1398 SDValue Mask = N->getOperand(1);
1399
1400 // fp_class x, 0 -> false
1401 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
1402 if (CMask->isNullValue())
1403 return DAG.getConstant(0, MVT::i1);
1404 }
1405
1406 return SDValue();
1407}
1408
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001409static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
1410 switch (Opc) {
1411 case ISD::FMAXNUM:
1412 return AMDGPUISD::FMAX3;
1413 case AMDGPUISD::SMAX:
1414 return AMDGPUISD::SMAX3;
1415 case AMDGPUISD::UMAX:
1416 return AMDGPUISD::UMAX3;
1417 case ISD::FMINNUM:
1418 return AMDGPUISD::FMIN3;
1419 case AMDGPUISD::SMIN:
1420 return AMDGPUISD::SMIN3;
1421 case AMDGPUISD::UMIN:
1422 return AMDGPUISD::UMIN3;
1423 default:
1424 llvm_unreachable("Not a min/max opcode");
1425 }
1426}
1427
1428SDValue SITargetLowering::performMin3Max3Combine(SDNode *N,
1429 DAGCombinerInfo &DCI) const {
1430 SelectionDAG &DAG = DCI.DAG;
1431
1432 unsigned Opc = N->getOpcode();
1433 SDValue Op0 = N->getOperand(0);
1434 SDValue Op1 = N->getOperand(1);
1435
1436 // Only do this if the inner op has one use since this will just increases
1437 // register pressure for no benefit.
1438
1439 // max(max(a, b), c)
1440 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
1441 SDLoc DL(N);
1442 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1443 DL,
1444 N->getValueType(0),
1445 Op0.getOperand(0),
1446 Op0.getOperand(1),
1447 Op1);
1448 }
1449
1450 // max(a, max(b, c))
1451 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
1452 SDLoc DL(N);
1453 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1454 DL,
1455 N->getValueType(0),
1456 Op0,
1457 Op1.getOperand(0),
1458 Op1.getOperand(1));
1459 }
1460
1461 return SDValue();
1462}
1463
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001464SDValue SITargetLowering::performSetCCCombine(SDNode *N,
1465 DAGCombinerInfo &DCI) const {
1466 SelectionDAG &DAG = DCI.DAG;
1467 SDLoc SL(N);
1468
1469 SDValue LHS = N->getOperand(0);
1470 SDValue RHS = N->getOperand(1);
1471 EVT VT = LHS.getValueType();
1472
1473 if (VT != MVT::f32 && VT != MVT::f64)
1474 return SDValue();
1475
1476 // Match isinf pattern
1477 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
1478 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
1479 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
1480 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
1481 if (!CRHS)
1482 return SDValue();
1483
1484 const APFloat &APF = CRHS->getValueAPF();
1485 if (APF.isInfinity() && !APF.isNegative()) {
1486 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
1487 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1,
1488 LHS.getOperand(0), DAG.getConstant(Mask, MVT::i32));
1489 }
1490 }
1491
1492 return SDValue();
1493}
1494
Tom Stellard75aadc22012-12-11 21:25:42 +00001495SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1496 DAGCombinerInfo &DCI) const {
1497 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001498 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001499
1500 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00001501 default:
1502 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001503 case ISD::SETCC:
1504 return performSetCCCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001505 case ISD::FMAXNUM: // TODO: What about fmax_legacy?
1506 case ISD::FMINNUM:
1507 case AMDGPUISD::SMAX:
1508 case AMDGPUISD::SMIN:
1509 case AMDGPUISD::UMAX:
1510 case AMDGPUISD::UMIN: {
1511 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
1512 getTargetMachine().getOptLevel() > CodeGenOpt::None)
1513 return performMin3Max3Combine(N, DCI);
1514 break;
1515 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001516
1517 case AMDGPUISD::CVT_F32_UBYTE0:
1518 case AMDGPUISD::CVT_F32_UBYTE1:
1519 case AMDGPUISD::CVT_F32_UBYTE2:
1520 case AMDGPUISD::CVT_F32_UBYTE3: {
1521 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
1522
1523 SDValue Src = N->getOperand(0);
1524 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
1525
1526 APInt KnownZero, KnownOne;
1527 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1528 !DCI.isBeforeLegalizeOps());
1529 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1530 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
1531 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
1532 DCI.CommitTargetLoweringOpt(TLO);
1533 }
1534
1535 break;
1536 }
1537
1538 case ISD::UINT_TO_FP: {
1539 return performUCharToFloatCombine(N, DCI);
Matt Arsenault8675db12014-08-29 16:01:14 +00001540
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001541 case ISD::FADD: {
1542 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1543 break;
1544
1545 EVT VT = N->getValueType(0);
1546 if (VT != MVT::f32)
1547 break;
1548
1549 SDValue LHS = N->getOperand(0);
1550 SDValue RHS = N->getOperand(1);
1551
1552 // These should really be instruction patterns, but writing patterns with
1553 // source modiifiers is a pain.
1554
1555 // fadd (fadd (a, a), b) -> mad 2.0, a, b
1556 if (LHS.getOpcode() == ISD::FADD) {
1557 SDValue A = LHS.getOperand(0);
1558 if (A == LHS.getOperand(1)) {
1559 const SDValue Two = DAG.getTargetConstantFP(2.0, MVT::f32);
1560 return DAG.getNode(AMDGPUISD::MAD, DL, VT, Two, A, RHS);
1561 }
1562 }
1563
1564 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
1565 if (RHS.getOpcode() == ISD::FADD) {
1566 SDValue A = RHS.getOperand(0);
1567 if (A == RHS.getOperand(1)) {
1568 const SDValue Two = DAG.getTargetConstantFP(2.0, MVT::f32);
1569 return DAG.getNode(AMDGPUISD::MAD, DL, VT, Two, A, LHS);
1570 }
1571 }
1572
1573 break;
1574 }
Matt Arsenault8675db12014-08-29 16:01:14 +00001575 case ISD::FSUB: {
1576 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1577 break;
1578
1579 EVT VT = N->getValueType(0);
1580
1581 // Try to get the fneg to fold into the source modifier. This undoes generic
1582 // DAG combines and folds them into the mad.
1583 if (VT == MVT::f32) {
1584 SDValue LHS = N->getOperand(0);
1585 SDValue RHS = N->getOperand(1);
1586
1587 if (LHS.getOpcode() == ISD::FMUL) {
1588 // (fsub (fmul a, b), c) -> mad a, b, (fneg c)
1589
1590 SDValue A = LHS.getOperand(0);
1591 SDValue B = LHS.getOperand(1);
1592 SDValue C = DAG.getNode(ISD::FNEG, DL, VT, RHS);
1593
1594 return DAG.getNode(AMDGPUISD::MAD, DL, VT, A, B, C);
1595 }
1596
1597 if (RHS.getOpcode() == ISD::FMUL) {
1598 // (fsub c, (fmul a, b)) -> mad (fneg a), b, c
1599
1600 SDValue A = DAG.getNode(ISD::FNEG, DL, VT, RHS.getOperand(0));
1601 SDValue B = RHS.getOperand(1);
1602 SDValue C = LHS;
1603
1604 return DAG.getNode(AMDGPUISD::MAD, DL, VT, A, B, C);
1605 }
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001606
1607 if (LHS.getOpcode() == ISD::FADD) {
1608 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
1609
1610 SDValue A = LHS.getOperand(0);
1611 if (A == LHS.getOperand(1)) {
1612 const SDValue Two = DAG.getTargetConstantFP(2.0, MVT::f32);
1613 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
1614
1615 return DAG.getNode(AMDGPUISD::MAD, DL, VT, Two, A, NegRHS);
1616 }
1617 }
1618
1619 if (RHS.getOpcode() == ISD::FADD) {
1620 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
1621
1622 SDValue A = RHS.getOperand(0);
1623 if (A == RHS.getOperand(1)) {
1624 const SDValue NegTwo = DAG.getTargetConstantFP(-2.0, MVT::f32);
1625 return DAG.getNode(AMDGPUISD::MAD, DL, VT, NegTwo, A, LHS);
1626 }
1627 }
Matt Arsenault8675db12014-08-29 16:01:14 +00001628 }
1629
1630 break;
1631 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001632 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001633 case ISD::LOAD:
1634 case ISD::STORE:
1635 case ISD::ATOMIC_LOAD:
1636 case ISD::ATOMIC_STORE:
1637 case ISD::ATOMIC_CMP_SWAP:
1638 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
1639 case ISD::ATOMIC_SWAP:
1640 case ISD::ATOMIC_LOAD_ADD:
1641 case ISD::ATOMIC_LOAD_SUB:
1642 case ISD::ATOMIC_LOAD_AND:
1643 case ISD::ATOMIC_LOAD_OR:
1644 case ISD::ATOMIC_LOAD_XOR:
1645 case ISD::ATOMIC_LOAD_NAND:
1646 case ISD::ATOMIC_LOAD_MIN:
1647 case ISD::ATOMIC_LOAD_MAX:
1648 case ISD::ATOMIC_LOAD_UMIN:
1649 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
1650 if (DCI.isBeforeLegalize())
1651 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001652
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001653 MemSDNode *MemNode = cast<MemSDNode>(N);
1654 SDValue Ptr = MemNode->getBasePtr();
1655
1656 // TODO: We could also do this for multiplies.
1657 unsigned AS = MemNode->getAddressSpace();
1658 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
1659 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
1660 if (NewPtr) {
1661 SmallVector<SDValue, 8> NewOps;
Aaron Ballmanf12dc9c2014-08-18 11:51:41 +00001662 for (unsigned I = 0, E = MemNode->getNumOperands(); I != E; ++I)
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001663 NewOps.push_back(MemNode->getOperand(I));
1664
1665 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
1666 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
1667 }
1668 }
1669 break;
1670 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001671 case ISD::AND:
1672 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00001673 case ISD::OR:
1674 return performOrCombine(N, DCI);
1675 case AMDGPUISD::FP_CLASS:
1676 return performClassCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001677 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00001678 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00001679}
Christian Konigd910b7d2013-02-26 17:52:16 +00001680
Matt Arsenault758659232013-05-18 00:21:46 +00001681/// \brief Test if RegClass is one of the VSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001682static bool isVSrc(unsigned RegClass) {
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001683 switch(RegClass) {
1684 default: return false;
1685 case AMDGPU::VSrc_32RegClassID:
1686 case AMDGPU::VCSrc_32RegClassID:
1687 case AMDGPU::VSrc_64RegClassID:
1688 case AMDGPU::VCSrc_64RegClassID:
1689 return true;
1690 }
Christian Konigf82901a2013-02-26 17:52:23 +00001691}
1692
Matt Arsenault758659232013-05-18 00:21:46 +00001693/// \brief Test if RegClass is one of the SSrc classes
Christian Konigf82901a2013-02-26 17:52:23 +00001694static bool isSSrc(unsigned RegClass) {
1695 return AMDGPU::SSrc_32RegClassID == RegClass ||
1696 AMDGPU::SSrc_64RegClassID == RegClass;
1697}
1698
1699/// \brief Analyze the possible immediate value Op
1700///
1701/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1702/// and the immediate value if it's a literal immediate
1703int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1704
Matt Arsenault303011a2014-12-17 21:04:08 +00001705 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1706 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001707
Tom Stellardedbf1eb2013-04-05 23:31:20 +00001708 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00001709 if (Node->getZExtValue() >> 32)
Tom Stellard7ed0b522014-04-03 20:19:27 +00001710 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001711
Matt Arsenault303011a2014-12-17 21:04:08 +00001712 if (TII->isInlineConstant(Node->getAPIntValue()))
1713 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00001714
Matt Arsenault303011a2014-12-17 21:04:08 +00001715 return Node->getZExtValue();
1716 }
1717
1718 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1719 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
1720 return 0;
1721
1722 if (Node->getValueType(0) == MVT::f32)
1723 return FloatToBits(Node->getValueAPF().convertToFloat());
1724
1725 return -1;
1726 }
1727
1728 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00001729}
1730
1731/// \brief Try to fold an immediate directly into an instruction
1732bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
1733 bool &ScalarSlotUsed) const {
1734
1735 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
Eric Christopherd9134482014-08-04 21:25:23 +00001736 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1737 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Craig Topper062a2ba2014-04-25 05:30:21 +00001738 if (!Mov || !TII->isMov(Mov->getMachineOpcode()))
Christian Konigf82901a2013-02-26 17:52:23 +00001739 return false;
1740
1741 const SDValue &Op = Mov->getOperand(0);
1742 int32_t Value = analyzeImmediate(Op.getNode());
1743 if (Value == -1) {
1744 // Not an immediate at all
1745 return false;
1746
1747 } else if (Value == 0) {
1748 // Inline immediates can always be fold
1749 Operand = Op;
1750 return true;
1751
1752 } else if (Value == Immediate) {
1753 // Already fold literal immediate
1754 Operand = Op;
1755 return true;
1756
1757 } else if (!ScalarSlotUsed && !Immediate) {
1758 // Fold this literal immediate
1759 ScalarSlotUsed = true;
1760 Immediate = Value;
1761 Operand = Op;
1762 return true;
1763
1764 }
1765
1766 return false;
1767}
1768
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001769const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1770 SelectionDAG &DAG, const SDValue &Op) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001771 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1772 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001773 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1774
1775 if (!Op->isMachineOpcode()) {
1776 switch(Op->getOpcode()) {
1777 case ISD::CopyFromReg: {
1778 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1779 unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1780 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1781 return MRI.getRegClass(Reg);
1782 }
1783 return TRI.getPhysRegClass(Reg);
1784 }
Craig Topper062a2ba2014-04-25 05:30:21 +00001785 default: return nullptr;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001786 }
1787 }
1788 const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1789 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1790 if (OpClassID != -1) {
1791 return TRI.getRegClass(OpClassID);
1792 }
1793 switch(Op.getMachineOpcode()) {
1794 case AMDGPU::COPY_TO_REGCLASS:
1795 // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1796 OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1797
1798 // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1799 // class, then the register class for the value could be either a
1800 // VReg or and SReg. In order to get a more accurate
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001801 if (isVSrc(OpClassID))
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001802 return getRegClassForNode(DAG, Op.getOperand(0));
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001803
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001804 return TRI.getRegClass(OpClassID);
1805 case AMDGPU::EXTRACT_SUBREG: {
1806 int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1807 const TargetRegisterClass *SuperClass =
1808 getRegClassForNode(DAG, Op.getOperand(0));
1809 return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1810 }
1811 case AMDGPU::REG_SEQUENCE:
1812 // Operand 0 is the register class id for REG_SEQUENCE instructions.
1813 return TRI.getRegClass(
1814 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1815 default:
1816 return getRegClassFor(Op.getSimpleValueType());
1817 }
1818}
1819
Christian Konigf82901a2013-02-26 17:52:23 +00001820/// \brief Does "Op" fit into register class "RegClass" ?
Tom Stellardb35efba2013-05-20 15:02:01 +00001821bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
Christian Konigf82901a2013-02-26 17:52:23 +00001822 unsigned RegClass) const {
Eric Christopherd9134482014-08-04 21:25:23 +00001823 const TargetRegisterInfo *TRI =
1824 getTargetMachine().getSubtargetImpl()->getRegisterInfo();
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001825 const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1826 if (!RC) {
Christian Konigf82901a2013-02-26 17:52:23 +00001827 return false;
Tom Stellard4c0ffcc2013-08-06 23:08:18 +00001828 }
1829 return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
Christian Konigf82901a2013-02-26 17:52:23 +00001830}
1831
Tom Stellardacec99c2013-06-05 23:39:50 +00001832/// \returns true if \p Node's operands are different from the SDValue list
1833/// \p Ops
1834static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) {
1835 for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
1836 if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
1837 return true;
1838 }
1839 }
1840 return false;
1841}
1842
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00001843/// TODO: This needs to be removed. It's current primary purpose is to fold
1844/// immediates into operands when legal. The legalization parts are redundant
1845/// with SIInstrInfo::legalizeOperands which is called in a post-isel hook.
Matt Arsenault253e5da2014-09-17 15:35:43 +00001846SDNode *SITargetLowering::legalizeOperands(MachineSDNode *Node,
1847 SelectionDAG &DAG) const {
Christian Konigf82901a2013-02-26 17:52:23 +00001848 // Original encoding (either e32 or e64)
1849 int Opcode = Node->getMachineOpcode();
Eric Christopherd9134482014-08-04 21:25:23 +00001850 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
1851 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00001852 const MCInstrDesc *Desc = &TII->get(Opcode);
1853
1854 unsigned NumDefs = Desc->getNumDefs();
1855 unsigned NumOps = Desc->getNumOperands();
1856
Christian Konig3c145802013-03-27 09:12:59 +00001857 // Commuted opcode if available
1858 int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
Craig Topper062a2ba2014-04-25 05:30:21 +00001859 const MCInstrDesc *DescRev = OpcodeRev == -1 ? nullptr : &TII->get(OpcodeRev);
Christian Konig3c145802013-03-27 09:12:59 +00001860
1861 assert(!DescRev || DescRev->getNumDefs() == NumDefs);
1862 assert(!DescRev || DescRev->getNumOperands() == NumOps);
1863
Christian Konigf82901a2013-02-26 17:52:23 +00001864 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
1865 bool HaveVSrc = false, HaveSSrc = false;
1866
Matt Arsenault08d84942014-06-03 23:06:13 +00001867 // First figure out what we already have in this instruction.
Christian Konigf82901a2013-02-26 17:52:23 +00001868 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1869 i != e && Op < NumOps; ++i, ++Op) {
1870
1871 unsigned RegClass = Desc->OpInfo[Op].RegClass;
1872 if (isVSrc(RegClass))
1873 HaveVSrc = true;
1874 else if (isSSrc(RegClass))
1875 HaveSSrc = true;
1876 else
1877 continue;
1878
1879 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
1880 if (Imm != -1 && Imm != 0) {
1881 // Literal immediate
1882 Immediate = Imm;
1883 }
1884 }
1885
Matt Arsenault08d84942014-06-03 23:06:13 +00001886 // If we neither have VSrc nor SSrc, it makes no sense to continue.
Christian Konigf82901a2013-02-26 17:52:23 +00001887 if (!HaveVSrc && !HaveSSrc)
1888 return Node;
1889
1890 // No scalar allowed when we have both VSrc and SSrc
1891 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
1892
Tom Stellard73ae1cb2014-09-23 21:26:25 +00001893 // If this instruction has an implicit use of VCC, then it can't use the
1894 // constant bus.
1895 for (unsigned i = 0, e = Desc->getNumImplicitUses(); i != e; ++i) {
1896 if (Desc->ImplicitUses[i] == AMDGPU::VCC) {
1897 ScalarSlotUsed = true;
1898 break;
1899 }
1900 }
1901
Christian Konigf82901a2013-02-26 17:52:23 +00001902 // Second go over the operands and try to fold them
1903 std::vector<SDValue> Ops;
1904 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1905 i != e && Op < NumOps; ++i, ++Op) {
1906
1907 const SDValue &Operand = Node->getOperand(i);
1908 Ops.push_back(Operand);
1909
Matt Arsenault08d84942014-06-03 23:06:13 +00001910 // Already folded immediate?
Christian Konigf82901a2013-02-26 17:52:23 +00001911 if (isa<ConstantSDNode>(Operand.getNode()) ||
1912 isa<ConstantFPSDNode>(Operand.getNode()))
1913 continue;
1914
Matt Arsenault08d84942014-06-03 23:06:13 +00001915 // Is this a VSrc or SSrc operand?
Christian Konigf82901a2013-02-26 17:52:23 +00001916 unsigned RegClass = Desc->OpInfo[Op].RegClass;
Christian Konig8370dbb2013-03-26 14:04:17 +00001917 if (isVSrc(RegClass) || isSSrc(RegClass)) {
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00001918 // Try to fold the immediates. If this ends up with multiple constant bus
1919 // uses, it will be legalized later.
1920 foldImm(Ops[i], Immediate, ScalarSlotUsed);
Christian Konig8370dbb2013-03-26 14:04:17 +00001921 continue;
1922 }
Christian Konig6612ac32013-02-26 17:52:36 +00001923
Christian Konig3c145802013-03-27 09:12:59 +00001924 if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
Christian Konig6612ac32013-02-26 17:52:36 +00001925
Christian Konig8370dbb2013-03-26 14:04:17 +00001926 unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
1927 assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
1928
1929 // Test if it makes sense to swap operands
1930 if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
1931 (!fitsRegClass(DAG, Ops[1], RegClass) &&
1932 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
Christian Konig6612ac32013-02-26 17:52:36 +00001933
1934 // Swap commutable operands
Matt Arsenault4be76e92014-04-07 16:44:26 +00001935 std::swap(Ops[0], Ops[1]);
Christian Konig3c145802013-03-27 09:12:59 +00001936
1937 Desc = DescRev;
Craig Topper062a2ba2014-04-25 05:30:21 +00001938 DescRev = nullptr;
Christian Konig8370dbb2013-03-26 14:04:17 +00001939 continue;
Christian Konig6612ac32013-02-26 17:52:36 +00001940 }
Christian Konig6612ac32013-02-26 17:52:36 +00001941 }
Christian Konige500e442013-02-26 17:52:47 +00001942 }
1943
Christian Konigf82901a2013-02-26 17:52:23 +00001944 // Add optional chain and glue
1945 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
1946 Ops.push_back(Node->getOperand(i));
1947
Tom Stellardb5a97002013-06-03 17:39:50 +00001948 // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
1949 // this case a brand new node is always be created, even if the operands
1950 // are the same as before. So, manually check if anything has been changed.
Tom Stellardacec99c2013-06-05 23:39:50 +00001951 if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) {
1952 return Node;
Tom Stellardb5a97002013-06-03 17:39:50 +00001953 }
1954
Christian Konig3c145802013-03-27 09:12:59 +00001955 // Create a complete new instruction
Andrew Trickef9de2a2013-05-25 02:42:55 +00001956 return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
Christian Konigd910b7d2013-02-26 17:52:16 +00001957}
Christian Konig8e06e2a2013-04-10 08:39:08 +00001958
1959/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00001960static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00001961 switch (Idx) {
1962 default: return 0;
1963 case AMDGPU::sub0: return 0;
1964 case AMDGPU::sub1: return 1;
1965 case AMDGPU::sub2: return 2;
1966 case AMDGPU::sub3: return 3;
1967 }
1968}
1969
1970/// \brief Adjust the writemask of MIMG instructions
1971void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1972 SelectionDAG &DAG) const {
1973 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00001974 unsigned Lane = 0;
1975 unsigned OldDmask = Node->getConstantOperandVal(0);
1976 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00001977
1978 // Try to figure out the used register components
1979 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1980 I != E; ++I) {
1981
1982 // Abort if we can't understand the usage
1983 if (!I->isMachineOpcode() ||
1984 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1985 return;
1986
Tom Stellard54774e52013-10-23 02:53:47 +00001987 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1988 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1989 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1990 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00001991 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00001992
Tom Stellard54774e52013-10-23 02:53:47 +00001993 // Set which texture component corresponds to the lane.
1994 unsigned Comp;
1995 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1996 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00001997 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00001998 Dmask &= ~(1 << Comp);
1999 }
2000
Christian Konig8e06e2a2013-04-10 08:39:08 +00002001 // Abort if we have more than one user per component
2002 if (Users[Lane])
2003 return;
2004
2005 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00002006 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002007 }
2008
Tom Stellard54774e52013-10-23 02:53:47 +00002009 // Abort if there's no change
2010 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00002011 return;
2012
2013 // Adjust the writemask in the node
2014 std::vector<SDValue> Ops;
Tom Stellard54774e52013-10-23 02:53:47 +00002015 Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
Christian Konig8e06e2a2013-04-10 08:39:08 +00002016 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
2017 Ops.push_back(Node->getOperand(i));
Craig Topper8c0b4d02014-04-28 05:57:50 +00002018 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002019
Christian Konig8b1ed282013-04-10 08:39:16 +00002020 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00002021 // (if NewDmask has only one bit set...)
2022 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002023 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00002024 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002025 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00002026 SDValue(Node, 0), RC);
2027 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
2028 return;
2029 }
2030
Christian Konig8e06e2a2013-04-10 08:39:08 +00002031 // Update the users of the node with the new indices
2032 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
2033
2034 SDNode *User = Users[i];
2035 if (!User)
2036 continue;
2037
2038 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
2039 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
2040
2041 switch (Idx) {
2042 default: break;
2043 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
2044 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
2045 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
2046 }
2047 }
2048}
2049
Tom Stellard3457a842014-10-09 19:06:00 +00002050/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
2051/// with frame index operands.
2052/// LLVM assumes that inputs are to these instructions are registers.
2053void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
2054 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002055
2056 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00002057 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
2058 if (!isa<FrameIndexSDNode>(Node->getOperand(i))) {
2059 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002060 continue;
2061 }
2062
Tom Stellard3457a842014-10-09 19:06:00 +00002063 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002064 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00002065 Node->getOperand(i).getValueType(),
2066 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002067 }
2068
Tom Stellard3457a842014-10-09 19:06:00 +00002069 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002070}
2071
Matt Arsenault08d84942014-06-03 23:06:13 +00002072/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00002073SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
2074 SelectionDAG &DAG) const {
Eric Christopherd9134482014-08-04 21:25:23 +00002075 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
2076 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Tom Stellard0518ff82013-06-03 17:39:58 +00002077 Node = AdjustRegClass(Node, DAG);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002078
Tom Stellard16a9a202013-08-14 23:24:17 +00002079 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00002080 adjustWritemask(Node, DAG);
2081
Matt Arsenault7d858d82014-11-02 23:46:54 +00002082 if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG ||
2083 Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002084 legalizeTargetIndependentNode(Node, DAG);
2085 return Node;
2086 }
2087
Matt Arsenault253e5da2014-09-17 15:35:43 +00002088 return legalizeOperands(Node, DAG);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002089}
Christian Konig8b1ed282013-04-10 08:39:16 +00002090
2091/// \brief Assign the register class depending on the number of
2092/// bits set in the writemask
2093void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
2094 SDNode *Node) const {
Eric Christopherd9134482014-08-04 21:25:23 +00002095 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
2096 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002097
Tom Stellarda99ada52014-11-21 22:31:44 +00002098 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00002099 TII->legalizeOperands(MI);
2100
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002101 if (TII->isMIMG(MI->getOpcode())) {
2102 unsigned VReg = MI->getOperand(0).getReg();
2103 unsigned Writemask = MI->getOperand(1).getImm();
2104 unsigned BitsSet = 0;
2105 for (unsigned i = 0; i < 4; ++i)
2106 BitsSet += Writemask & (1 << i) ? 1 : 0;
2107
2108 const TargetRegisterClass *RC;
2109 switch (BitsSet) {
2110 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002111 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002112 case 2: RC = &AMDGPU::VReg_64RegClass; break;
2113 case 3: RC = &AMDGPU::VReg_96RegClass; break;
2114 }
2115
2116 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
2117 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002118 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00002119 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00002120 }
2121
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002122 // Replace unused atomics with the no return version.
2123 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
2124 if (NoRetAtomicOp != -1) {
2125 if (!Node->hasAnyUseOfValue(0)) {
2126 MI->setDesc(TII->get(NoRetAtomicOp));
2127 MI->RemoveOperand(0);
2128 }
2129
2130 return;
2131 }
Christian Konig8b1ed282013-04-10 08:39:16 +00002132}
Tom Stellard0518ff82013-06-03 17:39:58 +00002133
Matt Arsenault485defe2014-11-05 19:01:17 +00002134static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
2135 SDValue K = DAG.getTargetConstant(Val, MVT::i32);
2136 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
2137}
2138
2139MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
2140 SDLoc DL,
2141 SDValue Ptr) const {
Tom Stellard794c8c02014-12-02 17:05:41 +00002142 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
2143 getTargetMachine().getSubtargetImpl()->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00002144#if 1
2145 // XXX - Workaround for moveToVALU not handling different register class
2146 // inserts for REG_SEQUENCE.
2147
2148 // Build the half of the subregister with the constants.
2149 const SDValue Ops0[] = {
2150 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, MVT::i32),
2151 buildSMovImm32(DAG, DL, 0),
2152 DAG.getTargetConstant(AMDGPU::sub0, MVT::i32),
Tom Stellard794c8c02014-12-02 17:05:41 +00002153 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002154 DAG.getTargetConstant(AMDGPU::sub1, MVT::i32)
2155 };
2156
2157 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
2158 MVT::v2i32, Ops0), 0);
2159
2160 // Combine the constants and the pointer.
2161 const SDValue Ops1[] = {
2162 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2163 Ptr,
2164 DAG.getTargetConstant(AMDGPU::sub0_sub1, MVT::i32),
2165 SubRegHi,
2166 DAG.getTargetConstant(AMDGPU::sub2_sub3, MVT::i32)
2167 };
2168
2169 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
2170#else
2171 const SDValue Ops[] = {
2172 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2173 Ptr,
2174 DAG.getTargetConstant(AMDGPU::sub0_sub1, MVT::i32),
2175 buildSMovImm32(DAG, DL, 0),
2176 DAG.getTargetConstant(AMDGPU::sub2, MVT::i32),
Tom Stellard794c8c02014-12-02 17:05:41 +00002177 buildSMovImm32(DAG, DL, TII->getDefaultRsrcFormat() >> 32),
Matt Arsenault485defe2014-11-05 19:01:17 +00002178 DAG.getTargetConstant(AMDGPU::sub3, MVT::i32)
2179 };
2180
2181 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2182
2183#endif
2184}
2185
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002186/// \brief Return a resource descriptor with the 'Add TID' bit enabled
2187/// The TID (Thread ID) is multipled by the stride value (bits [61:48]
2188/// of the resource descriptor) to create an offset, which is added to the
2189/// resource ponter.
2190MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
2191 SDLoc DL,
2192 SDValue Ptr,
2193 uint32_t RsrcDword1,
2194 uint64_t RsrcDword2And3) const {
2195 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
2196 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
2197 if (RsrcDword1) {
2198 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
2199 DAG.getConstant(RsrcDword1, MVT::i32)), 0);
2200 }
2201
2202 SDValue DataLo = buildSMovImm32(DAG, DL,
2203 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
2204 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
2205
2206 const SDValue Ops[] = {
2207 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32),
2208 PtrLo,
2209 DAG.getTargetConstant(AMDGPU::sub0, MVT::i32),
2210 PtrHi,
2211 DAG.getTargetConstant(AMDGPU::sub1, MVT::i32),
2212 DataLo,
2213 DAG.getTargetConstant(AMDGPU::sub2, MVT::i32),
2214 DataHi,
2215 DAG.getTargetConstant(AMDGPU::sub3, MVT::i32)
2216 };
2217
2218 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2219}
2220
2221MachineSDNode *SITargetLowering::buildScratchRSRC(SelectionDAG &DAG,
2222 SDLoc DL,
2223 SDValue Ptr) const {
Tom Stellard794c8c02014-12-02 17:05:41 +00002224 const SIInstrInfo *TII = static_cast<const SIInstrInfo *>(
2225 getTargetMachine().getSubtargetImpl()->getInstrInfo());
2226 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() | AMDGPU::RSRC_TID_ENABLE |
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002227 0xffffffff; // Size
2228
2229 return buildRSRC(DAG, DL, Ptr, 0, Rsrc);
2230}
2231
Tom Stellard0518ff82013-06-03 17:39:58 +00002232MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
2233 SelectionDAG &DAG) const {
2234
2235 SDLoc DL(N);
2236 unsigned NewOpcode = N->getMachineOpcode();
2237
2238 switch (N->getMachineOpcode()) {
2239 default: return N;
Tom Stellard0518ff82013-06-03 17:39:58 +00002240 case AMDGPU::S_LOAD_DWORD_IMM:
2241 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
2242 // Fall-through
2243 case AMDGPU::S_LOAD_DWORDX2_SGPR:
2244 if (NewOpcode == N->getMachineOpcode()) {
2245 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
2246 }
2247 // Fall-through
2248 case AMDGPU::S_LOAD_DWORDX4_IMM:
2249 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
2250 if (NewOpcode == N->getMachineOpcode()) {
2251 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
2252 }
2253 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
2254 return N;
2255 }
2256 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
Matt Arsenault485defe2014-11-05 19:01:17 +00002257
2258 const SDValue Zero64 = DAG.getTargetConstant(0, MVT::i64);
2259 SDValue Ptr(DAG.getMachineNode(AMDGPU::S_MOV_B64, DL, MVT::i64, Zero64), 0);
2260 MachineSDNode *RSrc = wrapAddr64Rsrc(DAG, DL, Ptr);
Matt Arsenault61a528a2014-09-10 23:26:19 +00002261
2262 SmallVector<SDValue, 8> Ops;
2263 Ops.push_back(SDValue(RSrc, 0));
2264 Ops.push_back(N->getOperand(0));
Marek Olsak58f61a82014-12-07 17:17:38 +00002265
2266 // The immediate offset is in dwords on SI and in bytes on VI.
2267 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
Marek Olsak0c056452014-12-10 19:25:31 +00002268 Ops.push_back(DAG.getTargetConstant(Offset->getSExtValue(), MVT::i32));
Marek Olsak58f61a82014-12-07 17:17:38 +00002269 else
Marek Olsak0c056452014-12-10 19:25:31 +00002270 Ops.push_back(DAG.getTargetConstant(Offset->getSExtValue() << 2, MVT::i32));
Matt Arsenault61a528a2014-09-10 23:26:19 +00002271
2272 // Copy remaining operands so we keep any chain and glue nodes that follow
2273 // the normal operands.
2274 for (unsigned I = 2, E = N->getNumOperands(); I != E; ++I)
2275 Ops.push_back(N->getOperand(I));
2276
Tom Stellard0518ff82013-06-03 17:39:58 +00002277 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
2278 }
2279 }
2280}
Tom Stellard94593ee2013-06-03 17:40:18 +00002281
2282SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2283 const TargetRegisterClass *RC,
2284 unsigned Reg, EVT VT) const {
2285 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2286
2287 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2288 cast<RegisterSDNode>(VReg)->getReg(), VT);
2289}