blob: 94fad32c007093529abd27db3ee5c1bc0cacd0bf [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 Arsenaultd48da142015-11-02 23:23:02 +000023#include "AMDGPUDiagnosticInfoUnsupported.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000024#include "AMDGPUIntrinsicInfo.h"
Matt Arsenault41e2f2b2014-02-24 21:01:28 +000025#include "AMDGPUSubtarget.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000026#include "SIInstrInfo.h"
27#include "SIMachineFunctionInfo.h"
28#include "SIRegisterInfo.h"
Alexey Samsonova253bf92014-08-27 19:36:53 +000029#include "llvm/ADT/BitVector.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000030#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
32#include "llvm/CodeGen/MachineRegisterInfo.h"
33#include "llvm/CodeGen/SelectionDAG.h"
Benjamin Kramerd78bb462013-05-23 17:10:37 +000034#include "llvm/IR/Function.h"
Matt Arsenault364a6742014-06-11 17:50:44 +000035#include "llvm/ADT/SmallString.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000036
37using namespace llvm;
38
Eric Christopher7792e322015-01-30 23:24:40 +000039SITargetLowering::SITargetLowering(TargetMachine &TM,
40 const AMDGPUSubtarget &STI)
41 : AMDGPUTargetLowering(TM, STI) {
Tom Stellard1bd80722014-04-30 15:31:33 +000042 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
Tom Stellard436780b2014-05-15 14:41:57 +000043 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000044
Christian Konig2214f142013-03-07 09:03:38 +000045 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
46 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
47
Tom Stellard334b29c2014-04-17 21:00:09 +000048 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
Tom Stellard45c0b3a2015-01-07 20:59:25 +000049 addRegisterClass(MVT::f32, &AMDGPU::VGPR_32RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000050
Tom Stellard436780b2014-05-15 14:41:57 +000051 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
52 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
53 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000054
Matt Arsenault61001bb2015-11-25 19:58:34 +000055 addRegisterClass(MVT::v2i64, &AMDGPU::SReg_128RegClass);
56 addRegisterClass(MVT::v2f64, &AMDGPU::SReg_128RegClass);
57
Tom Stellard436780b2014-05-15 14:41:57 +000058 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
59 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000060
Tom Stellardf0a21072014-11-18 20:39:39 +000061 addRegisterClass(MVT::v8i32, &AMDGPU::SReg_256RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000062 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
63
Tom Stellardf0a21072014-11-18 20:39:39 +000064 addRegisterClass(MVT::v16i32, &AMDGPU::SReg_512RegClass);
Christian Konig2214f142013-03-07 09:03:38 +000065 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
Tom Stellard75aadc22012-12-11 21:25:42 +000066
Eric Christopher23a3a7c2015-02-26 00:00:24 +000067 computeRegisterProperties(STI.getRegisterInfo());
Tom Stellard75aadc22012-12-11 21:25:42 +000068
Christian Konig2989ffc2013-03-18 11:34:16 +000069 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
70 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
71 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
72 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
73
Tom Stellard75aadc22012-12-11 21:25:42 +000074 setOperationAction(ISD::ADD, MVT::i32, Legal);
Matt Arsenaulte8d21462013-11-18 20:09:40 +000075 setOperationAction(ISD::ADDC, MVT::i32, Legal);
76 setOperationAction(ISD::ADDE, MVT::i32, Legal);
Matt Arsenaultb8b51532014-06-23 18:00:38 +000077 setOperationAction(ISD::SUBC, MVT::i32, Legal);
78 setOperationAction(ISD::SUBE, MVT::i32, Legal);
Aaron Watrydaabb202013-06-25 13:55:52 +000079
Matt Arsenaultad14ce82014-07-19 18:44:39 +000080 setOperationAction(ISD::FSIN, MVT::f32, Custom);
81 setOperationAction(ISD::FCOS, MVT::f32, Custom);
82
Matt Arsenault7c936902014-10-21 23:01:01 +000083 setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
84 setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
85
Tom Stellard35bb18c2013-08-26 15:06:04 +000086 // We need to custom lower vector stores from local memory
Tom Stellard35bb18c2013-08-26 15:06:04 +000087 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
Tom Stellardaf775432013-10-23 00:44:32 +000088 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
89 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
90
91 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
92 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
Tom Stellard35bb18c2013-08-26 15:06:04 +000093
Tom Stellard1c8788e2014-03-07 20:12:33 +000094 setOperationAction(ISD::STORE, MVT::i1, Custom);
Tom Stellard81d871d2013-11-13 23:36:50 +000095 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
96
Tom Stellard0ec134f2014-02-04 17:18:40 +000097 setOperationAction(ISD::SELECT, MVT::i64, Custom);
Tom Stellardda99c6e2014-03-24 16:07:30 +000098 setOperationAction(ISD::SELECT, MVT::f64, Promote);
99 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
Tom Stellard81d871d2013-11-13 23:36:50 +0000100
Tom Stellard3ca1bfc2014-06-10 16:01:22 +0000101 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
102 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
103 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
104 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
Tom Stellard754f80f2013-04-05 23:31:51 +0000105
Tom Stellard83747202013-07-18 21:43:53 +0000106 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
107 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
108
Matt Arsenaulte306a322014-10-21 16:25:08 +0000109 setOperationAction(ISD::BSWAP, MVT::i32, Legal);
110
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000111 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
114
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000115 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000116 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
118
Matt Arsenault5dbd5db2014-04-22 03:49:30 +0000119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
122
Matt Arsenault94812212014-11-14 18:18:16 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
Matt Arsenault4e466652014-04-16 01:41:30 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
125
Tom Stellard94593ee2013-06-03 17:40:18 +0000126 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
Tom Stellard9fa17912013-08-14 23:24:45 +0000127 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
128 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
129 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
Tom Stellard94593ee2013-06-03 17:40:18 +0000130
Tom Stellardafcf12f2013-09-12 02:55:14 +0000131 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000132 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
Tom Stellardafcf12f2013-09-12 02:55:14 +0000133
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000134 for (MVT VT : MVT::integer_valuetypes()) {
Matt Arsenaultbd223422015-01-14 01:35:17 +0000135 if (VT == MVT::i64)
136 continue;
137
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000138 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000139 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
140 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000141 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
Tom Stellard31209cc2013-07-15 19:00:09 +0000142
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000143 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000144 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
145 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000146 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000147
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000148 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
Matt Arsenaultbd223422015-01-14 01:35:17 +0000149 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
150 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000151 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
152 }
153
154 for (MVT VT : MVT::integer_vector_valuetypes()) {
155 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i16, Expand);
156 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v16i16, Expand);
157 }
158
159 for (MVT VT : MVT::fp_valuetypes())
160 setLoadExtAction(ISD::EXTLOAD, VT, MVT::f32, Expand);
Matt Arsenault470acd82014-04-15 22:28:39 +0000161
Matt Arsenault61001bb2015-11-25 19:58:34 +0000162 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
163 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
164
Matt Arsenault6f243792013-09-05 19:41:10 +0000165 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000166 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
Matt Arsenaulte1ce3442015-07-31 04:12:04 +0000167 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand);
Tom Stellardaf775432013-10-23 00:44:32 +0000168 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
Niels Ole Salscheider719fbc92013-08-08 16:06:15 +0000169
Matt Arsenault61001bb2015-11-25 19:58:34 +0000170
171 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
172
173 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
174 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
175
Matt Arsenault470acd82014-04-15 22:28:39 +0000176 setOperationAction(ISD::LOAD, MVT::i1, Custom);
177
Matt Arsenault61001bb2015-11-25 19:58:34 +0000178 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
179 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
180
181 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
182 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
183
184 setOperationAction(ISD::ConstantPool, MVT::v2i64, Expand);
185
Tom Stellardfd155822013-08-26 15:05:36 +0000186 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Tom Stellard04c0e982014-01-22 19:24:21 +0000187 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
Matt Arsenaulta98cd6a2013-12-19 05:32:55 +0000188 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
Michel Danzer49812b52013-07-10 16:37:07 +0000189
Tom Stellard5f337882014-04-29 23:12:43 +0000190 // These should use UDIVREM, so set them to expand
191 setOperationAction(ISD::UDIV, MVT::i64, Expand);
192 setOperationAction(ISD::UREM, MVT::i64, Expand);
193
Matt Arsenault0d89e842014-07-15 21:44:37 +0000194 setOperationAction(ISD::SELECT_CC, MVT::i1, Expand);
195 setOperationAction(ISD::SELECT, MVT::i1, Promote);
196
Matt Arsenault61001bb2015-11-25 19:58:34 +0000197 setOperationAction(ISD::TRUNCATE, MVT::v2i32, Expand);
198
199
200 setOperationAction(ISD::FP_ROUND, MVT::v2f32, Expand);
201
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000202 // We only support LOAD/STORE and vector manipulation ops for vectors
203 // with > 4 elements.
Matt Arsenault61001bb2015-11-25 19:58:34 +0000204 for (MVT VT : {MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32, MVT::v2i64, MVT::v2f64}) {
Tom Stellard967bf582014-02-13 23:34:15 +0000205 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
206 switch(Op) {
207 case ISD::LOAD:
208 case ISD::STORE:
209 case ISD::BUILD_VECTOR:
210 case ISD::BITCAST:
211 case ISD::EXTRACT_VECTOR_ELT:
212 case ISD::INSERT_VECTOR_ELT:
Tom Stellard967bf582014-02-13 23:34:15 +0000213 case ISD::INSERT_SUBVECTOR:
214 case ISD::EXTRACT_SUBVECTOR:
Matt Arsenault61001bb2015-11-25 19:58:34 +0000215 case ISD::SCALAR_TO_VECTOR:
Tom Stellard967bf582014-02-13 23:34:15 +0000216 break;
Tom Stellardc0503db2014-08-09 01:06:56 +0000217 case ISD::CONCAT_VECTORS:
218 setOperationAction(Op, VT, Custom);
219 break;
Tom Stellard967bf582014-02-13 23:34:15 +0000220 default:
Matt Arsenaultd504a742014-05-15 21:44:05 +0000221 setOperationAction(Op, VT, Expand);
Tom Stellard967bf582014-02-13 23:34:15 +0000222 break;
223 }
224 }
225 }
226
Matt Arsenault61001bb2015-11-25 19:58:34 +0000227 // Most operations are naturally 32-bit vector operations. We only support
228 // load and store of i64 vectors, so promote v2i64 vector operations to v4i32.
229 for (MVT Vec64 : { MVT::v2i64, MVT::v2f64 }) {
230 setOperationAction(ISD::BUILD_VECTOR, Vec64, Promote);
231 AddPromotedToType(ISD::BUILD_VECTOR, Vec64, MVT::v4i32);
232
233 setOperationAction(ISD::EXTRACT_VECTOR_ELT, Vec64, Promote);
234 AddPromotedToType(ISD::EXTRACT_VECTOR_ELT, Vec64, MVT::v4i32);
235
236 setOperationAction(ISD::INSERT_VECTOR_ELT, Vec64, Promote);
237 AddPromotedToType(ISD::INSERT_VECTOR_ELT, Vec64, MVT::v4i32);
238
239 setOperationAction(ISD::SCALAR_TO_VECTOR, Vec64, Promote);
240 AddPromotedToType(ISD::SCALAR_TO_VECTOR, Vec64, MVT::v4i32);
241 }
242
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000243 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
244 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
245 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
Matt Arsenaulta90d22f2014-04-17 17:06:37 +0000246 setOperationAction(ISD::FRINT, MVT::f64, Legal);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000247 }
248
Marek Olsak7d777282015-03-24 13:40:15 +0000249 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000250 setOperationAction(ISD::FDIV, MVT::f32, Custom);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +0000251 setOperationAction(ISD::FDIV, MVT::f64, Custom);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000252
Matt Arsenault02cb0ff2014-09-29 14:59:34 +0000253 setTargetDAGCombine(ISD::FADD);
Matt Arsenault8675db12014-08-29 16:01:14 +0000254 setTargetDAGCombine(ISD::FSUB);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +0000255 setTargetDAGCombine(ISD::FMINNUM);
256 setTargetDAGCombine(ISD::FMAXNUM);
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000257 setTargetDAGCombine(ISD::SMIN);
258 setTargetDAGCombine(ISD::SMAX);
259 setTargetDAGCombine(ISD::UMIN);
260 setTargetDAGCombine(ISD::UMAX);
Matt Arsenault41e2f2b2014-02-24 21:01:28 +0000261 setTargetDAGCombine(ISD::SELECT_CC);
Tom Stellard75aadc22012-12-11 21:25:42 +0000262 setTargetDAGCombine(ISD::SETCC);
Matt Arsenaultd0101a22015-01-06 23:00:46 +0000263 setTargetDAGCombine(ISD::AND);
Matt Arsenaultf2290332015-01-06 23:00:39 +0000264 setTargetDAGCombine(ISD::OR);
Matt Arsenault364a6742014-06-11 17:50:44 +0000265 setTargetDAGCombine(ISD::UINT_TO_FP);
266
Matt Arsenaultb2baffa2014-08-15 17:49:05 +0000267 // All memory operations. Some folding on the pointer operand is done to help
268 // matching the constant offsets in the addressing modes.
269 setTargetDAGCombine(ISD::LOAD);
270 setTargetDAGCombine(ISD::STORE);
271 setTargetDAGCombine(ISD::ATOMIC_LOAD);
272 setTargetDAGCombine(ISD::ATOMIC_STORE);
273 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP);
274 setTargetDAGCombine(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS);
275 setTargetDAGCombine(ISD::ATOMIC_SWAP);
276 setTargetDAGCombine(ISD::ATOMIC_LOAD_ADD);
277 setTargetDAGCombine(ISD::ATOMIC_LOAD_SUB);
278 setTargetDAGCombine(ISD::ATOMIC_LOAD_AND);
279 setTargetDAGCombine(ISD::ATOMIC_LOAD_OR);
280 setTargetDAGCombine(ISD::ATOMIC_LOAD_XOR);
281 setTargetDAGCombine(ISD::ATOMIC_LOAD_NAND);
282 setTargetDAGCombine(ISD::ATOMIC_LOAD_MIN);
283 setTargetDAGCombine(ISD::ATOMIC_LOAD_MAX);
284 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMIN);
285 setTargetDAGCombine(ISD::ATOMIC_LOAD_UMAX);
286
Christian Konigeecebd02013-03-26 14:04:02 +0000287 setSchedulingPreference(Sched::RegPressure);
Tom Stellard75aadc22012-12-11 21:25:42 +0000288}
289
Tom Stellard0125f2a2013-06-25 02:39:35 +0000290//===----------------------------------------------------------------------===//
291// TargetLowering queries
292//===----------------------------------------------------------------------===//
293
Matt Arsenaulte306a322014-10-21 16:25:08 +0000294bool SITargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &,
295 EVT) const {
296 // SI has some legal vector types, but no legal vector operations. Say no
297 // shuffles are legal in order to prefer scalarizing some vector operations.
298 return false;
299}
300
Tom Stellard70580f82015-07-20 14:28:41 +0000301bool SITargetLowering::isLegalFlatAddressingMode(const AddrMode &AM) const {
302 // Flat instructions do not have offsets, and only have the register
303 // address.
304 return AM.BaseOffs == 0 && (AM.Scale == 0 || AM.Scale == 1);
305}
306
Matt Arsenault711b3902015-08-07 20:18:34 +0000307bool SITargetLowering::isLegalMUBUFAddressingMode(const AddrMode &AM) const {
308 // MUBUF / MTBUF instructions have a 12-bit unsigned byte offset, and
309 // additionally can do r + r + i with addr64. 32-bit has more addressing
310 // mode options. Depending on the resource constant, it can also do
311 // (i64 r0) + (i32 r1) * (i14 i).
312 //
313 // Private arrays end up using a scratch buffer most of the time, so also
314 // assume those use MUBUF instructions. Scratch loads / stores are currently
315 // implemented as mubuf instructions with offen bit set, so slightly
316 // different than the normal addr64.
317 if (!isUInt<12>(AM.BaseOffs))
318 return false;
319
320 // FIXME: Since we can split immediate into soffset and immediate offset,
321 // would it make sense to allow any immediate?
322
323 switch (AM.Scale) {
324 case 0: // r + i or just i, depending on HasBaseReg.
325 return true;
326 case 1:
327 return true; // We have r + r or r + i.
328 case 2:
329 if (AM.HasBaseReg) {
330 // Reject 2 * r + r.
331 return false;
332 }
333
334 // Allow 2 * r as r + r
335 // Or 2 * r + i is allowed as r + r + i.
336 return true;
337 default: // Don't allow n * r
338 return false;
339 }
340}
341
Mehdi Amini0cdec1e2015-07-09 02:09:40 +0000342bool SITargetLowering::isLegalAddressingMode(const DataLayout &DL,
343 const AddrMode &AM, Type *Ty,
344 unsigned AS) const {
Matt Arsenault5015a892014-08-15 17:17:07 +0000345 // No global is ever allowed as a base.
346 if (AM.BaseGV)
347 return false;
348
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000349 switch (AS) {
Matt Arsenault711b3902015-08-07 20:18:34 +0000350 case AMDGPUAS::GLOBAL_ADDRESS: {
Tom Stellard70580f82015-07-20 14:28:41 +0000351 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
352 // Assume the we will use FLAT for all global memory accesses
353 // on VI.
354 // FIXME: This assumption is currently wrong. On VI we still use
355 // MUBUF instructions for the r + i addressing mode. As currently
356 // implemented, the MUBUF instructions only work on buffer < 4GB.
357 // It may be possible to support > 4GB buffers with MUBUF instructions,
358 // by setting the stride value in the resource descriptor which would
359 // increase the size limit to (stride * 4GB). However, this is risky,
360 // because it has never been validated.
361 return isLegalFlatAddressingMode(AM);
362 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000363
Matt Arsenault711b3902015-08-07 20:18:34 +0000364 return isLegalMUBUFAddressingMode(AM);
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000365 }
Matt Arsenault711b3902015-08-07 20:18:34 +0000366 case AMDGPUAS::CONSTANT_ADDRESS: {
367 // If the offset isn't a multiple of 4, it probably isn't going to be
368 // correctly aligned.
369 if (AM.BaseOffs % 4 != 0)
370 return isLegalMUBUFAddressingMode(AM);
371
372 // There are no SMRD extloads, so if we have to do a small type access we
373 // will use a MUBUF load.
374 // FIXME?: We also need to do this if unaligned, but we don't know the
375 // alignment here.
376 if (DL.getTypeStoreSize(Ty) < 4)
377 return isLegalMUBUFAddressingMode(AM);
378
379 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
380 // SMRD instructions have an 8-bit, dword offset on SI.
381 if (!isUInt<8>(AM.BaseOffs / 4))
382 return false;
383 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS) {
384 // On CI+, this can also be a 32-bit literal constant offset. If it fits
385 // in 8-bits, it can use a smaller encoding.
386 if (!isUInt<32>(AM.BaseOffs / 4))
387 return false;
388 } else if (Subtarget->getGeneration() == AMDGPUSubtarget::VOLCANIC_ISLANDS) {
389 // On VI, these use the SMEM format and the offset is 20-bit in bytes.
390 if (!isUInt<20>(AM.BaseOffs))
391 return false;
392 } else
393 llvm_unreachable("unhandled generation");
394
395 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
396 return true;
397
398 if (AM.Scale == 1 && AM.HasBaseReg)
399 return true;
400
401 return false;
402 }
403
404 case AMDGPUAS::PRIVATE_ADDRESS:
405 case AMDGPUAS::UNKNOWN_ADDRESS_SPACE:
406 return isLegalMUBUFAddressingMode(AM);
407
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000408 case AMDGPUAS::LOCAL_ADDRESS:
409 case AMDGPUAS::REGION_ADDRESS: {
410 // Basic, single offset DS instructions allow a 16-bit unsigned immediate
411 // field.
412 // XXX - If doing a 4-byte aligned 8-byte type access, we effectively have
413 // an 8-bit dword offset but we don't know the alignment here.
414 if (!isUInt<16>(AM.BaseOffs))
Matt Arsenault5015a892014-08-15 17:17:07 +0000415 return false;
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000416
417 if (AM.Scale == 0) // r + i or just i, depending on HasBaseReg.
418 return true;
419
420 if (AM.Scale == 1 && AM.HasBaseReg)
421 return true;
422
Matt Arsenault5015a892014-08-15 17:17:07 +0000423 return false;
424 }
Tom Stellard70580f82015-07-20 14:28:41 +0000425 case AMDGPUAS::FLAT_ADDRESS:
426 return isLegalFlatAddressingMode(AM);
427
Matt Arsenault73e06fa2015-06-04 16:17:42 +0000428 default:
429 llvm_unreachable("unhandled address space");
430 }
Matt Arsenault5015a892014-08-15 17:17:07 +0000431}
432
Matt Arsenaulte6986632015-01-14 01:35:22 +0000433bool SITargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000434 unsigned AddrSpace,
435 unsigned Align,
436 bool *IsFast) const {
Matt Arsenault1018c892014-04-24 17:08:26 +0000437 if (IsFast)
438 *IsFast = false;
439
Matt Arsenault1018c892014-04-24 17:08:26 +0000440 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
441 // which isn't a simple VT.
Tom Stellard81d871d2013-11-13 23:36:50 +0000442 if (!VT.isSimple() || VT == MVT::Other)
443 return false;
Matt Arsenault1018c892014-04-24 17:08:26 +0000444
Tom Stellardc6b299c2015-02-02 18:02:28 +0000445 // TODO - CI+ supports unaligned memory accesses, but this requires driver
446 // support.
Matt Arsenault1018c892014-04-24 17:08:26 +0000447
Matt Arsenault1018c892014-04-24 17:08:26 +0000448 // XXX - The only mention I see of this in the ISA manual is for LDS direct
449 // reads the "byte address and must be dword aligned". Is it also true for the
450 // normal loads and stores?
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000451 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS) {
452 // ds_read/write_b64 require 8-byte alignment, but we can do a 4 byte
453 // aligned, 8 byte access in a single operation using ds_read2/write2_b32
454 // with adjacent offsets.
Sanjay Patelce74db92015-09-03 15:03:19 +0000455 bool AlignedBy4 = (Align % 4 == 0);
456 if (IsFast)
457 *IsFast = AlignedBy4;
458 return AlignedBy4;
Matt Arsenault6f2a5262014-07-27 17:46:40 +0000459 }
Matt Arsenault1018c892014-04-24 17:08:26 +0000460
Tom Stellard33e64c62015-02-04 20:49:52 +0000461 // Smaller than dword value must be aligned.
462 // FIXME: This should be allowed on CI+
463 if (VT.bitsLT(MVT::i32))
464 return false;
465
Matt Arsenault1018c892014-04-24 17:08:26 +0000466 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
467 // byte-address are ignored, thus forcing Dword alignment.
Tom Stellarde812f2f2014-07-21 15:45:06 +0000468 // This applies to private, global, and constant memory.
Matt Arsenault1018c892014-04-24 17:08:26 +0000469 if (IsFast)
470 *IsFast = true;
Tom Stellardc6b299c2015-02-02 18:02:28 +0000471
472 return VT.bitsGT(MVT::i32) && Align % 4 == 0;
Tom Stellard0125f2a2013-06-25 02:39:35 +0000473}
474
Matt Arsenault46645fa2014-07-28 17:49:26 +0000475EVT SITargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
476 unsigned SrcAlign, bool IsMemset,
477 bool ZeroMemset,
478 bool MemcpyStrSrc,
479 MachineFunction &MF) const {
480 // FIXME: Should account for address space here.
481
482 // The default fallback uses the private pointer size as a guess for a type to
483 // use. Make sure we switch these to 64-bit accesses.
484
485 if (Size >= 16 && DstAlign >= 4) // XXX: Should only do for global
486 return MVT::v4i32;
487
488 if (Size >= 8 && DstAlign >= 4)
489 return MVT::v2i32;
490
491 // Use the default.
492 return MVT::Other;
493}
494
Chandler Carruth9d010ff2014-07-03 00:23:43 +0000495TargetLoweringBase::LegalizeTypeAction
496SITargetLowering::getPreferredVectorAction(EVT VT) const {
497 if (VT.getVectorNumElements() != 1 && VT.getScalarType().bitsLE(MVT::i16))
498 return TypeSplitVector;
499
500 return TargetLoweringBase::getPreferredVectorAction(VT);
Tom Stellardd86003e2013-08-14 23:25:00 +0000501}
Tom Stellard0125f2a2013-06-25 02:39:35 +0000502
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000503bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
504 Type *Ty) const {
Eric Christopher7792e322015-01-30 23:24:40 +0000505 const SIInstrInfo *TII =
506 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultd7bdcc42014-03-31 19:54:27 +0000507 return TII->isInlineConstant(Imm);
508}
509
Tom Stellardaf775432013-10-23 00:44:32 +0000510SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000511 SDLoc SL, SDValue Chain,
Matt Arsenaulte1f030c2014-04-11 20:59:54 +0000512 unsigned Offset, bool Signed) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000513 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardec2e43c2014-09-22 15:35:29 +0000514 MachineFunction &MF = DAG.getMachineFunction();
515 const SIRegisterInfo *TRI =
516 static_cast<const SIRegisterInfo*>(Subtarget->getRegisterInfo());
517 unsigned InputPtrReg = TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
Tom Stellard94593ee2013-06-03 17:40:18 +0000518
Matt Arsenault86033ca2014-07-28 17:31:39 +0000519 Type *Ty = VT.getTypeForEVT(*DAG.getContext());
520
521 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000522 MVT PtrVT = getPointerTy(DL, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000523 PointerType *PtrTy = PointerType::get(Ty, AMDGPUAS::CONSTANT_ADDRESS);
Matt Arsenaulta0269b62015-06-01 21:58:24 +0000524 SDValue BasePtr = DAG.getCopyFromReg(Chain, SL,
525 MRI.getLiveInVirtReg(InputPtrReg), PtrVT);
526 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
527 DAG.getConstant(Offset, SL, PtrVT));
Mehdi Amini44ede332015-07-09 02:09:04 +0000528 SDValue PtrOffset = DAG.getUNDEF(PtrVT);
Matt Arsenault86033ca2014-07-28 17:31:39 +0000529 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy));
530
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000531 unsigned Align = DL.getABITypeAlignment(Ty);
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000532
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000533 ISD::LoadExtType ExtTy = Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
Matt Arsenaultacd68b52015-09-09 01:12:27 +0000534 if (MemVT.isFloatingPoint())
535 ExtTy = ISD::EXTLOAD;
536
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000537 return DAG.getLoad(ISD::UNINDEXED, ExtTy,
Matt Arsenault86033ca2014-07-28 17:31:39 +0000538 VT, SL, Chain, Ptr, PtrOffset, PtrInfo, MemVT,
539 false, // isVolatile
540 true, // isNonTemporal
541 true, // isInvariant
Matt Arsenault81c7ae22015-06-04 16:00:27 +0000542 Align); // Alignment
Tom Stellard94593ee2013-06-03 17:40:18 +0000543}
544
Christian Konig2c8f6d52013-03-07 09:03:52 +0000545SDValue SITargetLowering::LowerFormalArguments(
Eric Christopher7792e322015-01-30 23:24:40 +0000546 SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
547 const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc DL, SelectionDAG &DAG,
548 SmallVectorImpl<SDValue> &InVals) const {
Tom Stellardec2e43c2014-09-22 15:35:29 +0000549 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +0000550 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000551
552 MachineFunction &MF = DAG.getMachineFunction();
553 FunctionType *FType = MF.getFunction()->getFunctionType();
Christian Konig99ee0f42013-03-07 09:04:14 +0000554 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000555
Matt Arsenaultd48da142015-11-02 23:23:02 +0000556 if (Subtarget->isAmdHsaOS() && Info->getShaderType() != ShaderType::COMPUTE) {
557 const Function *Fn = MF.getFunction();
558 DiagnosticInfoUnsupported NoGraphicsHSA(*Fn, "non-compute shaders with HSA");
559 DAG.getContext()->diagnose(NoGraphicsHSA);
560 return SDValue();
561 }
562
Tom Stellard0fbf8992015-10-06 21:16:34 +0000563 // FIXME: We currently assume all calling conventions are kernels.
Christian Konig2c8f6d52013-03-07 09:03:52 +0000564
565 SmallVector<ISD::InputArg, 16> Splits;
Alexey Samsonova253bf92014-08-27 19:36:53 +0000566 BitVector Skipped(Ins.size());
Christian Konig99ee0f42013-03-07 09:04:14 +0000567
568 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000569 const ISD::InputArg &Arg = Ins[i];
Matt Arsenault758659232013-05-18 00:21:46 +0000570
571 // First check if it's a PS input addr
Matt Arsenault762af962014-07-13 03:06:39 +0000572 if (Info->getShaderType() == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
Vincent Lejeuned6236442013-10-13 17:56:16 +0000573 !Arg.Flags.isByVal()) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000574
575 assert((PSInputNum <= 15) && "Too many PS inputs!");
576
577 if (!Arg.Used) {
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000578 // We can safely skip PS inputs
Alexey Samsonova253bf92014-08-27 19:36:53 +0000579 Skipped.set(i);
Christian Konig99ee0f42013-03-07 09:04:14 +0000580 ++PSInputNum;
581 continue;
582 }
583
584 Info->PSInputAddr |= 1 << PSInputNum++;
585 }
586
587 // Second split vertices into their elements
Matt Arsenault762af962014-07-13 03:06:39 +0000588 if (Info->getShaderType() != ShaderType::COMPUTE && Arg.VT.isVector()) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000589 ISD::InputArg NewArg = Arg;
590 NewArg.Flags.setSplit();
591 NewArg.VT = Arg.VT.getVectorElementType();
592
593 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
594 // three or five element vertex only needs three or five registers,
Benjamin Kramerdf005cb2015-08-08 18:27:36 +0000595 // NOT four or eight.
Andrew Trick05938a52015-02-16 18:10:47 +0000596 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000597 unsigned NumElements = ParamType->getVectorNumElements();
598
599 for (unsigned j = 0; j != NumElements; ++j) {
600 Splits.push_back(NewArg);
601 NewArg.PartOffset += NewArg.VT.getStoreSize();
602 }
603
Matt Arsenault762af962014-07-13 03:06:39 +0000604 } else if (Info->getShaderType() != ShaderType::COMPUTE) {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000605 Splits.push_back(Arg);
606 }
607 }
608
609 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopherb5217502014-08-06 18:45:26 +0000610 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
611 *DAG.getContext());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000612
Christian Konig99ee0f42013-03-07 09:04:14 +0000613 // At least one interpolation mode must be enabled or else the GPU will hang.
Matt Arsenault762af962014-07-13 03:06:39 +0000614 if (Info->getShaderType() == ShaderType::PIXEL &&
615 (Info->PSInputAddr & 0x7F) == 0) {
Christian Konig99ee0f42013-03-07 09:04:14 +0000616 Info->PSInputAddr |= 1;
617 CCInfo.AllocateReg(AMDGPU::VGPR0);
618 CCInfo.AllocateReg(AMDGPU::VGPR1);
619 }
620
Tom Stellarded882c22013-06-03 17:40:11 +0000621 // The pointer to the list of arguments is stored in SGPR0, SGPR1
NAKAMURA Takumi84965032015-09-22 11:14:12 +0000622 // The pointer to the scratch buffer is stored in SGPR2, SGPR3
Matt Arsenault762af962014-07-13 03:06:39 +0000623 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardfeab91c2014-12-02 17:41:43 +0000624 if (Subtarget->isAmdHsaOS())
625 Info->NumUserSGPRs = 2; // FIXME: Need to support scratch buffers.
626 else
627 Info->NumUserSGPRs = 4;
Tom Stellardec2e43c2014-09-22 15:35:29 +0000628
629 unsigned InputPtrReg =
630 TRI->getPreloadedValue(MF, SIRegisterInfo::INPUT_PTR);
631 unsigned InputPtrRegLo =
632 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 0);
633 unsigned InputPtrRegHi =
634 TRI->getPhysRegSubReg(InputPtrReg, &AMDGPU::SReg_32RegClass, 1);
635
Tom Stellardec2e43c2014-09-22 15:35:29 +0000636 CCInfo.AllocateReg(InputPtrRegLo);
637 CCInfo.AllocateReg(InputPtrRegHi);
Tom Stellardec2e43c2014-09-22 15:35:29 +0000638 MF.addLiveIn(InputPtrReg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultea03cf22015-11-30 15:46:47 +0000639
640 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
641
642 if (MFI->hasDispatchPtr()) {
Tom Stellard48f29f22015-11-26 00:43:29 +0000643 unsigned DispatchPtrReg =
644 TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_PTR);
645 unsigned DispatchPtrRegLo =
646 TRI->getPhysRegSubReg(DispatchPtrReg, &AMDGPU::SReg_32RegClass, 0);
647 unsigned DispatchPtrRegHi =
648 TRI->getPhysRegSubReg(DispatchPtrReg, &AMDGPU::SReg_32RegClass, 1);
649 CCInfo.AllocateReg(DispatchPtrRegLo);
650 CCInfo.AllocateReg(DispatchPtrRegHi);
651 MF.addLiveIn(DispatchPtrReg, &AMDGPU::SReg_64RegClass);
652 }
Tom Stellarded882c22013-06-03 17:40:11 +0000653 }
654
Matt Arsenault762af962014-07-13 03:06:39 +0000655 if (Info->getShaderType() == ShaderType::COMPUTE) {
Tom Stellardaf775432013-10-23 00:44:32 +0000656 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
657 Splits);
658 }
659
Christian Konig2c8f6d52013-03-07 09:03:52 +0000660 AnalyzeFormalArguments(CCInfo, Splits);
661
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000662 SmallVector<SDValue, 16> Chains;
663
Christian Konig2c8f6d52013-03-07 09:03:52 +0000664 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
665
Christian Konigb7be72d2013-05-17 09:46:48 +0000666 const ISD::InputArg &Arg = Ins[i];
Alexey Samsonova253bf92014-08-27 19:36:53 +0000667 if (Skipped[i]) {
Christian Konigb7be72d2013-05-17 09:46:48 +0000668 InVals.push_back(DAG.getUNDEF(Arg.VT));
Christian Konig99ee0f42013-03-07 09:04:14 +0000669 continue;
670 }
671
Christian Konig2c8f6d52013-03-07 09:03:52 +0000672 CCValAssign &VA = ArgLocs[ArgIdx++];
Craig Topper7f416c82014-11-16 21:17:18 +0000673 MVT VT = VA.getLocVT();
Tom Stellarded882c22013-06-03 17:40:11 +0000674
675 if (VA.isMemLoc()) {
Tom Stellardaf775432013-10-23 00:44:32 +0000676 VT = Ins[i].VT;
677 EVT MemVT = Splits[i].VT;
Tom Stellardb5798b02015-06-26 21:15:03 +0000678 const unsigned Offset = Subtarget->getExplicitKernelArgOffset() +
679 VA.getLocMemOffset();
Tom Stellard94593ee2013-06-03 17:40:18 +0000680 // The first 36 bytes of the input buffer contains information about
681 // thread group and global sizes.
Matt Arsenault0d519732015-07-10 22:28:41 +0000682 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, Chain,
Jan Veselye5121f32014-10-14 20:05:26 +0000683 Offset, Ins[i].Flags.isSExt());
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000684 Chains.push_back(Arg.getValue(1));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000685
Craig Toppere3dcce92015-08-01 22:20:21 +0000686 auto *ParamTy =
Andrew Trick05938a52015-02-16 18:10:47 +0000687 dyn_cast<PointerType>(FType->getParamType(Ins[i].getOrigArgIndex()));
Tom Stellardca7ecf32014-08-22 18:49:31 +0000688 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS &&
689 ParamTy && ParamTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
690 // On SI local pointers are just offsets into LDS, so they are always
691 // less than 16-bits. On CI and newer they could potentially be
692 // real pointers, so we can't guarantee their size.
693 Arg = DAG.getNode(ISD::AssertZext, DL, Arg.getValueType(), Arg,
694 DAG.getValueType(MVT::i16));
695 }
696
Tom Stellarded882c22013-06-03 17:40:11 +0000697 InVals.push_back(Arg);
Jan Veselye5121f32014-10-14 20:05:26 +0000698 Info->ABIArgOffset = Offset + MemVT.getStoreSize();
Tom Stellarded882c22013-06-03 17:40:11 +0000699 continue;
700 }
Christian Konig2c8f6d52013-03-07 09:03:52 +0000701 assert(VA.isRegLoc() && "Parameter must be in a register!");
702
703 unsigned Reg = VA.getLocReg();
Christian Konig2c8f6d52013-03-07 09:03:52 +0000704
705 if (VT == MVT::i64) {
706 // For now assume it is a pointer
707 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
708 &AMDGPU::SReg_64RegClass);
709 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000710 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
711 InVals.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000712 continue;
713 }
714
715 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
716
717 Reg = MF.addLiveIn(Reg, RC);
718 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
719
Christian Konig2c8f6d52013-03-07 09:03:52 +0000720 if (Arg.VT.isVector()) {
721
722 // Build a vector from the registers
Andrew Trick05938a52015-02-16 18:10:47 +0000723 Type *ParamType = FType->getParamType(Arg.getOrigArgIndex());
Christian Konig2c8f6d52013-03-07 09:03:52 +0000724 unsigned NumElements = ParamType->getVectorNumElements();
725
726 SmallVector<SDValue, 4> Regs;
727 Regs.push_back(Val);
728 for (unsigned j = 1; j != NumElements; ++j) {
729 Reg = ArgLocs[ArgIdx++].getLocReg();
730 Reg = MF.addLiveIn(Reg, RC);
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000731
732 SDValue Copy = DAG.getCopyFromReg(Chain, DL, Reg, VT);
733 Regs.push_back(Copy);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000734 }
735
736 // Fill up the missing vector elements
737 NumElements = Arg.VT.getVectorNumElements() - NumElements;
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000738 Regs.append(NumElements, DAG.getUNDEF(VT));
Matt Arsenault758659232013-05-18 00:21:46 +0000739
Craig Topper48d114b2014-04-26 18:35:24 +0000740 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
Christian Konig2c8f6d52013-03-07 09:03:52 +0000741 continue;
742 }
743
744 InVals.push_back(Val);
745 }
Tom Stellarde99fb652015-01-20 19:33:04 +0000746
747 if (Info->getShaderType() != ShaderType::COMPUTE) {
Craig Topper0013be12015-09-21 05:32:41 +0000748 unsigned ScratchIdx = CCInfo.getFirstUnallocated(makeArrayRef(
Tim Northover3b6b7ca2015-02-21 02:11:17 +0000749 AMDGPU::SGPR_32RegClass.begin(), AMDGPU::SGPR_32RegClass.getNumRegs()));
Tom Stellarde99fb652015-01-20 19:33:04 +0000750 Info->ScratchOffsetReg = AMDGPU::SGPR_32RegClass.getRegister(ScratchIdx);
751 }
Matt Arsenaultcf13d182015-07-10 22:51:36 +0000752
753 if (Chains.empty())
754 return Chain;
755
756 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
Christian Konig2c8f6d52013-03-07 09:03:52 +0000757}
758
Tom Stellard75aadc22012-12-11 21:25:42 +0000759MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
760 MachineInstr * MI, MachineBasicBlock * BB) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000761
Tom Stellard556d9aa2013-06-03 17:39:37 +0000762 MachineBasicBlock::iterator I = *MI;
Eric Christopher7792e322015-01-30 23:24:40 +0000763 const SIInstrInfo *TII =
764 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Tom Stellard556d9aa2013-06-03 17:39:37 +0000765
Tom Stellard75aadc22012-12-11 21:25:42 +0000766 switch (MI->getOpcode()) {
767 default:
768 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
Matt Arsenault20711b72015-02-20 22:10:45 +0000769 case AMDGPU::BRANCH:
770 return BB;
Tom Stellard81d871d2013-11-13 23:36:50 +0000771 case AMDGPU::SI_RegisterStorePseudo: {
772 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
Tom Stellard81d871d2013-11-13 23:36:50 +0000773 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
774 MachineInstrBuilder MIB =
775 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
776 Reg);
777 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
778 MIB.addOperand(MI->getOperand(i));
779
780 MI->eraseFromParent();
Vincent Lejeune79a58342014-05-10 19:18:25 +0000781 break;
782 }
Tom Stellard75aadc22012-12-11 21:25:42 +0000783 }
784 return BB;
785}
786
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000787bool SITargetLowering::enableAggressiveFMAFusion(EVT VT) const {
788 // This currently forces unfolding various combinations of fsub into fma with
789 // free fneg'd operands. As long as we have fast FMA (controlled by
790 // isFMAFasterThanFMulAndFAdd), we should perform these.
791
792 // When fma is quarter rate, for f64 where add / sub are at best half rate,
793 // most of these combines appear to be cycle neutral but save on instruction
794 // count / code size.
795 return true;
796}
797
Mehdi Amini44ede332015-07-09 02:09:04 +0000798EVT SITargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
799 EVT VT) const {
Tom Stellard83747202013-07-18 21:43:53 +0000800 if (!VT.isVector()) {
801 return MVT::i1;
802 }
Matt Arsenault8596f712014-11-28 22:51:38 +0000803 return EVT::getVectorVT(Ctx, MVT::i1, VT.getVectorNumElements());
Tom Stellard75aadc22012-12-11 21:25:42 +0000804}
805
Mehdi Aminieaabc512015-07-09 15:12:23 +0000806MVT SITargetLowering::getScalarShiftAmountTy(const DataLayout &, EVT) const {
Christian Konig082a14a2013-03-18 11:34:05 +0000807 return MVT::i32;
808}
809
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000810// Answering this is somewhat tricky and depends on the specific device which
811// have different rates for fma or all f64 operations.
812//
813// v_fma_f64 and v_mul_f64 always take the same number of cycles as each other
814// regardless of which device (although the number of cycles differs between
815// devices), so it is always profitable for f64.
816//
817// v_fma_f32 takes 4 or 16 cycles depending on the device, so it is profitable
818// only on full rate devices. Normally, we should prefer selecting v_mad_f32
819// which we can always do even without fused FP ops since it returns the same
820// result as the separate operations and since it is always full
821// rate. Therefore, we lie and report that it is not faster for f32. v_mad_f32
822// however does not support denormals, so we do report fma as faster if we have
823// a fast fma device and require denormals.
824//
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000825bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
826 VT = VT.getScalarType();
827
828 if (!VT.isSimple())
829 return false;
830
831 switch (VT.getSimpleVT().SimpleTy) {
832 case MVT::f32:
Matt Arsenault423bf3f2015-01-29 19:34:32 +0000833 // This is as fast on some subtargets. However, we always have full rate f32
834 // mad available which returns the same result as the separate operations
Matt Arsenault8d630032015-02-20 22:10:41 +0000835 // which we should prefer over fma. We can't use this if we want to support
836 // denormals, so only report this in these cases.
837 return Subtarget->hasFP32Denormals() && Subtarget->hasFastFMAF32();
Niels Ole Salscheiderd3a039f2013-08-10 10:38:54 +0000838 case MVT::f64:
839 return true;
840 default:
841 break;
842 }
843
844 return false;
845}
846
Tom Stellard75aadc22012-12-11 21:25:42 +0000847//===----------------------------------------------------------------------===//
848// Custom DAG Lowering Operations
849//===----------------------------------------------------------------------===//
850
851SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
852 switch (Op.getOpcode()) {
853 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +0000854 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
Tom Stellardf8794352012-12-19 22:10:31 +0000855 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Tom Stellard35bb18c2013-08-26 15:06:04 +0000856 case ISD::LOAD: {
Tom Stellarde812f2f2014-07-21 15:45:06 +0000857 SDValue Result = LowerLOAD(Op, DAG);
858 assert((!Result.getNode() ||
859 Result.getNode()->getNumValues() == 2) &&
860 "Load should return a value and a chain");
861 return Result;
Tom Stellard35bb18c2013-08-26 15:06:04 +0000862 }
Tom Stellardaf775432013-10-23 00:44:32 +0000863
Matt Arsenaultad14ce82014-07-19 18:44:39 +0000864 case ISD::FSIN:
865 case ISD::FCOS:
866 return LowerTrig(Op, DAG);
Tom Stellard0ec134f2014-02-04 17:18:40 +0000867 case ISD::SELECT: return LowerSELECT(Op, DAG);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +0000868 case ISD::FDIV: return LowerFDIV(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +0000869 case ISD::STORE: return LowerSTORE(Op, DAG);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000870 case ISD::GlobalAddress: {
871 MachineFunction &MF = DAG.getMachineFunction();
872 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
873 return LowerGlobalAddress(MFI, Op, DAG);
Tom Stellard94593ee2013-06-03 17:40:18 +0000874 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +0000875 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
876 case ISD::INTRINSIC_VOID: return LowerINTRINSIC_VOID(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000877 }
878 return SDValue();
879}
880
Tom Stellardf8794352012-12-19 22:10:31 +0000881/// \brief Helper function for LowerBRCOND
882static SDNode *findUser(SDValue Value, unsigned Opcode) {
Tom Stellard75aadc22012-12-11 21:25:42 +0000883
Tom Stellardf8794352012-12-19 22:10:31 +0000884 SDNode *Parent = Value.getNode();
885 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
886 I != E; ++I) {
887
888 if (I.getUse().get() != Value)
889 continue;
890
891 if (I->getOpcode() == Opcode)
892 return *I;
893 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000894 return nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000895}
896
Tom Stellardb02094e2014-07-21 15:45:01 +0000897SDValue SITargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
898
Tom Stellardc98ee202015-07-16 19:40:07 +0000899 SDLoc SL(Op);
Tom Stellardb02094e2014-07-21 15:45:01 +0000900 FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Op);
901 unsigned FrameIndex = FINode->getIndex();
902
Tom Stellardc98ee202015-07-16 19:40:07 +0000903 // A FrameIndex node represents a 32-bit offset into scratch memory. If
904 // the high bit of a frame index offset were to be set, this would mean
905 // that it represented an offset of ~2GB * 64 = ~128GB from the start of the
906 // scratch buffer, with 64 being the number of threads per wave.
907 //
908 // If we know the machine uses less than 128GB of scratch, then we can
909 // amrk the high bit of the FrameIndex node as known zero,
910 // which is important, because it means in most situations we can
911 // prove that values derived from FrameIndex nodes are non-negative.
912 // This enables us to take advantage of more addressing modes when
913 // accessing scratch buffers, since for scratch reads/writes, the register
914 // offset must always be positive.
915
916 SDValue TFI = DAG.getTargetFrameIndex(FrameIndex, MVT::i32);
917 if (Subtarget->enableHugeScratchBuffer())
918 return TFI;
919
920 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, TFI,
921 DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), 31)));
Tom Stellardb02094e2014-07-21 15:45:01 +0000922}
923
Tom Stellardf8794352012-12-19 22:10:31 +0000924/// This transforms the control flow intrinsics to get the branch destination as
925/// last parameter, also switches branch target with BR if the need arise
926SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
927 SelectionDAG &DAG) const {
928
Andrew Trickef9de2a2013-05-25 02:42:55 +0000929 SDLoc DL(BRCOND);
Tom Stellardf8794352012-12-19 22:10:31 +0000930
931 SDNode *Intr = BRCOND.getOperand(1).getNode();
932 SDValue Target = BRCOND.getOperand(2);
Craig Topper062a2ba2014-04-25 05:30:21 +0000933 SDNode *BR = nullptr;
Tom Stellardf8794352012-12-19 22:10:31 +0000934
935 if (Intr->getOpcode() == ISD::SETCC) {
936 // As long as we negate the condition everything is fine
937 SDNode *SetCC = Intr;
938 assert(SetCC->getConstantOperandVal(1) == 1);
NAKAMURA Takumi458a8272013-01-07 11:14:44 +0000939 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
940 ISD::SETNE);
Tom Stellardf8794352012-12-19 22:10:31 +0000941 Intr = SetCC->getOperand(0).getNode();
942
943 } else {
944 // Get the target from BR if we don't negate the condition
945 BR = findUser(BRCOND, ISD::BR);
946 Target = BR->getOperand(1);
947 }
948
949 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
950
951 // Build the result and
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000952 ArrayRef<EVT> Res(Intr->value_begin() + 1, Intr->value_end());
Tom Stellardf8794352012-12-19 22:10:31 +0000953
954 // operands of the new intrinsic call
955 SmallVector<SDValue, 4> Ops;
956 Ops.push_back(BRCOND.getOperand(0));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000957 Ops.append(Intr->op_begin() + 1, Intr->op_end());
Tom Stellardf8794352012-12-19 22:10:31 +0000958 Ops.push_back(Target);
959
960 // build the new intrinsic call
961 SDNode *Result = DAG.getNode(
962 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
Craig Topper48d114b2014-04-26 18:35:24 +0000963 DAG.getVTList(Res), Ops).getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000964
965 if (BR) {
966 // Give the branch instruction our target
967 SDValue Ops[] = {
968 BR->getOperand(0),
969 BRCOND.getOperand(2)
970 };
Chandler Carruth356665a2014-08-01 22:09:43 +0000971 SDValue NewBR = DAG.getNode(ISD::BR, DL, BR->getVTList(), Ops);
972 DAG.ReplaceAllUsesWith(BR, NewBR.getNode());
973 BR = NewBR.getNode();
Tom Stellardf8794352012-12-19 22:10:31 +0000974 }
975
976 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
977
978 // Copy the intrinsic results to registers
979 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
980 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
981 if (!CopyToReg)
982 continue;
983
984 Chain = DAG.getCopyToReg(
985 Chain, DL,
986 CopyToReg->getOperand(1),
987 SDValue(Result, i - 1),
988 SDValue());
989
990 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
991 }
992
993 // Remove the old intrinsic from the chain
994 DAG.ReplaceAllUsesOfValueWith(
995 SDValue(Intr, Intr->getNumValues() - 1),
996 Intr->getOperand(0));
997
998 return Chain;
Tom Stellard75aadc22012-12-11 21:25:42 +0000999}
1000
Tom Stellard067c8152014-07-21 14:01:14 +00001001SDValue SITargetLowering::LowerGlobalAddress(AMDGPUMachineFunction *MFI,
1002 SDValue Op,
1003 SelectionDAG &DAG) const {
1004 GlobalAddressSDNode *GSD = cast<GlobalAddressSDNode>(Op);
1005
1006 if (GSD->getAddressSpace() != AMDGPUAS::CONSTANT_ADDRESS)
1007 return AMDGPUTargetLowering::LowerGlobalAddress(MFI, Op, DAG);
1008
1009 SDLoc DL(GSD);
1010 const GlobalValue *GV = GSD->getGlobal();
Mehdi Amini44ede332015-07-09 02:09:04 +00001011 MVT PtrVT = getPointerTy(DAG.getDataLayout(), GSD->getAddressSpace());
Tom Stellard067c8152014-07-21 14:01:14 +00001012
1013 SDValue Ptr = DAG.getNode(AMDGPUISD::CONST_DATA_PTR, DL, PtrVT);
1014 SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32);
1015
1016 SDValue PtrLo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001017 DAG.getConstant(0, DL, MVT::i32));
Tom Stellard067c8152014-07-21 14:01:14 +00001018 SDValue PtrHi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, Ptr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001019 DAG.getConstant(1, DL, MVT::i32));
Tom Stellard067c8152014-07-21 14:01:14 +00001020
1021 SDValue Lo = DAG.getNode(ISD::ADDC, DL, DAG.getVTList(MVT::i32, MVT::Glue),
1022 PtrLo, GA);
1023 SDValue Hi = DAG.getNode(ISD::ADDE, DL, DAG.getVTList(MVT::i32, MVT::Glue),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001024 PtrHi, DAG.getConstant(0, DL, MVT::i32),
Tom Stellard067c8152014-07-21 14:01:14 +00001025 SDValue(Lo.getNode(), 1));
1026 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
1027}
1028
Tom Stellardfc92e772015-05-12 14:18:14 +00001029SDValue SITargetLowering::copyToM0(SelectionDAG &DAG, SDValue Chain, SDLoc DL,
1030 SDValue V) const {
1031 // We can't use CopyToReg, because MachineCSE won't combine COPY instructions,
1032 // so we will end up with redundant moves to m0.
1033 //
1034 // We can't use S_MOV_B32, because there is no way to specify m0 as the
1035 // destination register.
1036 //
1037 // We have to use them both. Machine cse will combine all the S_MOV_B32
1038 // instructions and the register coalescer eliminate the extra copies.
1039 SDNode *M0 = DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, V.getValueType(), V);
1040 return DAG.getCopyToReg(Chain, DL, DAG.getRegister(AMDGPU::M0, MVT::i32),
1041 SDValue(M0, 0), SDValue()); // Glue
1042 // A Null SDValue creates
1043 // a glue result.
1044}
1045
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001046SDValue SITargetLowering::lowerImplicitZextParam(SelectionDAG &DAG,
1047 SDValue Op,
1048 MVT VT,
1049 unsigned Offset) const {
1050 SDLoc SL(Op);
1051 SDValue Param = LowerParameter(DAG, MVT::i32, MVT::i32, SL,
1052 DAG.getEntryNode(), Offset, false);
1053 // The local size values will have the hi 16-bits as zero.
1054 return DAG.getNode(ISD::AssertZext, SL, MVT::i32, Param,
1055 DAG.getValueType(VT));
1056}
1057
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001058SDValue SITargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
1059 SelectionDAG &DAG) const {
1060 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellarddcb9f092015-07-09 21:20:37 +00001061 auto MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellardec2e43c2014-09-22 15:35:29 +00001062 const SIRegisterInfo *TRI =
Eric Christopher7792e322015-01-30 23:24:40 +00001063 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo());
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001064
1065 EVT VT = Op.getValueType();
1066 SDLoc DL(Op);
1067 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1068
Sanjay Patela2607012015-09-16 16:31:21 +00001069 // TODO: Should this propagate fast-math-flags?
1070
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001071 switch (IntrinsicID) {
Tom Stellard48f29f22015-11-26 00:43:29 +00001072 case Intrinsic::amdgcn_dispatch_ptr:
1073 return CreateLiveInRegister(DAG, &AMDGPU::SReg_64RegClass,
1074 TRI->getPreloadedValue(MF, SIRegisterInfo::DISPATCH_PTR), VT);
1075
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001076 case Intrinsic::r600_read_ngroups_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001077 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1078 SI::KernelInputOffsets::NGROUPS_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001079 case Intrinsic::r600_read_ngroups_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001080 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1081 SI::KernelInputOffsets::NGROUPS_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001082 case Intrinsic::r600_read_ngroups_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001083 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1084 SI::KernelInputOffsets::NGROUPS_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001085 case Intrinsic::r600_read_global_size_x:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001086 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1087 SI::KernelInputOffsets::GLOBAL_SIZE_X, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001088 case Intrinsic::r600_read_global_size_y:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001089 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1090 SI::KernelInputOffsets::GLOBAL_SIZE_Y, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001091 case Intrinsic::r600_read_global_size_z:
Tom Stellardec2e43c2014-09-22 15:35:29 +00001092 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(),
1093 SI::KernelInputOffsets::GLOBAL_SIZE_Z, false);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001094 case Intrinsic::r600_read_local_size_x:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001095 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1096 SI::KernelInputOffsets::LOCAL_SIZE_X);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001097 case Intrinsic::r600_read_local_size_y:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001098 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1099 SI::KernelInputOffsets::LOCAL_SIZE_Y);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001100 case Intrinsic::r600_read_local_size_z:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001101 return lowerImplicitZextParam(DAG, Op, MVT::i16,
1102 SI::KernelInputOffsets::LOCAL_SIZE_Z);
Jan Veselye5121f32014-10-14 20:05:26 +00001103 case Intrinsic::AMDGPU_read_workdim:
Matt Arsenaultff6da2f2015-11-30 21:15:45 +00001104 // Really only 2 bits.
1105 return lowerImplicitZextParam(DAG, Op, MVT::i8,
1106 getImplicitParameterOffset(MFI, GRID_DIM));
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001107 case Intrinsic::r600_read_tgid_x:
1108 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +00001109 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001110 case Intrinsic::r600_read_tgid_y:
1111 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +00001112 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001113 case Intrinsic::r600_read_tgid_z:
1114 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +00001115 TRI->getPreloadedValue(MF, SIRegisterInfo::TGID_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001116 case Intrinsic::r600_read_tidig_x:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001117 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +00001118 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_X), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001119 case Intrinsic::r600_read_tidig_y:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001120 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +00001121 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Y), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001122 case Intrinsic::r600_read_tidig_z:
Tom Stellard45c0b3a2015-01-07 20:59:25 +00001123 return CreateLiveInRegister(DAG, &AMDGPU::VGPR_32RegClass,
Tom Stellardec2e43c2014-09-22 15:35:29 +00001124 TRI->getPreloadedValue(MF, SIRegisterInfo::TIDIG_Z), VT);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001125 case AMDGPUIntrinsic::SI_load_const: {
1126 SDValue Ops[] = {
1127 Op.getOperand(1),
1128 Op.getOperand(2)
1129 };
1130
1131 MachineMemOperand *MMO = MF.getMachineMemOperand(
1132 MachinePointerInfo(),
1133 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
1134 VT.getStoreSize(), 4);
1135 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
1136 Op->getVTList(), Ops, VT, MMO);
1137 }
1138 case AMDGPUIntrinsic::SI_sample:
1139 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
1140 case AMDGPUIntrinsic::SI_sampleb:
1141 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
1142 case AMDGPUIntrinsic::SI_sampled:
1143 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
1144 case AMDGPUIntrinsic::SI_samplel:
1145 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
1146 case AMDGPUIntrinsic::SI_vs_load_input:
1147 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
1148 Op.getOperand(1),
1149 Op.getOperand(2),
1150 Op.getOperand(3));
Marek Olsak43650e42015-03-24 13:40:08 +00001151
1152 case AMDGPUIntrinsic::AMDGPU_fract:
1153 case AMDGPUIntrinsic::AMDIL_fraction: // Legacy name.
1154 return DAG.getNode(ISD::FSUB, DL, VT, Op.getOperand(1),
1155 DAG.getNode(ISD::FFLOOR, DL, VT, Op.getOperand(1)));
Tom Stellard2a9d9472015-05-12 15:00:46 +00001156 case AMDGPUIntrinsic::SI_fs_constant: {
1157 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1158 SDValue Glue = M0.getValue(1);
1159 return DAG.getNode(AMDGPUISD::INTERP_MOV, DL, MVT::f32,
1160 DAG.getConstant(2, DL, MVT::i32), // P0
1161 Op.getOperand(1), Op.getOperand(2), Glue);
1162 }
Marek Olsak6f6d3182015-10-29 15:29:09 +00001163 case AMDGPUIntrinsic::SI_packf16:
1164 if (Op.getOperand(1).isUndef() && Op.getOperand(2).isUndef())
1165 return DAG.getUNDEF(MVT::i32);
1166 return Op;
Tom Stellard2a9d9472015-05-12 15:00:46 +00001167 case AMDGPUIntrinsic::SI_fs_interp: {
1168 SDValue IJ = Op.getOperand(4);
1169 SDValue I = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1170 DAG.getConstant(0, DL, MVT::i32));
1171 SDValue J = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, IJ,
1172 DAG.getConstant(1, DL, MVT::i32));
1173 SDValue M0 = copyToM0(DAG, DAG.getEntryNode(), DL, Op.getOperand(3));
1174 SDValue Glue = M0.getValue(1);
1175 SDValue P1 = DAG.getNode(AMDGPUISD::INTERP_P1, DL,
1176 DAG.getVTList(MVT::f32, MVT::Glue),
1177 I, Op.getOperand(1), Op.getOperand(2), Glue);
1178 Glue = SDValue(P1.getNode(), 1);
1179 return DAG.getNode(AMDGPUISD::INTERP_P2, DL, MVT::f32, P1, J,
1180 Op.getOperand(1), Op.getOperand(2), Glue);
1181 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001182 default:
1183 return AMDGPUTargetLowering::LowerOperation(Op, DAG);
1184 }
1185}
1186
1187SDValue SITargetLowering::LowerINTRINSIC_VOID(SDValue Op,
1188 SelectionDAG &DAG) const {
1189 MachineFunction &MF = DAG.getMachineFunction();
Tom Stellardfc92e772015-05-12 14:18:14 +00001190 SDLoc DL(Op);
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001191 SDValue Chain = Op.getOperand(0);
1192 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1193
1194 switch (IntrinsicID) {
Tom Stellardfc92e772015-05-12 14:18:14 +00001195 case AMDGPUIntrinsic::SI_sendmsg: {
1196 Chain = copyToM0(DAG, Chain, DL, Op.getOperand(3));
1197 SDValue Glue = Chain.getValue(1);
1198 return DAG.getNode(AMDGPUISD::SENDMSG, DL, MVT::Other, Chain,
1199 Op.getOperand(2), Glue);
1200 }
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001201 case AMDGPUIntrinsic::SI_tbuffer_store: {
Matt Arsenaulta5789bb2014-07-26 06:23:37 +00001202 SDValue Ops[] = {
1203 Chain,
1204 Op.getOperand(2),
1205 Op.getOperand(3),
1206 Op.getOperand(4),
1207 Op.getOperand(5),
1208 Op.getOperand(6),
1209 Op.getOperand(7),
1210 Op.getOperand(8),
1211 Op.getOperand(9),
1212 Op.getOperand(10),
1213 Op.getOperand(11),
1214 Op.getOperand(12),
1215 Op.getOperand(13),
1216 Op.getOperand(14)
1217 };
1218
1219 EVT VT = Op.getOperand(3).getValueType();
1220
1221 MachineMemOperand *MMO = MF.getMachineMemOperand(
1222 MachinePointerInfo(),
1223 MachineMemOperand::MOStore,
1224 VT.getStoreSize(), 4);
1225 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
1226 Op->getVTList(), Ops, VT, MMO);
1227 }
1228 default:
1229 return SDValue();
1230 }
1231}
1232
Tom Stellard81d871d2013-11-13 23:36:50 +00001233SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1234 SDLoc DL(Op);
1235 LoadSDNode *Load = cast<LoadSDNode>(Op);
1236
Tom Stellarde812f2f2014-07-21 15:45:06 +00001237 if (Op.getValueType().isVector()) {
1238 assert(Op.getValueType().getVectorElementType() == MVT::i32 &&
1239 "Custom lowering for non-i32 vectors hasn't been implemented.");
1240 unsigned NumElements = Op.getValueType().getVectorNumElements();
1241 assert(NumElements != 2 && "v2 loads are supported for all address spaces.");
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001242
Tom Stellarde812f2f2014-07-21 15:45:06 +00001243 switch (Load->getAddressSpace()) {
1244 default: break;
1245 case AMDGPUAS::GLOBAL_ADDRESS:
1246 case AMDGPUAS::PRIVATE_ADDRESS:
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001247 if (NumElements >= 8)
1248 return SplitVectorLoad(Op, DAG);
1249
Tom Stellarde812f2f2014-07-21 15:45:06 +00001250 // v4 loads are supported for private and global memory.
1251 if (NumElements <= 4)
1252 break;
1253 // fall-through
1254 case AMDGPUAS::LOCAL_ADDRESS:
Matt Arsenaultff05da82015-11-24 12:18:54 +00001255 // If properly aligned, if we split we might be able to use ds_read_b64.
1256 return SplitVectorLoad(Op, DAG);
Tom Stellarde812f2f2014-07-21 15:45:06 +00001257 }
Tom Stellarde9373602014-01-22 19:24:14 +00001258 }
Tom Stellard81d871d2013-11-13 23:36:50 +00001259
Tom Stellarde812f2f2014-07-21 15:45:06 +00001260 return AMDGPUTargetLowering::LowerLOAD(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001261}
1262
Tom Stellard9fa17912013-08-14 23:24:45 +00001263SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
1264 const SDValue &Op,
1265 SelectionDAG &DAG) const {
1266 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
1267 Op.getOperand(2),
Tom Stellard868fd922014-04-17 21:00:11 +00001268 Op.getOperand(3),
Tom Stellard9fa17912013-08-14 23:24:45 +00001269 Op.getOperand(4));
1270}
1271
Tom Stellard0ec134f2014-02-04 17:18:40 +00001272SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
1273 if (Op.getValueType() != MVT::i64)
1274 return SDValue();
1275
1276 SDLoc DL(Op);
1277 SDValue Cond = Op.getOperand(0);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001278
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001279 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
1280 SDValue One = DAG.getConstant(1, DL, MVT::i32);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001281
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001282 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
1283 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
1284
1285 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
1286 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001287
1288 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
1289
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001290 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
1291 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001292
1293 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
1294
Tom Stellard7ea3d6d2014-03-31 14:01:55 +00001295 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
1296 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
Tom Stellard0ec134f2014-02-04 17:18:40 +00001297}
1298
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001299// Catch division cases where we can use shortcuts with rcp and rsq
1300// instructions.
1301SDValue SITargetLowering::LowerFastFDIV(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001302 SDLoc SL(Op);
1303 SDValue LHS = Op.getOperand(0);
1304 SDValue RHS = Op.getOperand(1);
1305 EVT VT = Op.getValueType();
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001306 bool Unsafe = DAG.getTarget().Options.UnsafeFPMath;
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001307
1308 if (const ConstantFPSDNode *CLHS = dyn_cast<ConstantFPSDNode>(LHS)) {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001309 if ((Unsafe || (VT == MVT::f32 && !Subtarget->hasFP32Denormals())) &&
1310 CLHS->isExactlyValue(1.0)) {
1311 // v_rcp_f32 and v_rsq_f32 do not support denormals, and according to
1312 // the CI documentation has a worst case error of 1 ulp.
1313 // OpenCL requires <= 2.5 ulp for 1.0 / x, so it should always be OK to
1314 // use it as long as we aren't trying to use denormals.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001315
1316 // 1.0 / sqrt(x) -> rsq(x)
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001317 //
1318 // XXX - Is UnsafeFPMath sufficient to do this for f64? The maximum ULP
1319 // error seems really high at 2^29 ULP.
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001320 if (RHS.getOpcode() == ISD::FSQRT)
1321 return DAG.getNode(AMDGPUISD::RSQ, SL, VT, RHS.getOperand(0));
1322
1323 // 1.0 / x -> rcp(x)
1324 return DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
1325 }
1326 }
1327
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001328 if (Unsafe) {
1329 // Turn into multiply by the reciprocal.
1330 // x / y -> x * (1.0 / y)
Sanjay Patela2607012015-09-16 16:31:21 +00001331 SDNodeFlags Flags;
1332 Flags.setUnsafeAlgebra(true);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001333 SDValue Recip = DAG.getNode(AMDGPUISD::RCP, SL, VT, RHS);
Sanjay Patela2607012015-09-16 16:31:21 +00001334 return DAG.getNode(ISD::FMUL, SL, VT, LHS, Recip, &Flags);
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001335 }
1336
1337 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001338}
1339
1340SDValue SITargetLowering::LowerFDIV32(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault22ca3f82014-07-15 23:50:10 +00001341 SDValue FastLowered = LowerFastFDIV(Op, DAG);
1342 if (FastLowered.getNode())
1343 return FastLowered;
1344
1345 // This uses v_rcp_f32 which does not handle denormals. Let this hit a
1346 // selection error for now rather than do something incorrect.
1347 if (Subtarget->hasFP32Denormals())
1348 return SDValue();
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001349
1350 SDLoc SL(Op);
1351 SDValue LHS = Op.getOperand(0);
1352 SDValue RHS = Op.getOperand(1);
1353
1354 SDValue r1 = DAG.getNode(ISD::FABS, SL, MVT::f32, RHS);
1355
1356 const APFloat K0Val(BitsToFloat(0x6f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001357 const SDValue K0 = DAG.getConstantFP(K0Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001358
1359 const APFloat K1Val(BitsToFloat(0x2f800000));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001360 const SDValue K1 = DAG.getConstantFP(K1Val, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001361
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001362 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001363
Mehdi Amini44ede332015-07-09 02:09:04 +00001364 EVT SetCCVT =
1365 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001366
1367 SDValue r2 = DAG.getSetCC(SL, SetCCVT, r1, K0, ISD::SETOGT);
1368
1369 SDValue r3 = DAG.getNode(ISD::SELECT, SL, MVT::f32, r2, K1, One);
1370
Sanjay Patela2607012015-09-16 16:31:21 +00001371 // TODO: Should this propagate fast-math-flags?
1372
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001373 r1 = DAG.getNode(ISD::FMUL, SL, MVT::f32, RHS, r3);
1374
1375 SDValue r0 = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f32, r1);
1376
1377 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f32, LHS, r0);
1378
1379 return DAG.getNode(ISD::FMUL, SL, MVT::f32, r3, Mul);
1380}
1381
1382SDValue SITargetLowering::LowerFDIV64(SDValue Op, SelectionDAG &DAG) const {
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001383 if (DAG.getTarget().Options.UnsafeFPMath)
1384 return LowerFastFDIV(Op, DAG);
1385
1386 SDLoc SL(Op);
1387 SDValue X = Op.getOperand(0);
1388 SDValue Y = Op.getOperand(1);
1389
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001390 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001391
1392 SDVTList ScaleVT = DAG.getVTList(MVT::f64, MVT::i1);
1393
1394 SDValue DivScale0 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, Y, Y, X);
1395
1396 SDValue NegDivScale0 = DAG.getNode(ISD::FNEG, SL, MVT::f64, DivScale0);
1397
1398 SDValue Rcp = DAG.getNode(AMDGPUISD::RCP, SL, MVT::f64, DivScale0);
1399
1400 SDValue Fma0 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Rcp, One);
1401
1402 SDValue Fma1 = DAG.getNode(ISD::FMA, SL, MVT::f64, Rcp, Fma0, Rcp);
1403
1404 SDValue Fma2 = DAG.getNode(ISD::FMA, SL, MVT::f64, NegDivScale0, Fma1, One);
1405
1406 SDValue DivScale1 = DAG.getNode(AMDGPUISD::DIV_SCALE, SL, ScaleVT, X, Y, X);
1407
1408 SDValue Fma3 = DAG.getNode(ISD::FMA, SL, MVT::f64, Fma1, Fma2, Fma1);
1409 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, DivScale1, Fma3);
1410
1411 SDValue Fma4 = DAG.getNode(ISD::FMA, SL, MVT::f64,
1412 NegDivScale0, Mul, DivScale1);
1413
1414 SDValue Scale;
1415
1416 if (Subtarget->getGeneration() == AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1417 // Workaround a hardware bug on SI where the condition output from div_scale
1418 // is not usable.
1419
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001420 const SDValue Hi = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault0bbcd8b2015-02-14 04:30:08 +00001421
1422 // Figure out if the scale to use for div_fmas.
1423 SDValue NumBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1424 SDValue DenBC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Y);
1425 SDValue Scale0BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale0);
1426 SDValue Scale1BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, DivScale1);
1427
1428 SDValue NumHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, NumBC, Hi);
1429 SDValue DenHi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, DenBC, Hi);
1430
1431 SDValue Scale0Hi
1432 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale0BC, Hi);
1433 SDValue Scale1Hi
1434 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Scale1BC, Hi);
1435
1436 SDValue CmpDen = DAG.getSetCC(SL, MVT::i1, DenHi, Scale0Hi, ISD::SETEQ);
1437 SDValue CmpNum = DAG.getSetCC(SL, MVT::i1, NumHi, Scale1Hi, ISD::SETEQ);
1438 Scale = DAG.getNode(ISD::XOR, SL, MVT::i1, CmpNum, CmpDen);
1439 } else {
1440 Scale = DivScale1.getValue(1);
1441 }
1442
1443 SDValue Fmas = DAG.getNode(AMDGPUISD::DIV_FMAS, SL, MVT::f64,
1444 Fma4, Fma3, Mul, Scale);
1445
1446 return DAG.getNode(AMDGPUISD::DIV_FIXUP, SL, MVT::f64, Fmas, Y, X);
Matt Arsenaulte9fa3b82014-07-15 20:18:31 +00001447}
1448
1449SDValue SITargetLowering::LowerFDIV(SDValue Op, SelectionDAG &DAG) const {
1450 EVT VT = Op.getValueType();
1451
1452 if (VT == MVT::f32)
1453 return LowerFDIV32(Op, DAG);
1454
1455 if (VT == MVT::f64)
1456 return LowerFDIV64(Op, DAG);
1457
1458 llvm_unreachable("Unexpected type for fdiv");
1459}
1460
Tom Stellard81d871d2013-11-13 23:36:50 +00001461SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1462 SDLoc DL(Op);
1463 StoreSDNode *Store = cast<StoreSDNode>(Op);
1464 EVT VT = Store->getMemoryVT();
1465
Tom Stellard9b3816b2014-06-24 23:33:04 +00001466 // These stores are legal.
Tom Stellardb02094e2014-07-21 15:45:01 +00001467 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) {
1468 if (VT.isVector() && VT.getVectorNumElements() > 4)
Matt Arsenault83e60582014-07-24 17:10:35 +00001469 return ScalarizeVectorStore(Op, DAG);
Tom Stellardb02094e2014-07-21 15:45:01 +00001470 return SDValue();
1471 }
1472
Tom Stellard81d871d2013-11-13 23:36:50 +00001473 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1474 if (Ret.getNode())
1475 return Ret;
1476
1477 if (VT.isVector() && VT.getVectorNumElements() >= 8)
Matt Arsenault4d801cd2015-11-24 12:05:03 +00001478 return SplitVectorStore(Op, DAG);
Tom Stellard81d871d2013-11-13 23:36:50 +00001479
Tom Stellard1c8788e2014-03-07 20:12:33 +00001480 if (VT == MVT::i1)
1481 return DAG.getTruncStore(Store->getChain(), DL,
1482 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
1483 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
1484
Tom Stellarde812f2f2014-07-21 15:45:06 +00001485 return SDValue();
Tom Stellard81d871d2013-11-13 23:36:50 +00001486}
1487
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001488SDValue SITargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001489 SDLoc DL(Op);
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001490 EVT VT = Op.getValueType();
1491 SDValue Arg = Op.getOperand(0);
Sanjay Patela2607012015-09-16 16:31:21 +00001492 // TODO: Should this propagate fast-math-flags?
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001493 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
1494 DAG.getNode(ISD::FMUL, DL, VT, Arg,
1495 DAG.getConstantFP(0.5/M_PI, DL,
1496 VT)));
Matt Arsenaultad14ce82014-07-19 18:44:39 +00001497
1498 switch (Op.getOpcode()) {
1499 case ISD::FCOS:
1500 return DAG.getNode(AMDGPUISD::COS_HW, SDLoc(Op), VT, FractPart);
1501 case ISD::FSIN:
1502 return DAG.getNode(AMDGPUISD::SIN_HW, SDLoc(Op), VT, FractPart);
1503 default:
1504 llvm_unreachable("Wrong trig opcode");
1505 }
1506}
1507
Tom Stellard75aadc22012-12-11 21:25:42 +00001508//===----------------------------------------------------------------------===//
1509// Custom DAG optimizations
1510//===----------------------------------------------------------------------===//
1511
Matt Arsenault364a6742014-06-11 17:50:44 +00001512SDValue SITargetLowering::performUCharToFloatCombine(SDNode *N,
Matt Arsenaulte6986632015-01-14 01:35:22 +00001513 DAGCombinerInfo &DCI) const {
Matt Arsenault364a6742014-06-11 17:50:44 +00001514 EVT VT = N->getValueType(0);
1515 EVT ScalarVT = VT.getScalarType();
1516 if (ScalarVT != MVT::f32)
1517 return SDValue();
1518
1519 SelectionDAG &DAG = DCI.DAG;
1520 SDLoc DL(N);
1521
1522 SDValue Src = N->getOperand(0);
1523 EVT SrcVT = Src.getValueType();
1524
1525 // TODO: We could try to match extracting the higher bytes, which would be
1526 // easier if i8 vectors weren't promoted to i32 vectors, particularly after
1527 // types are legalized. v4i8 -> v4f32 is probably the only case to worry
1528 // about in practice.
1529 if (DCI.isAfterLegalizeVectorOps() && SrcVT == MVT::i32) {
1530 if (DAG.MaskedValueIsZero(Src, APInt::getHighBitsSet(32, 24))) {
1531 SDValue Cvt = DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Src);
1532 DCI.AddToWorklist(Cvt.getNode());
1533 return Cvt;
1534 }
1535 }
1536
1537 // We are primarily trying to catch operations on illegal vector types
1538 // before they are expanded.
1539 // For scalars, we can use the more flexible method of checking masked bits
1540 // after legalization.
1541 if (!DCI.isBeforeLegalize() ||
1542 !SrcVT.isVector() ||
1543 SrcVT.getVectorElementType() != MVT::i8) {
1544 return SDValue();
1545 }
1546
1547 assert(DCI.isBeforeLegalize() && "Unexpected legal type");
1548
1549 // Weird sized vectors are a pain to handle, but we know 3 is really the same
1550 // size as 4.
1551 unsigned NElts = SrcVT.getVectorNumElements();
1552 if (!SrcVT.isSimple() && NElts != 3)
1553 return SDValue();
1554
1555 // Handle v4i8 -> v4f32 extload. Replace the v4i8 with a legal i32 load to
1556 // prevent a mess from expanding to v4i32 and repacking.
1557 if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) {
1558 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), SrcVT);
1559 EVT RegVT = getEquivalentLoadRegType(*DAG.getContext(), SrcVT);
1560 EVT FloatVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, NElts);
Matt Arsenault364a6742014-06-11 17:50:44 +00001561 LoadSDNode *Load = cast<LoadSDNode>(Src);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001562
1563 unsigned AS = Load->getAddressSpace();
1564 unsigned Align = Load->getAlignment();
1565 Type *Ty = LoadVT.getTypeForEVT(*DAG.getContext());
Mehdi Aminia749f2a2015-07-09 02:09:52 +00001566 unsigned ABIAlignment = DAG.getDataLayout().getABITypeAlignment(Ty);
Matt Arsenaulte6986632015-01-14 01:35:22 +00001567
1568 // Don't try to replace the load if we have to expand it due to alignment
1569 // problems. Otherwise we will end up scalarizing the load, and trying to
1570 // repack into the vector for no real reason.
1571 if (Align < ABIAlignment &&
1572 !allowsMisalignedMemoryAccesses(LoadVT, AS, Align, nullptr)) {
1573 return SDValue();
1574 }
1575
Matt Arsenault364a6742014-06-11 17:50:44 +00001576 SDValue NewLoad = DAG.getExtLoad(ISD::ZEXTLOAD, DL, RegVT,
1577 Load->getChain(),
1578 Load->getBasePtr(),
1579 LoadVT,
1580 Load->getMemOperand());
1581
1582 // Make sure successors of the original load stay after it by updating
1583 // them to use the new Chain.
1584 DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), NewLoad.getValue(1));
1585
1586 SmallVector<SDValue, 4> Elts;
1587 if (RegVT.isVector())
1588 DAG.ExtractVectorElements(NewLoad, Elts);
1589 else
1590 Elts.push_back(NewLoad);
1591
1592 SmallVector<SDValue, 4> Ops;
1593
1594 unsigned EltIdx = 0;
1595 for (SDValue Elt : Elts) {
1596 unsigned ComponentsInElt = std::min(4u, NElts - 4 * EltIdx);
1597 for (unsigned I = 0; I < ComponentsInElt; ++I) {
1598 unsigned Opc = AMDGPUISD::CVT_F32_UBYTE0 + I;
1599 SDValue Cvt = DAG.getNode(Opc, DL, MVT::f32, Elt);
1600 DCI.AddToWorklist(Cvt.getNode());
1601 Ops.push_back(Cvt);
1602 }
1603
1604 ++EltIdx;
1605 }
1606
1607 assert(Ops.size() == NElts);
1608
1609 return DAG.getNode(ISD::BUILD_VECTOR, DL, FloatVT, Ops);
1610 }
1611
1612 return SDValue();
1613}
1614
Eric Christopher6c5b5112015-03-11 18:43:21 +00001615/// \brief Return true if the given offset Size in bytes can be folded into
1616/// the immediate offsets of a memory instruction for the given address space.
1617static bool canFoldOffset(unsigned OffsetSize, unsigned AS,
1618 const AMDGPUSubtarget &STI) {
1619 switch (AS) {
1620 case AMDGPUAS::GLOBAL_ADDRESS: {
1621 // MUBUF instructions a 12-bit offset in bytes.
1622 return isUInt<12>(OffsetSize);
1623 }
1624 case AMDGPUAS::CONSTANT_ADDRESS: {
1625 // SMRD instructions have an 8-bit offset in dwords on SI and
1626 // a 20-bit offset in bytes on VI.
1627 if (STI.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
1628 return isUInt<20>(OffsetSize);
1629 else
1630 return (OffsetSize % 4 == 0) && isUInt<8>(OffsetSize / 4);
1631 }
1632 case AMDGPUAS::LOCAL_ADDRESS:
1633 case AMDGPUAS::REGION_ADDRESS: {
1634 // The single offset versions have a 16-bit offset in bytes.
1635 return isUInt<16>(OffsetSize);
1636 }
1637 case AMDGPUAS::PRIVATE_ADDRESS:
1638 // Indirect register addressing does not use any offsets.
1639 default:
1640 return 0;
1641 }
1642}
1643
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001644// (shl (add x, c1), c2) -> add (shl x, c2), (shl c1, c2)
1645
1646// This is a variant of
1647// (mul (add x, c1), c2) -> add (mul x, c2), (mul c1, c2),
1648//
1649// The normal DAG combiner will do this, but only if the add has one use since
1650// that would increase the number of instructions.
1651//
1652// This prevents us from seeing a constant offset that can be folded into a
1653// memory instruction's addressing mode. If we know the resulting add offset of
1654// a pointer can be folded into an addressing offset, we can replace the pointer
1655// operand with the add of new constant offset. This eliminates one of the uses,
1656// and may allow the remaining use to also be simplified.
1657//
1658SDValue SITargetLowering::performSHLPtrCombine(SDNode *N,
1659 unsigned AddrSpace,
1660 DAGCombinerInfo &DCI) const {
1661 SDValue N0 = N->getOperand(0);
1662 SDValue N1 = N->getOperand(1);
1663
1664 if (N0.getOpcode() != ISD::ADD)
1665 return SDValue();
1666
1667 const ConstantSDNode *CN1 = dyn_cast<ConstantSDNode>(N1);
1668 if (!CN1)
1669 return SDValue();
1670
1671 const ConstantSDNode *CAdd = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1672 if (!CAdd)
1673 return SDValue();
1674
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001675 // If the resulting offset is too large, we can't fold it into the addressing
1676 // mode offset.
1677 APInt Offset = CAdd->getAPIntValue() << CN1->getAPIntValue();
Eric Christopher6c5b5112015-03-11 18:43:21 +00001678 if (!canFoldOffset(Offset.getZExtValue(), AddrSpace, *Subtarget))
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001679 return SDValue();
1680
1681 SelectionDAG &DAG = DCI.DAG;
1682 SDLoc SL(N);
1683 EVT VT = N->getValueType(0);
1684
1685 SDValue ShlX = DAG.getNode(ISD::SHL, SL, VT, N0.getOperand(0), N1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001686 SDValue COffset = DAG.getConstant(Offset, SL, MVT::i32);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00001687
1688 return DAG.getNode(ISD::ADD, SL, VT, ShlX, COffset);
1689}
1690
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001691SDValue SITargetLowering::performAndCombine(SDNode *N,
1692 DAGCombinerInfo &DCI) const {
1693 if (DCI.isBeforeLegalize())
1694 return SDValue();
1695
1696 SelectionDAG &DAG = DCI.DAG;
1697
1698 // (and (fcmp ord x, x), (fcmp une (fabs x), inf)) ->
1699 // fp_class x, ~(s_nan | q_nan | n_infinity | p_infinity)
1700 SDValue LHS = N->getOperand(0);
1701 SDValue RHS = N->getOperand(1);
1702
1703 if (LHS.getOpcode() == ISD::SETCC &&
1704 RHS.getOpcode() == ISD::SETCC) {
1705 ISD::CondCode LCC = cast<CondCodeSDNode>(LHS.getOperand(2))->get();
1706 ISD::CondCode RCC = cast<CondCodeSDNode>(RHS.getOperand(2))->get();
1707
1708 SDValue X = LHS.getOperand(0);
1709 SDValue Y = RHS.getOperand(0);
1710 if (Y.getOpcode() != ISD::FABS || Y.getOperand(0) != X)
1711 return SDValue();
1712
1713 if (LCC == ISD::SETO) {
1714 if (X != LHS.getOperand(1))
1715 return SDValue();
1716
1717 if (RCC == ISD::SETUNE) {
1718 const ConstantFPSDNode *C1 = dyn_cast<ConstantFPSDNode>(RHS.getOperand(1));
1719 if (!C1 || !C1->isInfinity() || C1->isNegative())
1720 return SDValue();
1721
1722 const uint32_t Mask = SIInstrFlags::N_NORMAL |
1723 SIInstrFlags::N_SUBNORMAL |
1724 SIInstrFlags::N_ZERO |
1725 SIInstrFlags::P_ZERO |
1726 SIInstrFlags::P_SUBNORMAL |
1727 SIInstrFlags::P_NORMAL;
1728
1729 static_assert(((~(SIInstrFlags::S_NAN |
1730 SIInstrFlags::Q_NAN |
1731 SIInstrFlags::N_INFINITY |
1732 SIInstrFlags::P_INFINITY)) & 0x3ff) == Mask,
1733 "mask not equal");
1734
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001735 SDLoc DL(N);
1736 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
1737 X, DAG.getConstant(Mask, DL, MVT::i32));
Matt Arsenaultd0101a22015-01-06 23:00:46 +00001738 }
1739 }
1740 }
1741
1742 return SDValue();
1743}
1744
Matt Arsenaultf2290332015-01-06 23:00:39 +00001745SDValue SITargetLowering::performOrCombine(SDNode *N,
1746 DAGCombinerInfo &DCI) const {
1747 SelectionDAG &DAG = DCI.DAG;
1748 SDValue LHS = N->getOperand(0);
1749 SDValue RHS = N->getOperand(1);
1750
1751 // or (fp_class x, c1), (fp_class x, c2) -> fp_class x, (c1 | c2)
1752 if (LHS.getOpcode() == AMDGPUISD::FP_CLASS &&
1753 RHS.getOpcode() == AMDGPUISD::FP_CLASS) {
1754 SDValue Src = LHS.getOperand(0);
1755 if (Src != RHS.getOperand(0))
1756 return SDValue();
1757
1758 const ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(LHS.getOperand(1));
1759 const ConstantSDNode *CRHS = dyn_cast<ConstantSDNode>(RHS.getOperand(1));
1760 if (!CLHS || !CRHS)
1761 return SDValue();
1762
1763 // Only 10 bits are used.
1764 static const uint32_t MaxMask = 0x3ff;
1765
1766 uint32_t NewMask = (CLHS->getZExtValue() | CRHS->getZExtValue()) & MaxMask;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001767 SDLoc DL(N);
1768 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, MVT::i1,
1769 Src, DAG.getConstant(NewMask, DL, MVT::i32));
Matt Arsenaultf2290332015-01-06 23:00:39 +00001770 }
1771
1772 return SDValue();
1773}
1774
1775SDValue SITargetLowering::performClassCombine(SDNode *N,
1776 DAGCombinerInfo &DCI) const {
1777 SelectionDAG &DAG = DCI.DAG;
1778 SDValue Mask = N->getOperand(1);
1779
1780 // fp_class x, 0 -> false
1781 if (const ConstantSDNode *CMask = dyn_cast<ConstantSDNode>(Mask)) {
1782 if (CMask->isNullValue())
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001783 return DAG.getConstant(0, SDLoc(N), MVT::i1);
Matt Arsenaultf2290332015-01-06 23:00:39 +00001784 }
1785
1786 return SDValue();
1787}
1788
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001789static unsigned minMaxOpcToMin3Max3Opc(unsigned Opc) {
1790 switch (Opc) {
1791 case ISD::FMAXNUM:
1792 return AMDGPUISD::FMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00001793 case ISD::SMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001794 return AMDGPUISD::SMAX3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00001795 case ISD::UMAX:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001796 return AMDGPUISD::UMAX3;
1797 case ISD::FMINNUM:
1798 return AMDGPUISD::FMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00001799 case ISD::SMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001800 return AMDGPUISD::SMIN3;
Matt Arsenault5881f4e2015-06-09 00:52:37 +00001801 case ISD::UMIN:
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001802 return AMDGPUISD::UMIN3;
1803 default:
1804 llvm_unreachable("Not a min/max opcode");
1805 }
1806}
1807
1808SDValue SITargetLowering::performMin3Max3Combine(SDNode *N,
1809 DAGCombinerInfo &DCI) const {
1810 SelectionDAG &DAG = DCI.DAG;
1811
1812 unsigned Opc = N->getOpcode();
1813 SDValue Op0 = N->getOperand(0);
1814 SDValue Op1 = N->getOperand(1);
1815
1816 // Only do this if the inner op has one use since this will just increases
1817 // register pressure for no benefit.
1818
1819 // max(max(a, b), c)
1820 if (Op0.getOpcode() == Opc && Op0.hasOneUse()) {
1821 SDLoc DL(N);
1822 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1823 DL,
1824 N->getValueType(0),
1825 Op0.getOperand(0),
1826 Op0.getOperand(1),
1827 Op1);
1828 }
1829
1830 // max(a, max(b, c))
1831 if (Op1.getOpcode() == Opc && Op1.hasOneUse()) {
1832 SDLoc DL(N);
1833 return DAG.getNode(minMaxOpcToMin3Max3Opc(Opc),
1834 DL,
1835 N->getValueType(0),
1836 Op0,
1837 Op1.getOperand(0),
1838 Op1.getOperand(1));
1839 }
1840
1841 return SDValue();
1842}
1843
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001844SDValue SITargetLowering::performSetCCCombine(SDNode *N,
1845 DAGCombinerInfo &DCI) const {
1846 SelectionDAG &DAG = DCI.DAG;
1847 SDLoc SL(N);
1848
1849 SDValue LHS = N->getOperand(0);
1850 SDValue RHS = N->getOperand(1);
1851 EVT VT = LHS.getValueType();
1852
1853 if (VT != MVT::f32 && VT != MVT::f64)
1854 return SDValue();
1855
1856 // Match isinf pattern
1857 // (fcmp oeq (fabs x), inf) -> (fp_class x, (p_infinity | n_infinity))
1858 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
1859 if (CC == ISD::SETOEQ && LHS.getOpcode() == ISD::FABS) {
1860 const ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
1861 if (!CRHS)
1862 return SDValue();
1863
1864 const APFloat &APF = CRHS->getValueAPF();
1865 if (APF.isInfinity() && !APF.isNegative()) {
1866 unsigned Mask = SIInstrFlags::P_INFINITY | SIInstrFlags::N_INFINITY;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001867 return DAG.getNode(AMDGPUISD::FP_CLASS, SL, MVT::i1, LHS.getOperand(0),
1868 DAG.getConstant(Mask, SL, MVT::i32));
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001869 }
1870 }
1871
1872 return SDValue();
1873}
1874
Tom Stellard75aadc22012-12-11 21:25:42 +00001875SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1876 DAGCombinerInfo &DCI) const {
1877 SelectionDAG &DAG = DCI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001878 SDLoc DL(N);
Tom Stellard75aadc22012-12-11 21:25:42 +00001879
1880 switch (N->getOpcode()) {
Matt Arsenault22b4c252014-12-21 16:48:42 +00001881 default:
1882 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Matt Arsenault6f6233d2015-01-06 23:00:41 +00001883 case ISD::SETCC:
1884 return performSetCCCombine(N, DCI);
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001885 case ISD::FMAXNUM: // TODO: What about fmax_legacy?
1886 case ISD::FMINNUM:
Matt Arsenault5881f4e2015-06-09 00:52:37 +00001887 case ISD::SMAX:
1888 case ISD::SMIN:
1889 case ISD::UMAX:
1890 case ISD::UMIN: {
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001891 if (DCI.getDAGCombineLevel() >= AfterLegalizeDAG &&
Tom Stellard7c840bc2015-03-16 15:53:55 +00001892 N->getValueType(0) != MVT::f64 &&
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00001893 getTargetMachine().getOptLevel() > CodeGenOpt::None)
1894 return performMin3Max3Combine(N, DCI);
1895 break;
1896 }
Matt Arsenault364a6742014-06-11 17:50:44 +00001897
1898 case AMDGPUISD::CVT_F32_UBYTE0:
1899 case AMDGPUISD::CVT_F32_UBYTE1:
1900 case AMDGPUISD::CVT_F32_UBYTE2:
1901 case AMDGPUISD::CVT_F32_UBYTE3: {
1902 unsigned Offset = N->getOpcode() - AMDGPUISD::CVT_F32_UBYTE0;
1903
1904 SDValue Src = N->getOperand(0);
1905 APInt Demanded = APInt::getBitsSet(32, 8 * Offset, 8 * Offset + 8);
1906
1907 APInt KnownZero, KnownOne;
1908 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1909 !DCI.isBeforeLegalizeOps());
1910 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1911 if (TLO.ShrinkDemandedConstant(Src, Demanded) ||
1912 TLI.SimplifyDemandedBits(Src, Demanded, KnownZero, KnownOne, TLO)) {
1913 DCI.CommitTargetLoweringOpt(TLO);
1914 }
1915
1916 break;
1917 }
1918
1919 case ISD::UINT_TO_FP: {
1920 return performUCharToFloatCombine(N, DCI);
Matt Arsenault8675db12014-08-29 16:01:14 +00001921
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001922 case ISD::FADD: {
1923 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1924 break;
1925
1926 EVT VT = N->getValueType(0);
1927 if (VT != MVT::f32)
1928 break;
1929
Matt Arsenault8d630032015-02-20 22:10:41 +00001930 // Only do this if we are not trying to support denormals. v_mad_f32 does
1931 // not support denormals ever.
1932 if (Subtarget->hasFP32Denormals())
1933 break;
1934
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001935 SDValue LHS = N->getOperand(0);
1936 SDValue RHS = N->getOperand(1);
1937
1938 // These should really be instruction patterns, but writing patterns with
1939 // source modiifiers is a pain.
1940
1941 // fadd (fadd (a, a), b) -> mad 2.0, a, b
1942 if (LHS.getOpcode() == ISD::FADD) {
1943 SDValue A = LHS.getOperand(0);
1944 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001945 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00001946 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, RHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001947 }
1948 }
1949
1950 // fadd (b, fadd (a, a)) -> mad 2.0, a, b
1951 if (RHS.getOpcode() == ISD::FADD) {
1952 SDValue A = RHS.getOperand(0);
1953 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001954 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00001955 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, LHS);
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001956 }
1957 }
1958
Matt Arsenault8d630032015-02-20 22:10:41 +00001959 return SDValue();
Matt Arsenault02cb0ff2014-09-29 14:59:34 +00001960 }
Matt Arsenault8675db12014-08-29 16:01:14 +00001961 case ISD::FSUB: {
1962 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
1963 break;
1964
1965 EVT VT = N->getValueType(0);
1966
1967 // Try to get the fneg to fold into the source modifier. This undoes generic
1968 // DAG combines and folds them into the mad.
Matt Arsenault8d630032015-02-20 22:10:41 +00001969 //
1970 // Only do this if we are not trying to support denormals. v_mad_f32 does
1971 // not support denormals ever.
1972 if (VT == MVT::f32 &&
1973 !Subtarget->hasFP32Denormals()) {
Matt Arsenault8675db12014-08-29 16:01:14 +00001974 SDValue LHS = N->getOperand(0);
1975 SDValue RHS = N->getOperand(1);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001976 if (LHS.getOpcode() == ISD::FADD) {
1977 // (fsub (fadd a, a), c) -> mad 2.0, a, (fneg c)
1978
1979 SDValue A = LHS.getOperand(0);
1980 if (A == LHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001981 const SDValue Two = DAG.getConstantFP(2.0, DL, MVT::f32);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001982 SDValue NegRHS = DAG.getNode(ISD::FNEG, DL, VT, RHS);
1983
Matt Arsenault8d630032015-02-20 22:10:41 +00001984 return DAG.getNode(ISD::FMAD, DL, VT, Two, A, NegRHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001985 }
1986 }
1987
1988 if (RHS.getOpcode() == ISD::FADD) {
1989 // (fsub c, (fadd a, a)) -> mad -2.0, a, c
1990
1991 SDValue A = RHS.getOperand(0);
1992 if (A == RHS.getOperand(1)) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001993 const SDValue NegTwo = DAG.getConstantFP(-2.0, DL, MVT::f32);
Matt Arsenault8d630032015-02-20 22:10:41 +00001994 return DAG.getNode(ISD::FMAD, DL, VT, NegTwo, A, LHS);
Matt Arsenault3d4233f2014-09-29 14:59:38 +00001995 }
1996 }
Matt Arsenault8d630032015-02-20 22:10:41 +00001997
1998 return SDValue();
Matt Arsenault8675db12014-08-29 16:01:14 +00001999 }
2000
2001 break;
2002 }
Matt Arsenault364a6742014-06-11 17:50:44 +00002003 }
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002004 case ISD::LOAD:
2005 case ISD::STORE:
2006 case ISD::ATOMIC_LOAD:
2007 case ISD::ATOMIC_STORE:
2008 case ISD::ATOMIC_CMP_SWAP:
2009 case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
2010 case ISD::ATOMIC_SWAP:
2011 case ISD::ATOMIC_LOAD_ADD:
2012 case ISD::ATOMIC_LOAD_SUB:
2013 case ISD::ATOMIC_LOAD_AND:
2014 case ISD::ATOMIC_LOAD_OR:
2015 case ISD::ATOMIC_LOAD_XOR:
2016 case ISD::ATOMIC_LOAD_NAND:
2017 case ISD::ATOMIC_LOAD_MIN:
2018 case ISD::ATOMIC_LOAD_MAX:
2019 case ISD::ATOMIC_LOAD_UMIN:
2020 case ISD::ATOMIC_LOAD_UMAX: { // TODO: Target mem intrinsics.
2021 if (DCI.isBeforeLegalize())
2022 break;
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002023
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002024 MemSDNode *MemNode = cast<MemSDNode>(N);
2025 SDValue Ptr = MemNode->getBasePtr();
2026
2027 // TODO: We could also do this for multiplies.
2028 unsigned AS = MemNode->getAddressSpace();
2029 if (Ptr.getOpcode() == ISD::SHL && AS != AMDGPUAS::PRIVATE_ADDRESS) {
2030 SDValue NewPtr = performSHLPtrCombine(Ptr.getNode(), AS, DCI);
2031 if (NewPtr) {
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002032 SmallVector<SDValue, 8> NewOps(MemNode->op_begin(), MemNode->op_end());
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002033
2034 NewOps[N->getOpcode() == ISD::STORE ? 2 : 1] = NewPtr;
2035 return SDValue(DAG.UpdateNodeOperands(MemNode, NewOps), 0);
2036 }
2037 }
2038 break;
2039 }
Matt Arsenaultd0101a22015-01-06 23:00:46 +00002040 case ISD::AND:
2041 return performAndCombine(N, DCI);
Matt Arsenaultf2290332015-01-06 23:00:39 +00002042 case ISD::OR:
2043 return performOrCombine(N, DCI);
2044 case AMDGPUISD::FP_CLASS:
2045 return performClassCombine(N, DCI);
Matt Arsenaultb2baffa2014-08-15 17:49:05 +00002046 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002047 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
Tom Stellard75aadc22012-12-11 21:25:42 +00002048}
Christian Konigd910b7d2013-02-26 17:52:16 +00002049
Christian Konigf82901a2013-02-26 17:52:23 +00002050/// \brief Analyze the possible immediate value Op
2051///
2052/// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
2053/// and the immediate value if it's a literal immediate
2054int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
2055
Eric Christopher7792e322015-01-30 23:24:40 +00002056 const SIInstrInfo *TII =
2057 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konigf82901a2013-02-26 17:52:23 +00002058
Tom Stellardedbf1eb2013-04-05 23:31:20 +00002059 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
Matt Arsenault303011a2014-12-17 21:04:08 +00002060 if (TII->isInlineConstant(Node->getAPIntValue()))
2061 return 0;
Christian Konigf82901a2013-02-26 17:52:23 +00002062
Matt Arsenault11a4d672015-02-13 19:05:03 +00002063 uint64_t Val = Node->getZExtValue();
2064 return isUInt<32>(Val) ? Val : -1;
Matt Arsenault303011a2014-12-17 21:04:08 +00002065 }
2066
2067 if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
2068 if (TII->isInlineConstant(Node->getValueAPF().bitcastToAPInt()))
2069 return 0;
2070
2071 if (Node->getValueType(0) == MVT::f32)
2072 return FloatToBits(Node->getValueAPF().convertToFloat());
2073
2074 return -1;
2075 }
2076
2077 return -1;
Christian Konigf82901a2013-02-26 17:52:23 +00002078}
2079
Christian Konig8e06e2a2013-04-10 08:39:08 +00002080/// \brief Helper function for adjustWritemask
Benjamin Kramer635e3682013-05-23 15:43:05 +00002081static unsigned SubIdx2Lane(unsigned Idx) {
Christian Konig8e06e2a2013-04-10 08:39:08 +00002082 switch (Idx) {
2083 default: return 0;
2084 case AMDGPU::sub0: return 0;
2085 case AMDGPU::sub1: return 1;
2086 case AMDGPU::sub2: return 2;
2087 case AMDGPU::sub3: return 3;
2088 }
2089}
2090
2091/// \brief Adjust the writemask of MIMG instructions
2092void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
2093 SelectionDAG &DAG) const {
2094 SDNode *Users[4] = { };
Tom Stellard54774e52013-10-23 02:53:47 +00002095 unsigned Lane = 0;
2096 unsigned OldDmask = Node->getConstantOperandVal(0);
2097 unsigned NewDmask = 0;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002098
2099 // Try to figure out the used register components
2100 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
2101 I != E; ++I) {
2102
2103 // Abort if we can't understand the usage
2104 if (!I->isMachineOpcode() ||
2105 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
2106 return;
2107
Tom Stellard54774e52013-10-23 02:53:47 +00002108 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
2109 // Note that subregs are packed, i.e. Lane==0 is the first bit set
2110 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
2111 // set, etc.
Christian Konig8b1ed282013-04-10 08:39:16 +00002112 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
Christian Konig8e06e2a2013-04-10 08:39:08 +00002113
Tom Stellard54774e52013-10-23 02:53:47 +00002114 // Set which texture component corresponds to the lane.
2115 unsigned Comp;
2116 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
2117 assert(Dmask);
Tom Stellard03a5c082013-10-23 03:50:25 +00002118 Comp = countTrailingZeros(Dmask);
Tom Stellard54774e52013-10-23 02:53:47 +00002119 Dmask &= ~(1 << Comp);
2120 }
2121
Christian Konig8e06e2a2013-04-10 08:39:08 +00002122 // Abort if we have more than one user per component
2123 if (Users[Lane])
2124 return;
2125
2126 Users[Lane] = *I;
Tom Stellard54774e52013-10-23 02:53:47 +00002127 NewDmask |= 1 << Comp;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002128 }
2129
Tom Stellard54774e52013-10-23 02:53:47 +00002130 // Abort if there's no change
2131 if (NewDmask == OldDmask)
Christian Konig8e06e2a2013-04-10 08:39:08 +00002132 return;
2133
2134 // Adjust the writemask in the node
2135 std::vector<SDValue> Ops;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002136 Ops.push_back(DAG.getTargetConstant(NewDmask, SDLoc(Node), MVT::i32));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +00002137 Ops.insert(Ops.end(), Node->op_begin() + 1, Node->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +00002138 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002139
Christian Konig8b1ed282013-04-10 08:39:16 +00002140 // If we only got one lane, replace it with a copy
Tom Stellard54774e52013-10-23 02:53:47 +00002141 // (if NewDmask has only one bit set...)
2142 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002143 SDValue RC = DAG.getTargetConstant(AMDGPU::VGPR_32RegClassID, SDLoc(),
2144 MVT::i32);
Christian Konig8b1ed282013-04-10 08:39:16 +00002145 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002146 SDLoc(), Users[Lane]->getValueType(0),
Christian Konig8b1ed282013-04-10 08:39:16 +00002147 SDValue(Node, 0), RC);
2148 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
2149 return;
2150 }
2151
Christian Konig8e06e2a2013-04-10 08:39:08 +00002152 // Update the users of the node with the new indices
2153 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
2154
2155 SDNode *User = Users[i];
2156 if (!User)
2157 continue;
2158
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002159 SDValue Op = DAG.getTargetConstant(Idx, SDLoc(User), MVT::i32);
Christian Konig8e06e2a2013-04-10 08:39:08 +00002160 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
2161
2162 switch (Idx) {
2163 default: break;
2164 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
2165 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
2166 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
2167 }
2168 }
2169}
2170
Tom Stellardc98ee202015-07-16 19:40:07 +00002171static bool isFrameIndexOp(SDValue Op) {
2172 if (Op.getOpcode() == ISD::AssertZext)
2173 Op = Op.getOperand(0);
2174
2175 return isa<FrameIndexSDNode>(Op);
2176}
2177
Tom Stellard3457a842014-10-09 19:06:00 +00002178/// \brief Legalize target independent instructions (e.g. INSERT_SUBREG)
2179/// with frame index operands.
2180/// LLVM assumes that inputs are to these instructions are registers.
2181void SITargetLowering::legalizeTargetIndependentNode(SDNode *Node,
2182 SelectionDAG &DAG) const {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002183
2184 SmallVector<SDValue, 8> Ops;
Tom Stellard3457a842014-10-09 19:06:00 +00002185 for (unsigned i = 0; i < Node->getNumOperands(); ++i) {
Tom Stellardc98ee202015-07-16 19:40:07 +00002186 if (!isFrameIndexOp(Node->getOperand(i))) {
Tom Stellard3457a842014-10-09 19:06:00 +00002187 Ops.push_back(Node->getOperand(i));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002188 continue;
2189 }
2190
Tom Stellard3457a842014-10-09 19:06:00 +00002191 SDLoc DL(Node);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002192 Ops.push_back(SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL,
Tom Stellard3457a842014-10-09 19:06:00 +00002193 Node->getOperand(i).getValueType(),
2194 Node->getOperand(i)), 0));
Tom Stellard8dd392e2014-10-09 18:09:15 +00002195 }
2196
Tom Stellard3457a842014-10-09 19:06:00 +00002197 DAG.UpdateNodeOperands(Node, Ops);
Tom Stellard8dd392e2014-10-09 18:09:15 +00002198}
2199
Matt Arsenault08d84942014-06-03 23:06:13 +00002200/// \brief Fold the instructions after selecting them.
Christian Konig8e06e2a2013-04-10 08:39:08 +00002201SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
2202 SelectionDAG &DAG) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002203 const SIInstrInfo *TII =
2204 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Christian Konig8e06e2a2013-04-10 08:39:08 +00002205
Tom Stellard16a9a202013-08-14 23:24:17 +00002206 if (TII->isMIMG(Node->getMachineOpcode()))
Christian Konig8e06e2a2013-04-10 08:39:08 +00002207 adjustWritemask(Node, DAG);
2208
Matt Arsenault7d858d82014-11-02 23:46:54 +00002209 if (Node->getMachineOpcode() == AMDGPU::INSERT_SUBREG ||
2210 Node->getMachineOpcode() == AMDGPU::REG_SEQUENCE) {
Tom Stellard8dd392e2014-10-09 18:09:15 +00002211 legalizeTargetIndependentNode(Node, DAG);
2212 return Node;
2213 }
Tom Stellard654d6692015-01-08 15:08:17 +00002214 return Node;
Christian Konig8e06e2a2013-04-10 08:39:08 +00002215}
Christian Konig8b1ed282013-04-10 08:39:16 +00002216
2217/// \brief Assign the register class depending on the number of
2218/// bits set in the writemask
2219void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
2220 SDNode *Node) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002221 const SIInstrInfo *TII =
2222 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002223
Tom Stellarda99ada52014-11-21 22:31:44 +00002224 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
Matt Arsenault6005fcb2015-10-21 21:51:02 +00002225
2226 if (TII->isVOP3(MI->getOpcode())) {
2227 // Make sure constant bus requirements are respected.
2228 TII->legalizeOperandsVOP3(MRI, MI);
2229 return;
2230 }
Matt Arsenaultcb0ac3d2014-09-26 17:54:59 +00002231
Matt Arsenault3add6432015-10-20 04:35:43 +00002232 if (TII->isMIMG(*MI)) {
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002233 unsigned VReg = MI->getOperand(0).getReg();
2234 unsigned Writemask = MI->getOperand(1).getImm();
2235 unsigned BitsSet = 0;
2236 for (unsigned i = 0; i < 4; ++i)
2237 BitsSet += Writemask & (1 << i) ? 1 : 0;
2238
2239 const TargetRegisterClass *RC;
2240 switch (BitsSet) {
2241 default: return;
Tom Stellard45c0b3a2015-01-07 20:59:25 +00002242 case 1: RC = &AMDGPU::VGPR_32RegClass; break;
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002243 case 2: RC = &AMDGPU::VReg_64RegClass; break;
2244 case 3: RC = &AMDGPU::VReg_96RegClass; break;
2245 }
2246
2247 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
2248 MI->setDesc(TII->get(NewOpcode));
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002249 MRI.setRegClass(VReg, RC);
Christian Konig8b1ed282013-04-10 08:39:16 +00002250 return;
Christian Konig8b1ed282013-04-10 08:39:16 +00002251 }
2252
Matt Arsenault7ac9c4a2014-09-08 15:07:31 +00002253 // Replace unused atomics with the no return version.
2254 int NoRetAtomicOp = AMDGPU::getAtomicNoRetOp(MI->getOpcode());
2255 if (NoRetAtomicOp != -1) {
2256 if (!Node->hasAnyUseOfValue(0)) {
2257 MI->setDesc(TII->get(NoRetAtomicOp));
2258 MI->RemoveOperand(0);
2259 }
2260
2261 return;
2262 }
Christian Konig8b1ed282013-04-10 08:39:16 +00002263}
Tom Stellard0518ff82013-06-03 17:39:58 +00002264
Matt Arsenault485defe2014-11-05 19:01:17 +00002265static SDValue buildSMovImm32(SelectionDAG &DAG, SDLoc DL, uint64_t Val) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002266 SDValue K = DAG.getTargetConstant(Val, DL, MVT::i32);
Matt Arsenault485defe2014-11-05 19:01:17 +00002267 return SDValue(DAG.getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, K), 0);
2268}
2269
2270MachineSDNode *SITargetLowering::wrapAddr64Rsrc(SelectionDAG &DAG,
2271 SDLoc DL,
2272 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002273 const SIInstrInfo *TII =
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002274 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenault485defe2014-11-05 19:01:17 +00002275
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002276 // Build the half of the subregister with the constants before building the
2277 // full 128-bit register. If we are building multiple resource descriptors,
2278 // this will allow CSEing of the 2-component register.
2279 const SDValue Ops0[] = {
2280 DAG.getTargetConstant(AMDGPU::SGPR_64RegClassID, DL, MVT::i32),
2281 buildSMovImm32(DAG, DL, 0),
2282 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
2283 buildSMovImm32(DAG, DL, TII->getDefaultRsrcDataFormat() >> 32),
2284 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32)
2285 };
Matt Arsenault485defe2014-11-05 19:01:17 +00002286
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002287 SDValue SubRegHi = SDValue(DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL,
2288 MVT::v2i32, Ops0), 0);
Matt Arsenault485defe2014-11-05 19:01:17 +00002289
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002290 // Combine the constants and the pointer.
2291 const SDValue Ops1[] = {
2292 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
2293 Ptr,
2294 DAG.getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32),
2295 SubRegHi,
2296 DAG.getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32)
2297 };
Matt Arsenault485defe2014-11-05 19:01:17 +00002298
Matt Arsenault2d6fdb82015-09-25 17:08:42 +00002299 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops1);
Matt Arsenault485defe2014-11-05 19:01:17 +00002300}
2301
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002302/// \brief Return a resource descriptor with the 'Add TID' bit enabled
Benjamin Kramerdf005cb2015-08-08 18:27:36 +00002303/// The TID (Thread ID) is multiplied by the stride value (bits [61:48]
2304/// of the resource descriptor) to create an offset, which is added to
2305/// the resource pointer.
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002306MachineSDNode *SITargetLowering::buildRSRC(SelectionDAG &DAG,
2307 SDLoc DL,
2308 SDValue Ptr,
2309 uint32_t RsrcDword1,
2310 uint64_t RsrcDword2And3) const {
2311 SDValue PtrLo = DAG.getTargetExtractSubreg(AMDGPU::sub0, DL, MVT::i32, Ptr);
2312 SDValue PtrHi = DAG.getTargetExtractSubreg(AMDGPU::sub1, DL, MVT::i32, Ptr);
2313 if (RsrcDword1) {
2314 PtrHi = SDValue(DAG.getMachineNode(AMDGPU::S_OR_B32, DL, MVT::i32, PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002315 DAG.getConstant(RsrcDword1, DL, MVT::i32)),
2316 0);
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002317 }
2318
2319 SDValue DataLo = buildSMovImm32(DAG, DL,
2320 RsrcDword2And3 & UINT64_C(0xFFFFFFFF));
2321 SDValue DataHi = buildSMovImm32(DAG, DL, RsrcDword2And3 >> 32);
2322
2323 const SDValue Ops[] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002324 DAG.getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002325 PtrLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002326 DAG.getTargetConstant(AMDGPU::sub0, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002327 PtrHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002328 DAG.getTargetConstant(AMDGPU::sub1, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002329 DataLo,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002330 DAG.getTargetConstant(AMDGPU::sub2, DL, MVT::i32),
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002331 DataHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002332 DAG.getTargetConstant(AMDGPU::sub3, DL, MVT::i32)
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002333 };
2334
2335 return DAG.getMachineNode(AMDGPU::REG_SEQUENCE, DL, MVT::v4i32, Ops);
2336}
2337
2338MachineSDNode *SITargetLowering::buildScratchRSRC(SelectionDAG &DAG,
2339 SDLoc DL,
2340 SDValue Ptr) const {
Eric Christopher7792e322015-01-30 23:24:40 +00002341 const SIInstrInfo *TII =
2342 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo());
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002343
Marek Olsakd1a69a22015-09-29 23:37:32 +00002344 return buildRSRC(DAG, DL, Ptr, 0, TII->getScratchRsrcWords23());
Matt Arsenaultf3cd4512014-11-05 19:01:19 +00002345}
2346
Tom Stellard94593ee2013-06-03 17:40:18 +00002347SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2348 const TargetRegisterClass *RC,
2349 unsigned Reg, EVT VT) const {
2350 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
2351
2352 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
2353 cast<RegisterSDNode>(VReg)->getReg(), VT);
2354}
Tom Stellardd7e6f132015-04-08 01:09:26 +00002355
2356//===----------------------------------------------------------------------===//
2357// SI Inline Assembly Support
2358//===----------------------------------------------------------------------===//
2359
2360std::pair<unsigned, const TargetRegisterClass *>
2361SITargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
Benjamin Kramer9bfb6272015-07-05 19:29:18 +00002362 StringRef Constraint,
Tom Stellardd7e6f132015-04-08 01:09:26 +00002363 MVT VT) const {
2364 if (Constraint == "r") {
2365 switch(VT.SimpleTy) {
2366 default: llvm_unreachable("Unhandled type for 'r' inline asm constraint");
2367 case MVT::i64:
2368 return std::make_pair(0U, &AMDGPU::SGPR_64RegClass);
2369 case MVT::i32:
2370 return std::make_pair(0U, &AMDGPU::SGPR_32RegClass);
2371 }
2372 }
2373
2374 if (Constraint.size() > 1) {
2375 const TargetRegisterClass *RC = nullptr;
2376 if (Constraint[1] == 'v') {
2377 RC = &AMDGPU::VGPR_32RegClass;
2378 } else if (Constraint[1] == 's') {
2379 RC = &AMDGPU::SGPR_32RegClass;
2380 }
2381
2382 if (RC) {
Matt Arsenault0b554ed2015-06-23 02:05:55 +00002383 uint32_t Idx;
2384 bool Failed = Constraint.substr(2).getAsInteger(10, Idx);
2385 if (!Failed && Idx < RC->getNumRegs())
Tom Stellardd7e6f132015-04-08 01:09:26 +00002386 return std::make_pair(RC->getRegister(Idx), RC);
2387 }
2388 }
2389 return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
2390}