blob: 3350654daade02cfbe60dab624fb49ef7ca44094 [file] [log] [blame]
Tom Stellard75aadc22012-12-11 21:25:42 +00001//===-- AMDGPUISelLowering.cpp - AMDGPU Common DAG lowering functions -----===//
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 This is the parent TargetLowering class for hardware code gen
12/// targets.
13//
14//===----------------------------------------------------------------------===//
15
16#include "AMDGPUISelLowering.h"
Tom Stellarded882c22013-06-03 17:40:11 +000017#include "AMDGPU.h"
Tom Stellardca166212017-01-30 21:56:46 +000018#include "AMDGPUCallLowering.h"
Tom Stellard81d871d2013-11-13 23:36:50 +000019#include "AMDGPUFrameLowering.h"
Matt Arsenaultc791f392014-06-23 18:00:31 +000020#include "AMDGPUIntrinsicInfo.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000021#include "AMDGPURegisterInfo.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000022#include "AMDGPUSubtarget.h"
Tom Stellardacfeebf2013-07-23 01:48:05 +000023#include "R600MachineFunctionInfo.h"
Tom Stellarded882c22013-06-03 17:40:11 +000024#include "SIMachineFunctionInfo.h"
Christian Konig2c8f6d52013-03-07 09:03:52 +000025#include "llvm/CodeGen/CallingConvLower.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000026#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineRegisterInfo.h"
28#include "llvm/CodeGen/SelectionDAG.h"
29#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Tom Stellardc026e8b2013-06-28 15:47:08 +000030#include "llvm/IR/DataLayout.h"
Oliver Stannard7e7d9832016-02-02 13:52:43 +000031#include "llvm/IR/DiagnosticInfo.h"
Matt Arsenault6e3a4512016-01-18 22:01:13 +000032#include "SIInstrInfo.h"
Tom Stellard75aadc22012-12-11 21:25:42 +000033using namespace llvm;
Matt Arsenault16353872014-04-22 16:42:00 +000034
Matt Arsenaulte935f052016-06-18 05:15:53 +000035static bool allocateKernArg(unsigned ValNo, MVT ValVT, MVT LocVT,
36 CCValAssign::LocInfo LocInfo,
37 ISD::ArgFlagsTy ArgFlags, CCState &State) {
38 MachineFunction &MF = State.getMachineFunction();
39 AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>();
Tom Stellardaf775432013-10-23 00:44:32 +000040
Tom Stellardbbeb45a2016-09-16 21:53:00 +000041 uint64_t Offset = MFI->allocateKernArg(LocVT.getStoreSize(),
Matt Arsenaulte935f052016-06-18 05:15:53 +000042 ArgFlags.getOrigAlign());
43 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT, Offset, LocVT, LocInfo));
Tom Stellardaf775432013-10-23 00:44:32 +000044 return true;
45}
Tom Stellard75aadc22012-12-11 21:25:42 +000046
Christian Konig2c8f6d52013-03-07 09:03:52 +000047#include "AMDGPUGenCallingConv.inc"
48
Matt Arsenaultc9df7942014-06-11 03:29:54 +000049// Find a larger type to do a load / store of a vector with.
50EVT AMDGPUTargetLowering::getEquivalentMemType(LLVMContext &Ctx, EVT VT) {
51 unsigned StoreSize = VT.getStoreSizeInBits();
52 if (StoreSize <= 32)
53 return EVT::getIntegerVT(Ctx, StoreSize);
54
55 assert(StoreSize % 32 == 0 && "Store size not a multiple of 32");
56 return EVT::getVectorVT(Ctx, MVT::i32, StoreSize / 32);
57}
58
Matt Arsenault43e92fe2016-06-24 06:30:11 +000059AMDGPUTargetLowering::AMDGPUTargetLowering(const TargetMachine &TM,
Eric Christopher7792e322015-01-30 23:24:40 +000060 const AMDGPUSubtarget &STI)
61 : TargetLowering(TM), Subtarget(&STI) {
Tom Stellard75aadc22012-12-11 21:25:42 +000062 // Lower floating point store/load to integer store/load to reduce the number
63 // of patterns in tablegen.
Tom Stellard75aadc22012-12-11 21:25:42 +000064 setOperationAction(ISD::LOAD, MVT::f32, Promote);
65 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
66
Tom Stellardadf732c2013-07-18 21:43:48 +000067 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
68 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
69
Tom Stellard75aadc22012-12-11 21:25:42 +000070 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
71 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
72
Tom Stellardaf775432013-10-23 00:44:32 +000073 setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
74 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
75
76 setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
77 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
78
Matt Arsenault71e66762016-05-21 02:27:49 +000079 setOperationAction(ISD::LOAD, MVT::i64, Promote);
80 AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
81
82 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
83 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
84
Tom Stellard7512c082013-07-12 18:14:56 +000085 setOperationAction(ISD::LOAD, MVT::f64, Promote);
Matt Arsenault71e66762016-05-21 02:27:49 +000086 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::v2i32);
Tom Stellard7512c082013-07-12 18:14:56 +000087
Matt Arsenaulte8a076a2014-05-08 18:01:56 +000088 setOperationAction(ISD::LOAD, MVT::v2f64, Promote);
Matt Arsenault71e66762016-05-21 02:27:49 +000089 AddPromotedToType(ISD::LOAD, MVT::v2f64, MVT::v4i32);
Tom Stellard0344cdf2013-08-01 15:23:42 +000090
Matt Arsenaultbd223422015-01-14 01:35:17 +000091 // There are no 64-bit extloads. These should be done as a 32-bit extload and
92 // an extension to 64-bit.
93 for (MVT VT : MVT::integer_valuetypes()) {
94 setLoadExtAction(ISD::EXTLOAD, MVT::i64, VT, Expand);
95 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, VT, Expand);
96 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, VT, Expand);
97 }
98
Matt Arsenault71e66762016-05-21 02:27:49 +000099 for (MVT VT : MVT::integer_valuetypes()) {
100 if (VT == MVT::i64)
101 continue;
102
103 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
104 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
105 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
106 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
107
108 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
109 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
110 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
111 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
112
113 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
114 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
115 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
116 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
117 }
118
Ahmed Bougacha2b6917b2015-01-08 00:51:32 +0000119 for (MVT VT : MVT::integer_vector_valuetypes()) {
120 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i8, Expand);
121 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i8, Expand);
122 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i8, Expand);
123 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i8, Expand);
124 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i8, Expand);
125 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i8, Expand);
126 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i16, Expand);
127 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i16, Expand);
128 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i16, Expand);
129 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i16, Expand);
130 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i16, Expand);
131 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i16, Expand);
132 }
Tom Stellardb03edec2013-08-16 01:12:16 +0000133
Matt Arsenault71e66762016-05-21 02:27:49 +0000134 setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
135 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, MVT::v2f16, Expand);
136 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, MVT::v4f16, Expand);
137 setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, MVT::v8f16, Expand);
138
139 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
140 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
141 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Expand);
142 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f32, Expand);
143
144 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
145 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
146 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f16, Expand);
147 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f16, Expand);
148
149 setOperationAction(ISD::STORE, MVT::f32, Promote);
150 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
151
152 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
153 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
154
155 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
156 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
157
158 setOperationAction(ISD::STORE, MVT::v8f32, Promote);
159 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
160
161 setOperationAction(ISD::STORE, MVT::v16f32, Promote);
162 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
163
164 setOperationAction(ISD::STORE, MVT::i64, Promote);
165 AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
166
167 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
168 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
169
170 setOperationAction(ISD::STORE, MVT::f64, Promote);
171 AddPromotedToType(ISD::STORE, MVT::f64, MVT::v2i32);
172
173 setOperationAction(ISD::STORE, MVT::v2f64, Promote);
174 AddPromotedToType(ISD::STORE, MVT::v2f64, MVT::v4i32);
175
Matt Arsenault71e66762016-05-21 02:27:49 +0000176 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
177 setTruncStoreAction(MVT::i64, MVT::i8, Expand);
178 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
179 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
180
181 setTruncStoreAction(MVT::v2i64, MVT::v2i1, Expand);
182 setTruncStoreAction(MVT::v2i64, MVT::v2i8, Expand);
183 setTruncStoreAction(MVT::v2i64, MVT::v2i16, Expand);
184 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
185
186 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
187 setTruncStoreAction(MVT::v2f32, MVT::v2f16, Expand);
188 setTruncStoreAction(MVT::v4f32, MVT::v4f16, Expand);
189 setTruncStoreAction(MVT::v8f32, MVT::v8f16, Expand);
190
191 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
192 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
193
194 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
195 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
196
197 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Expand);
198 setTruncStoreAction(MVT::v4f64, MVT::v4f16, Expand);
199
200 setTruncStoreAction(MVT::v8f64, MVT::v8f32, Expand);
201 setTruncStoreAction(MVT::v8f64, MVT::v8f16, Expand);
202
203
204 setOperationAction(ISD::Constant, MVT::i32, Legal);
205 setOperationAction(ISD::Constant, MVT::i64, Legal);
206 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
207 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
208
209 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
210 setOperationAction(ISD::BRIND, MVT::Other, Expand);
211
212 // This is totally unsupported, just custom lower to produce an error.
213 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
214
215 // We need to custom lower some of the intrinsics
216 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
217 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
218
219 // Library functions. These default to Expand, but we have instructions
220 // for them.
221 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
222 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
223 setOperationAction(ISD::FPOW, MVT::f32, Legal);
224 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
225 setOperationAction(ISD::FABS, MVT::f32, Legal);
226 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
227 setOperationAction(ISD::FRINT, MVT::f32, Legal);
228 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
229 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
230 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
231
232 setOperationAction(ISD::FROUND, MVT::f32, Custom);
233 setOperationAction(ISD::FROUND, MVT::f64, Custom);
234
235 setOperationAction(ISD::FNEARBYINT, MVT::f32, Custom);
236 setOperationAction(ISD::FNEARBYINT, MVT::f64, Custom);
237
238 setOperationAction(ISD::FREM, MVT::f32, Custom);
239 setOperationAction(ISD::FREM, MVT::f64, Custom);
240
241 // v_mad_f32 does not support denormals according to some sources.
242 if (!Subtarget->hasFP32Denormals())
243 setOperationAction(ISD::FMAD, MVT::f32, Legal);
244
245 // Expand to fneg + fadd.
246 setOperationAction(ISD::FSUB, MVT::f64, Expand);
247
248 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
249 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
250 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
251 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
252 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
253 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
254 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f32, Custom);
255 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
256 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f32, Custom);
257 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i32, Custom);
Tom Stellardaeb45642014-02-04 17:18:43 +0000258
Matt Arsenaulte8208ec2014-06-18 17:05:26 +0000259 if (Subtarget->getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
Matt Arsenault46010932014-06-18 17:05:30 +0000260 setOperationAction(ISD::FCEIL, MVT::f64, Custom);
261 setOperationAction(ISD::FTRUNC, MVT::f64, Custom);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +0000262 setOperationAction(ISD::FRINT, MVT::f64, Custom);
Matt Arsenault46010932014-06-18 17:05:30 +0000263 setOperationAction(ISD::FFLOOR, MVT::f64, Custom);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +0000264 }
265
Matt Arsenault6e439652014-06-10 19:00:20 +0000266 if (!Subtarget->hasBFI()) {
267 // fcopysign can be done in a single instruction with BFI.
268 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
269 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
270 }
271
Tim Northoverf861de32014-07-18 08:43:24 +0000272 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
Tom Stellard94c21bc2016-11-01 16:31:48 +0000273 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Custom);
Tim Northoverf861de32014-07-18 08:43:24 +0000274
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000275 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
276 for (MVT VT : ScalarIntVTs) {
Matt Arsenault71e66762016-05-21 02:27:49 +0000277 // These should use [SU]DIVREM, so set them to expand
Jan Vesely4a33bc62014-08-12 17:31:17 +0000278 setOperationAction(ISD::SDIV, VT, Expand);
Matt Arsenault71e66762016-05-21 02:27:49 +0000279 setOperationAction(ISD::UDIV, VT, Expand);
280 setOperationAction(ISD::SREM, VT, Expand);
281 setOperationAction(ISD::UREM, VT, Expand);
Matt Arsenault717c1d02014-06-15 21:08:58 +0000282
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000283 // GPU does not have divrem function for signed or unsigned.
Jan Vesely109efdf2014-06-22 21:43:00 +0000284 setOperationAction(ISD::SDIVREM, VT, Custom);
Matt Arsenault717c1d02014-06-15 21:08:58 +0000285 setOperationAction(ISD::UDIVREM, VT, Custom);
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000286
287 // GPU does not have [S|U]MUL_LOHI functions as a single instruction.
288 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
289 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
290
291 setOperationAction(ISD::BSWAP, VT, Expand);
292 setOperationAction(ISD::CTTZ, VT, Expand);
293 setOperationAction(ISD::CTLZ, VT, Expand);
294 }
295
Matt Arsenault60425062014-06-10 19:18:28 +0000296 if (!Subtarget->hasBCNT(32))
297 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
298
299 if (!Subtarget->hasBCNT(64))
300 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
301
Matt Arsenault717c1d02014-06-15 21:08:58 +0000302 // The hardware supports 32-bit ROTR, but not ROTL.
303 setOperationAction(ISD::ROTL, MVT::i32, Expand);
304 setOperationAction(ISD::ROTL, MVT::i64, Expand);
305 setOperationAction(ISD::ROTR, MVT::i64, Expand);
306
307 setOperationAction(ISD::MUL, MVT::i64, Expand);
308 setOperationAction(ISD::MULHU, MVT::i64, Expand);
309 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Matt Arsenault717c1d02014-06-15 21:08:58 +0000310 setOperationAction(ISD::UDIV, MVT::i32, Expand);
311 setOperationAction(ISD::UREM, MVT::i32, Expand);
312 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +0000313 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Matt Arsenaultc9961752014-10-03 23:54:56 +0000314 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
315 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
Matt Arsenault717c1d02014-06-15 21:08:58 +0000316 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
Matt Arsenaultb5b51102014-06-10 19:18:21 +0000317
Matt Arsenault5881f4e2015-06-09 00:52:37 +0000318 setOperationAction(ISD::SMIN, MVT::i32, Legal);
319 setOperationAction(ISD::UMIN, MVT::i32, Legal);
320 setOperationAction(ISD::SMAX, MVT::i32, Legal);
321 setOperationAction(ISD::UMAX, MVT::i32, Legal);
322
Matt Arsenaultde5fbe92016-01-11 17:02:00 +0000323 if (Subtarget->hasFFBH())
324 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom);
Jan Vesely6ddb8dd2014-07-15 15:51:09 +0000325
Craig Topper33772c52016-04-28 03:34:31 +0000326 if (Subtarget->hasFFBL())
327 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Legal);
Matt Arsenault5ca3c722016-01-11 16:37:46 +0000328
Matt Arsenaultf058d672016-01-11 16:50:29 +0000329 setOperationAction(ISD::CTLZ, MVT::i64, Custom);
330 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
331
Matt Arsenault59b8b772016-03-01 04:58:17 +0000332 // We only really have 32-bit BFE instructions (and 16-bit on VI).
333 //
334 // On SI+ there are 64-bit BFEs, but they are scalar only and there isn't any
335 // effort to match them now. We want this to be false for i64 cases when the
336 // extraction isn't restricted to the upper or lower half. Ideally we would
337 // have some pass reduce 64-bit extracts to 32-bit if possible. Extracts that
338 // span the midpoint are probably relatively rare, so don't worry about them
339 // for now.
340 if (Subtarget->hasBFE())
341 setHasExtractBitsInsn(true);
342
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000343 static const MVT::SimpleValueType VectorIntTypes[] = {
Tom Stellardf6d80232013-08-21 22:14:17 +0000344 MVT::v2i32, MVT::v4i32
Aaron Watry0a794a462013-06-25 13:55:57 +0000345 };
Aaron Watry0a794a462013-06-25 13:55:57 +0000346
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000347 for (MVT VT : VectorIntTypes) {
Matt Arsenaultb5b51102014-06-10 19:18:21 +0000348 // Expand the following operations for the current type by default.
Aaron Watry0a794a462013-06-25 13:55:57 +0000349 setOperationAction(ISD::ADD, VT, Expand);
350 setOperationAction(ISD::AND, VT, Expand);
Tom Stellardaa313d02013-07-30 14:31:03 +0000351 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
352 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000353 setOperationAction(ISD::MUL, VT, Expand);
Valery Pykhtin8a89d362016-11-01 10:26:48 +0000354 setOperationAction(ISD::MULHU, VT, Expand);
355 setOperationAction(ISD::MULHS, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000356 setOperationAction(ISD::OR, VT, Expand);
357 setOperationAction(ISD::SHL, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000358 setOperationAction(ISD::SRA, VT, Expand);
Matt Arsenault825fb0b2014-06-13 04:00:30 +0000359 setOperationAction(ISD::SRL, VT, Expand);
360 setOperationAction(ISD::ROTL, VT, Expand);
361 setOperationAction(ISD::ROTR, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000362 setOperationAction(ISD::SUB, VT, Expand);
Matt Arsenault825fb0b2014-06-13 04:00:30 +0000363 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
Tom Stellardaa313d02013-07-30 14:31:03 +0000364 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
Jan Vesely109efdf2014-06-22 21:43:00 +0000365 setOperationAction(ISD::SDIV, VT, Expand);
Matt Arsenault717c1d02014-06-15 21:08:58 +0000366 setOperationAction(ISD::UDIV, VT, Expand);
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000367 setOperationAction(ISD::SREM, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000368 setOperationAction(ISD::UREM, VT, Expand);
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000369 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
370 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
Jan Vesely109efdf2014-06-22 21:43:00 +0000371 setOperationAction(ISD::SDIVREM, VT, Custom);
Artyom Skrobov63471332015-10-15 09:18:47 +0000372 setOperationAction(ISD::UDIVREM, VT, Expand);
Matt Arsenaultc4d3d3a2014-06-23 18:00:49 +0000373 setOperationAction(ISD::ADDC, VT, Expand);
374 setOperationAction(ISD::SUBC, VT, Expand);
375 setOperationAction(ISD::ADDE, VT, Expand);
376 setOperationAction(ISD::SUBE, VT, Expand);
Matt Arsenault9fe669c2014-03-06 17:34:03 +0000377 setOperationAction(ISD::SELECT, VT, Expand);
Tom Stellard67ae4762013-07-18 21:43:35 +0000378 setOperationAction(ISD::VSELECT, VT, Expand);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000379 setOperationAction(ISD::SELECT_CC, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000380 setOperationAction(ISD::XOR, VT, Expand);
Matt Arsenault13ccc8f2014-06-09 16:20:25 +0000381 setOperationAction(ISD::BSWAP, VT, Expand);
Matt Arsenaultb5b51102014-06-10 19:18:21 +0000382 setOperationAction(ISD::CTPOP, VT, Expand);
383 setOperationAction(ISD::CTTZ, VT, Expand);
384 setOperationAction(ISD::CTLZ, VT, Expand);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000385 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
Aaron Watry0a794a462013-06-25 13:55:57 +0000386 }
Tom Stellarda92ff872013-08-16 23:51:24 +0000387
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000388 static const MVT::SimpleValueType FloatVectorTypes[] = {
Tom Stellardf6d80232013-08-21 22:14:17 +0000389 MVT::v2f32, MVT::v4f32
Tom Stellarda92ff872013-08-16 23:51:24 +0000390 };
Tom Stellarda92ff872013-08-16 23:51:24 +0000391
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000392 for (MVT VT : FloatVectorTypes) {
Tom Stellard175e7a82013-11-27 21:23:39 +0000393 setOperationAction(ISD::FABS, VT, Expand);
Matt Arsenault7c936902014-10-21 23:01:01 +0000394 setOperationAction(ISD::FMINNUM, VT, Expand);
395 setOperationAction(ISD::FMAXNUM, VT, Expand);
Tom Stellarda92ff872013-08-16 23:51:24 +0000396 setOperationAction(ISD::FADD, VT, Expand);
Jan Vesely85f0dbc2014-06-18 17:57:29 +0000397 setOperationAction(ISD::FCEIL, VT, Expand);
Tom Stellard3dbf1f82014-05-02 15:41:47 +0000398 setOperationAction(ISD::FCOS, VT, Expand);
Tom Stellarda92ff872013-08-16 23:51:24 +0000399 setOperationAction(ISD::FDIV, VT, Expand);
Tom Stellard5222a882014-06-20 17:06:05 +0000400 setOperationAction(ISD::FEXP2, VT, Expand);
Tom Stellarda79e9f02014-06-20 17:06:07 +0000401 setOperationAction(ISD::FLOG2, VT, Expand);
Matt Arsenault16e31332014-09-10 21:44:27 +0000402 setOperationAction(ISD::FREM, VT, Expand);
Tom Stellardbfebd1f2014-02-04 17:18:37 +0000403 setOperationAction(ISD::FPOW, VT, Expand);
Tom Stellardad3aff22013-08-16 23:51:29 +0000404 setOperationAction(ISD::FFLOOR, VT, Expand);
Tom Stellardeddfa692013-12-20 05:11:55 +0000405 setOperationAction(ISD::FTRUNC, VT, Expand);
Tom Stellarda92ff872013-08-16 23:51:24 +0000406 setOperationAction(ISD::FMUL, VT, Expand);
Matt Arsenaultc6f8fdb2014-06-26 01:28:05 +0000407 setOperationAction(ISD::FMA, VT, Expand);
Tom Stellardb249b752013-08-16 23:51:33 +0000408 setOperationAction(ISD::FRINT, VT, Expand);
Matt Arsenault692bd5e2014-06-18 22:03:45 +0000409 setOperationAction(ISD::FNEARBYINT, VT, Expand);
Tom Stellarde118b8b2013-10-29 16:37:20 +0000410 setOperationAction(ISD::FSQRT, VT, Expand);
Tom Stellard3dbf1f82014-05-02 15:41:47 +0000411 setOperationAction(ISD::FSIN, VT, Expand);
Tom Stellarda92ff872013-08-16 23:51:24 +0000412 setOperationAction(ISD::FSUB, VT, Expand);
Matt Arsenault616a8e42014-06-01 07:38:21 +0000413 setOperationAction(ISD::FNEG, VT, Expand);
Matt Arsenault616a8e42014-06-01 07:38:21 +0000414 setOperationAction(ISD::VSELECT, VT, Expand);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000415 setOperationAction(ISD::SELECT_CC, VT, Expand);
Matt Arsenault6e439652014-06-10 19:00:20 +0000416 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
Matt Arsenaulte54e1c32014-06-23 18:00:44 +0000417 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
Tom Stellarda92ff872013-08-16 23:51:24 +0000418 }
Matt Arsenaultfae02982014-03-17 18:58:11 +0000419
Matt Arsenault1cc49912016-05-25 17:34:58 +0000420 // This causes using an unrolled select operation rather than expansion with
421 // bit operations. This is in general better, but the alternative using BFI
422 // instructions may be better if the select sources are SGPRs.
423 setOperationAction(ISD::SELECT, MVT::v2f32, Promote);
424 AddPromotedToType(ISD::SELECT, MVT::v2f32, MVT::v2i32);
425
426 setOperationAction(ISD::SELECT, MVT::v4f32, Promote);
427 AddPromotedToType(ISD::SELECT, MVT::v4f32, MVT::v4i32);
428
Matt Arsenault38d8ed22016-12-09 17:49:14 +0000429 // There are no libcalls of any kind.
430 for (int I = 0; I < RTLIB::UNKNOWN_LIBCALL; ++I)
431 setLibcallName(static_cast<RTLIB::Libcall>(I), nullptr);
432
Matt Arsenaultfcdddf92014-11-26 21:23:15 +0000433 setBooleanContents(ZeroOrNegativeOneBooleanContent);
434 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
435
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000436 setSchedulingPreference(Sched::RegPressure);
437 setJumpIsExpensive(true);
Matt Arsenault88716832017-01-10 19:08:15 +0000438
439 // FIXME: This is only partially true. If we have to do vector compares, any
440 // SGPR pair can be a condition register. If we have a uniform condition, we
441 // are better off doing SALU operations, where there is only one SCC. For now,
442 // we don't have a way of knowing during instruction selection if a condition
443 // will be uniform and we always use vector compares. Assume we are using
444 // vector compares until that is fixed.
Stanislav Mekhanoshin0ee250e2016-11-28 18:58:49 +0000445 setHasMultipleConditionRegisters(true);
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000446
Matt Arsenault996a0ef2014-08-09 03:46:58 +0000447 // SI at least has hardware support for floating point exceptions, but no way
448 // of using or handling them is implemented. They are also optional in OpenCL
449 // (Section 7.3)
Matt Arsenaultf639c322016-01-28 20:53:42 +0000450 setHasFloatingPointExceptions(Subtarget->hasFPExceptions());
Matt Arsenault996a0ef2014-08-09 03:46:58 +0000451
Matt Arsenaultd5f91fd2014-06-23 18:00:52 +0000452 PredictableSelectIsExpensive = false;
453
Nirav Daved32a4212017-01-26 16:46:13 +0000454 // We want to find all load dependencies for long chains of stores to enable
455 // merging into very wide vectors. The problem is with vectors with > 4
456 // elements. MergeConsecutiveStores will attempt to merge these because x8/x16
457 // vectors are a legal type, even though we have to split the loads
458 // usually. When we can more precisely specify load legality per address
459 // space, we should be able to make FindBetterChain/MergeConsecutiveStores
460 // smarter so that they can figure out what to do in 2 iterations without all
461 // N > 4 stores on the same chain.
462 GatherAllAliasesMaxDepth = 16;
463
Matt Arsenaultfd8c24e2014-06-13 17:20:53 +0000464 // FIXME: Need to really handle these.
465 MaxStoresPerMemcpy = 4096;
466 MaxStoresPerMemmove = 4096;
467 MaxStoresPerMemset = 4096;
Matt Arsenault71e66762016-05-21 02:27:49 +0000468
469 setTargetDAGCombine(ISD::BITCAST);
Matt Arsenault71e66762016-05-21 02:27:49 +0000470 setTargetDAGCombine(ISD::SHL);
471 setTargetDAGCombine(ISD::SRA);
472 setTargetDAGCombine(ISD::SRL);
473 setTargetDAGCombine(ISD::MUL);
Matt Arsenault2712d4a2016-08-27 01:32:27 +0000474 setTargetDAGCombine(ISD::MULHU);
475 setTargetDAGCombine(ISD::MULHS);
Matt Arsenault71e66762016-05-21 02:27:49 +0000476 setTargetDAGCombine(ISD::SELECT);
477 setTargetDAGCombine(ISD::SELECT_CC);
478 setTargetDAGCombine(ISD::STORE);
479 setTargetDAGCombine(ISD::FADD);
480 setTargetDAGCombine(ISD::FSUB);
Matt Arsenault2529fba2017-01-12 00:09:34 +0000481 setTargetDAGCombine(ISD::FNEG);
Matt Arsenault9dba9bd2017-02-02 02:27:04 +0000482 setTargetDAGCombine(ISD::FABS);
Tom Stellard75aadc22012-12-11 21:25:42 +0000483}
484
Tom Stellard28d06de2013-08-05 22:22:07 +0000485//===----------------------------------------------------------------------===//
486// Target Information
487//===----------------------------------------------------------------------===//
488
Matt Arsenault45337df2017-01-12 18:58:15 +0000489static bool fnegFoldsIntoOp(unsigned Opc) {
490 switch (Opc) {
491 case ISD::FADD:
492 case ISD::FSUB:
493 case ISD::FMUL:
494 case ISD::FMA:
495 case ISD::FMAD:
496 case ISD::FSIN:
Matt Arsenault53f0cc22017-01-26 01:25:36 +0000497 case ISD::FTRUNC:
498 case ISD::FRINT:
499 case ISD::FNEARBYINT:
Matt Arsenault45337df2017-01-12 18:58:15 +0000500 case AMDGPUISD::RCP:
501 case AMDGPUISD::RCP_LEGACY:
502 case AMDGPUISD::SIN_HW:
503 case AMDGPUISD::FMUL_LEGACY:
504 return true;
505 default:
506 return false;
507 }
508}
509
Mehdi Amini44ede332015-07-09 02:09:04 +0000510MVT AMDGPUTargetLowering::getVectorIdxTy(const DataLayout &) const {
Tom Stellard28d06de2013-08-05 22:22:07 +0000511 return MVT::i32;
512}
513
Matt Arsenaultd5f91fd2014-06-23 18:00:52 +0000514bool AMDGPUTargetLowering::isSelectSupported(SelectSupportKind SelType) const {
515 return true;
516}
517
Matt Arsenault14d46452014-06-15 20:23:38 +0000518// The backend supports 32 and 64 bit floating point immediates.
519// FIXME: Why are we reporting vectors of FP immediates as legal?
520bool AMDGPUTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
521 EVT ScalarVT = VT.getScalarType();
Matt Arsenault4e55c1e2016-12-22 03:05:30 +0000522 return (ScalarVT == MVT::f32 || ScalarVT == MVT::f64 ||
523 (ScalarVT == MVT::f16 && Subtarget->has16BitInsts()));
Matt Arsenault14d46452014-06-15 20:23:38 +0000524}
525
526// We don't want to shrink f64 / f32 constants.
527bool AMDGPUTargetLowering::ShouldShrinkFPConstant(EVT VT) const {
528 EVT ScalarVT = VT.getScalarType();
529 return (ScalarVT != MVT::f32 && ScalarVT != MVT::f64);
530}
531
Matt Arsenault810cb622014-12-12 00:00:24 +0000532bool AMDGPUTargetLowering::shouldReduceLoadWidth(SDNode *N,
533 ISD::LoadExtType,
534 EVT NewVT) const {
535
536 unsigned NewSize = NewVT.getStoreSizeInBits();
537
538 // If we are reducing to a 32-bit load, this is always better.
539 if (NewSize == 32)
540 return true;
541
542 EVT OldVT = N->getValueType(0);
543 unsigned OldSize = OldVT.getStoreSizeInBits();
544
545 // Don't produce extloads from sub 32-bit types. SI doesn't have scalar
546 // extloads, so doing one requires using a buffer_load. In cases where we
547 // still couldn't use a scalar load, using the wider load shouldn't really
548 // hurt anything.
549
550 // If the old size already had to be an extload, there's no harm in continuing
551 // to reduce the width.
552 return (OldSize < 32);
553}
554
Matt Arsenaultc5559bb2013-11-15 04:42:23 +0000555bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy,
556 EVT CastTy) const {
Matt Arsenaultc5559bb2013-11-15 04:42:23 +0000557
Matt Arsenault327bb5a2016-07-01 22:47:50 +0000558 assert(LoadTy.getSizeInBits() == CastTy.getSizeInBits());
Matt Arsenaultc5559bb2013-11-15 04:42:23 +0000559
Matt Arsenault327bb5a2016-07-01 22:47:50 +0000560 if (LoadTy.getScalarType() == MVT::i32)
561 return false;
562
563 unsigned LScalarSize = LoadTy.getScalarSizeInBits();
564 unsigned CastScalarSize = CastTy.getScalarSizeInBits();
565
566 return (LScalarSize < CastScalarSize) ||
567 (CastScalarSize >= 32);
Matt Arsenaultc5559bb2013-11-15 04:42:23 +0000568}
Tom Stellard28d06de2013-08-05 22:22:07 +0000569
Matt Arsenaultb56d8432015-01-13 19:46:48 +0000570// SI+ has instructions for cttz / ctlz for 32-bit values. This is probably also
571// profitable with the expansion for 64-bit since it's generally good to
572// speculate things.
573// FIXME: These should really have the size as a parameter.
574bool AMDGPUTargetLowering::isCheapToSpeculateCttz() const {
575 return true;
576}
577
578bool AMDGPUTargetLowering::isCheapToSpeculateCtlz() const {
579 return true;
580}
581
Tom Stellard75aadc22012-12-11 21:25:42 +0000582//===---------------------------------------------------------------------===//
Tom Stellardc54731a2013-07-23 23:55:03 +0000583// Target Properties
584//===---------------------------------------------------------------------===//
585
586bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
587 assert(VT.isFloatingPoint());
Matt Arsenaultc79dc702016-11-15 02:25:28 +0000588 return VT == MVT::f32 || VT == MVT::f64 || (Subtarget->has16BitInsts() &&
589 VT == MVT::f16);
Tom Stellardc54731a2013-07-23 23:55:03 +0000590}
591
592bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
Matt Arsenaultc79dc702016-11-15 02:25:28 +0000593 return isFAbsFree(VT);
Tom Stellardc54731a2013-07-23 23:55:03 +0000594}
595
Matt Arsenault65ad1602015-05-24 00:51:27 +0000596bool AMDGPUTargetLowering:: storeOfVectorConstantIsCheap(EVT MemVT,
597 unsigned NumElem,
598 unsigned AS) const {
599 return true;
600}
601
Matt Arsenault61dc2352015-10-12 23:59:50 +0000602bool AMDGPUTargetLowering::aggressivelyPreferBuildVectorSources(EVT VecVT) const {
603 // There are few operations which truly have vector input operands. Any vector
604 // operation is going to involve operations on each component, and a
605 // build_vector will be a copy per element, so it always makes sense to use a
606 // build_vector input in place of the extracted element to avoid a copy into a
607 // super register.
608 //
609 // We should probably only do this if all users are extracts only, but this
610 // should be the common case.
611 return true;
612}
613
Benjamin Kramer53f9df42014-02-12 10:17:54 +0000614bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const {
Matt Arsenault0cdcd962014-02-10 19:57:42 +0000615 // Truncate is just accessing a subregister.
Tom Stellard115a6152016-11-10 16:02:37 +0000616
617 unsigned SrcSize = Source.getSizeInBits();
618 unsigned DestSize = Dest.getSizeInBits();
619
620 return DestSize < SrcSize && DestSize % 32 == 0 ;
Benjamin Kramer53f9df42014-02-12 10:17:54 +0000621}
622
623bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const {
624 // Truncate is just accessing a subregister.
Tom Stellard115a6152016-11-10 16:02:37 +0000625
626 unsigned SrcSize = Source->getScalarSizeInBits();
627 unsigned DestSize = Dest->getScalarSizeInBits();
628
629 if (DestSize== 16 && Subtarget->has16BitInsts())
630 return SrcSize >= 32;
631
632 return DestSize < SrcSize && DestSize % 32 == 0;
Matt Arsenault0cdcd962014-02-10 19:57:42 +0000633}
634
Matt Arsenaultb517c812014-03-27 17:23:31 +0000635bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const {
Mehdi Aminia749f2a2015-07-09 02:09:52 +0000636 unsigned SrcSize = Src->getScalarSizeInBits();
637 unsigned DestSize = Dest->getScalarSizeInBits();
Matt Arsenaultb517c812014-03-27 17:23:31 +0000638
Tom Stellard115a6152016-11-10 16:02:37 +0000639 if (SrcSize == 16 && Subtarget->has16BitInsts())
640 return DestSize >= 32;
641
Matt Arsenaultb517c812014-03-27 17:23:31 +0000642 return SrcSize == 32 && DestSize == 64;
643}
644
645bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const {
646 // Any register load of a 64-bit value really requires 2 32-bit moves. For all
647 // practical purposes, the extra mov 0 to load a 64-bit is free. As used,
648 // this will enable reducing 64-bit operations the 32-bit, which is always
649 // good.
Tom Stellard115a6152016-11-10 16:02:37 +0000650
651 if (Src == MVT::i16)
652 return Dest == MVT::i32 ||Dest == MVT::i64 ;
653
Matt Arsenaultb517c812014-03-27 17:23:31 +0000654 return Src == MVT::i32 && Dest == MVT::i64;
655}
656
Aaron Ballman3c81e462014-06-26 13:45:47 +0000657bool AMDGPUTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
658 return isZExtFree(Val.getValueType(), VT2);
659}
660
Matt Arsenaulta7f1e0c2014-03-24 19:43:31 +0000661bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const {
662 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a
663 // limited number of native 64-bit operations. Shrinking an operation to fit
664 // in a single 32-bit register should always be helpful. As currently used,
665 // this is much less general than the name suggests, and is only used in
666 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is
667 // not profitable, and may actually be harmful.
668 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32;
669}
670
Tom Stellardc54731a2013-07-23 23:55:03 +0000671//===---------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +0000672// TargetLowering Callbacks
673//===---------------------------------------------------------------------===//
674
Tom Stellardca166212017-01-30 21:56:46 +0000675CCAssignFn *AMDGPUCallLowering::CCAssignFnForCall(CallingConv::ID CC,
676 bool IsVarArg) const {
677 return CC_AMDGPU;
678}
679
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000680/// The SelectionDAGBuilder will automatically promote function arguments
681/// with illegal types. However, this does not work for the AMDGPU targets
682/// since the function arguments are stored in memory as these illegal types.
683/// In order to handle this properly we need to get the original types sizes
684/// from the LLVM IR Function and fixup the ISD:InputArg values before
685/// passing them to AnalyzeFormalArguments()
Christian Konig2c8f6d52013-03-07 09:03:52 +0000686
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000687/// When the SelectionDAGBuilder computes the Ins, it takes care of splitting
688/// input values across multiple registers. Each item in the Ins array
689/// represents a single value that will be stored in regsters. Ins[x].VT is
690/// the value type of the value that will be stored in the register, so
691/// whatever SDNode we lower the argument to needs to be this type.
692///
693/// In order to correctly lower the arguments we need to know the size of each
694/// argument. Since Ins[x].VT gives us the size of the register that will
695/// hold the value, we need to look at Ins[x].ArgVT to see the 'real' type
696/// for the orignal function argument so that we can deduce the correct memory
697/// type to use for Ins[x]. In most cases the correct memory type will be
698/// Ins[x].ArgVT. However, this will not always be the case. If, for example,
699/// we have a kernel argument of type v8i8, this argument will be split into
700/// 8 parts and each part will be represented by its own item in the Ins array.
701/// For each part the Ins[x].ArgVT will be the v8i8, which is the full type of
702/// the argument before it was split. From this, we deduce that the memory type
703/// for each individual part is i8. We pass the memory type as LocVT to the
704/// calling convention analysis function and the register type (Ins[x].VT) as
705/// the ValVT.
706void AMDGPUTargetLowering::analyzeFormalArgumentsCompute(CCState &State,
707 const SmallVectorImpl<ISD::InputArg> &Ins) const {
708 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
709 const ISD::InputArg &In = Ins[i];
710 EVT MemVT;
711
712 unsigned NumRegs = getNumRegisters(State.getContext(), In.ArgVT);
713
Tom Stellard7998db62016-09-16 22:20:24 +0000714 if (!Subtarget->isAmdHsaOS() &&
715 (In.ArgVT == MVT::i16 || In.ArgVT == MVT::i8 || In.ArgVT == MVT::f16)) {
Tom Stellardbbeb45a2016-09-16 21:53:00 +0000716 // The ABI says the caller will extend these values to 32-bits.
717 MemVT = In.ArgVT.isInteger() ? MVT::i32 : MVT::f32;
718 } else if (NumRegs == 1) {
719 // This argument is not split, so the IR type is the memory type.
720 assert(!In.Flags.isSplit());
721 if (In.ArgVT.isExtended()) {
722 // We have an extended type, like i24, so we should just use the register type
723 MemVT = In.VT;
724 } else {
725 MemVT = In.ArgVT;
726 }
727 } else if (In.ArgVT.isVector() && In.VT.isVector() &&
728 In.ArgVT.getScalarType() == In.VT.getScalarType()) {
729 assert(In.ArgVT.getVectorNumElements() > In.VT.getVectorNumElements());
730 // We have a vector value which has been split into a vector with
731 // the same scalar type, but fewer elements. This should handle
732 // all the floating-point vector types.
733 MemVT = In.VT;
734 } else if (In.ArgVT.isVector() &&
735 In.ArgVT.getVectorNumElements() == NumRegs) {
736 // This arg has been split so that each element is stored in a separate
737 // register.
738 MemVT = In.ArgVT.getScalarType();
739 } else if (In.ArgVT.isExtended()) {
740 // We have an extended type, like i65.
741 MemVT = In.VT;
742 } else {
743 unsigned MemoryBits = In.ArgVT.getStoreSizeInBits() / NumRegs;
744 assert(In.ArgVT.getStoreSizeInBits() % NumRegs == 0);
745 if (In.VT.isInteger()) {
746 MemVT = EVT::getIntegerVT(State.getContext(), MemoryBits);
747 } else if (In.VT.isVector()) {
748 assert(!In.VT.getScalarType().isFloatingPoint());
749 unsigned NumElements = In.VT.getVectorNumElements();
750 assert(MemoryBits % NumElements == 0);
751 // This vector type has been split into another vector type with
752 // a different elements size.
753 EVT ScalarVT = EVT::getIntegerVT(State.getContext(),
754 MemoryBits / NumElements);
755 MemVT = EVT::getVectorVT(State.getContext(), ScalarVT, NumElements);
756 } else {
757 llvm_unreachable("cannot deduce memory type.");
758 }
759 }
760
761 // Convert one element vectors to scalar.
762 if (MemVT.isVector() && MemVT.getVectorNumElements() == 1)
763 MemVT = MemVT.getScalarType();
764
765 if (MemVT.isExtended()) {
766 // This should really only happen if we have vec3 arguments
767 assert(MemVT.isVector() && MemVT.getVectorNumElements() == 3);
768 MemVT = MemVT.getPow2VectorType(State.getContext());
769 }
770
771 assert(MemVT.isSimple());
772 allocateKernArg(i, In.VT, MemVT.getSimpleVT(), CCValAssign::Full, In.Flags,
773 State);
774 }
775}
776
777void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
778 const SmallVectorImpl<ISD::InputArg> &Ins) const {
Christian Konig2c8f6d52013-03-07 09:03:52 +0000779 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
Tom Stellard75aadc22012-12-11 21:25:42 +0000780}
781
Marek Olsak8a0f3352016-01-13 17:23:04 +0000782void AMDGPUTargetLowering::AnalyzeReturn(CCState &State,
783 const SmallVectorImpl<ISD::OutputArg> &Outs) const {
784
785 State.AnalyzeReturn(Outs, RetCC_SI);
786}
787
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000788SDValue
789AMDGPUTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
790 bool isVarArg,
791 const SmallVectorImpl<ISD::OutputArg> &Outs,
792 const SmallVectorImpl<SDValue> &OutVals,
793 const SDLoc &DL, SelectionDAG &DAG) const {
Matt Arsenault9babdf42016-06-22 20:15:28 +0000794 return DAG.getNode(AMDGPUISD::ENDPGM, DL, MVT::Other, Chain);
Tom Stellard75aadc22012-12-11 21:25:42 +0000795}
796
797//===---------------------------------------------------------------------===//
798// Target specific lowering
799//===---------------------------------------------------------------------===//
800
Matt Arsenault16353872014-04-22 16:42:00 +0000801SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI,
802 SmallVectorImpl<SDValue> &InVals) const {
803 SDValue Callee = CLI.Callee;
804 SelectionDAG &DAG = CLI.DAG;
805
806 const Function &Fn = *DAG.getMachineFunction().getFunction();
807
808 StringRef FuncName("<unknown>");
809
Matt Arsenaultde1c34102014-04-25 22:22:01 +0000810 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee))
811 FuncName = G->getSymbol();
812 else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Matt Arsenault16353872014-04-22 16:42:00 +0000813 FuncName = G->getGlobal()->getName();
814
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000815 DiagnosticInfoUnsupported NoCalls(
816 Fn, "unsupported call to function " + FuncName, CLI.DL.getDebugLoc());
Matt Arsenault16353872014-04-22 16:42:00 +0000817 DAG.getContext()->diagnose(NoCalls);
Matt Arsenault9430b912016-05-18 16:10:11 +0000818
Matt Arsenault0b386362016-12-15 20:50:12 +0000819 if (!CLI.IsTailCall) {
820 for (unsigned I = 0, E = CLI.Ins.size(); I != E; ++I)
821 InVals.push_back(DAG.getUNDEF(CLI.Ins[I].VT));
822 }
Matt Arsenault9430b912016-05-18 16:10:11 +0000823
824 return DAG.getEntryNode();
Matt Arsenault16353872014-04-22 16:42:00 +0000825}
826
Matt Arsenault19c54882015-08-26 18:37:13 +0000827SDValue AMDGPUTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
828 SelectionDAG &DAG) const {
829 const Function &Fn = *DAG.getMachineFunction().getFunction();
830
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000831 DiagnosticInfoUnsupported NoDynamicAlloca(Fn, "unsupported dynamic alloca",
832 SDLoc(Op).getDebugLoc());
Matt Arsenault19c54882015-08-26 18:37:13 +0000833 DAG.getContext()->diagnose(NoDynamicAlloca);
Diana Picuse440f992016-06-23 09:19:16 +0000834 auto Ops = {DAG.getConstant(0, SDLoc(), Op.getValueType()), Op.getOperand(0)};
835 return DAG.getMergeValues(Ops, SDLoc());
Matt Arsenault19c54882015-08-26 18:37:13 +0000836}
837
Matt Arsenault14d46452014-06-15 20:23:38 +0000838SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op,
839 SelectionDAG &DAG) const {
Tom Stellard75aadc22012-12-11 21:25:42 +0000840 switch (Op.getOpcode()) {
841 default:
Matthias Braun8c209aa2017-01-28 02:02:38 +0000842 Op->print(errs(), &DAG);
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +0000843 llvm_unreachable("Custom lowering code for this"
844 "instruction is not implemented yet!");
Tom Stellard75aadc22012-12-11 21:25:42 +0000845 break;
Tom Stellard75aadc22012-12-11 21:25:42 +0000846 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
Tom Stellardd86003e2013-08-14 23:25:00 +0000847 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
848 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000849 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
850 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
Jan Vesely109efdf2014-06-22 21:43:00 +0000851 case ISD::SDIVREM: return LowerSDIVREM(Op, DAG);
Matt Arsenault16e31332014-09-10 21:44:27 +0000852 case ISD::FREM: return LowerFREM(Op, DAG);
Matt Arsenault46010932014-06-18 17:05:30 +0000853 case ISD::FCEIL: return LowerFCEIL(Op, DAG);
854 case ISD::FTRUNC: return LowerFTRUNC(Op, DAG);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +0000855 case ISD::FRINT: return LowerFRINT(Op, DAG);
Matt Arsenault692bd5e2014-06-18 22:03:45 +0000856 case ISD::FNEARBYINT: return LowerFNEARBYINT(Op, DAG);
Matt Arsenaultb0055482015-01-21 18:18:25 +0000857 case ISD::FROUND: return LowerFROUND(Op, DAG);
Matt Arsenault46010932014-06-18 17:05:30 +0000858 case ISD::FFLOOR: return LowerFFLOOR(Op, DAG);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +0000859 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Tom Stellardc947d8c2013-10-30 17:22:05 +0000860 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Tom Stellard94c21bc2016-11-01 16:31:48 +0000861 case ISD::FP_TO_FP16: return LowerFP_TO_FP16(Op, DAG);
Matt Arsenaultc9961752014-10-03 23:54:56 +0000862 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
863 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Matt Arsenaultf058d672016-01-11 16:50:29 +0000864 case ISD::CTLZ:
865 case ISD::CTLZ_ZERO_UNDEF:
866 return LowerCTLZ(Op, DAG);
Matt Arsenault19c54882015-08-26 18:37:13 +0000867 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Tom Stellard75aadc22012-12-11 21:25:42 +0000868 }
869 return Op;
870}
871
Matt Arsenaultd125d742014-03-27 17:23:24 +0000872void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N,
873 SmallVectorImpl<SDValue> &Results,
874 SelectionDAG &DAG) const {
875 switch (N->getOpcode()) {
876 case ISD::SIGN_EXTEND_INREG:
877 // Different parts of legalization seem to interpret which type of
878 // sign_extend_inreg is the one to check for custom lowering. The extended
879 // from type is what really matters, but some places check for custom
880 // lowering of the result type. This results in trying to use
881 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do
882 // nothing here and let the illegal result integer be handled normally.
883 return;
Matt Arsenaultd125d742014-03-27 17:23:24 +0000884 default:
885 return;
886 }
887}
888
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000889static bool hasDefinedInitializer(const GlobalValue *GV) {
890 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
891 if (!GVar || !GVar->hasInitializer())
892 return false;
893
Matt Arsenault8226fc42016-03-02 23:00:21 +0000894 return !isa<UndefValue>(GVar->getInitializer());
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000895}
896
Tom Stellardc026e8b2013-06-28 15:47:08 +0000897SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
898 SDValue Op,
899 SelectionDAG &DAG) const {
900
Mehdi Amini44ede332015-07-09 02:09:04 +0000901 const DataLayout &DL = DAG.getDataLayout();
Tom Stellardc026e8b2013-06-28 15:47:08 +0000902 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
Tom Stellardc026e8b2013-06-28 15:47:08 +0000903 const GlobalValue *GV = G->getGlobal();
Tom Stellardc026e8b2013-06-28 15:47:08 +0000904
Tom Stellard04c0e982014-01-22 19:24:21 +0000905 switch (G->getAddressSpace()) {
Tom Stellard04c0e982014-01-22 19:24:21 +0000906 case AMDGPUAS::LOCAL_ADDRESS: {
907 // XXX: What does the value of G->getOffset() mean?
908 assert(G->getOffset() == 0 &&
909 "Do not know what to do with an non-zero offset");
Tom Stellardc026e8b2013-06-28 15:47:08 +0000910
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000911 // TODO: We could emit code to handle the initialization somewhere.
912 if (hasDefinedInitializer(GV))
913 break;
914
Matt Arsenault52ef4012016-07-26 16:45:58 +0000915 unsigned Offset = MFI->allocateLDSGlobal(DL, *GV);
916 return DAG.getConstant(Offset, SDLoc(Op), Op.getValueType());
Tom Stellard04c0e982014-01-22 19:24:21 +0000917 }
Tom Stellard04c0e982014-01-22 19:24:21 +0000918 }
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000919
920 const Function &Fn = *DAG.getMachineFunction().getFunction();
Oliver Stannard7e7d9832016-02-02 13:52:43 +0000921 DiagnosticInfoUnsupported BadInit(
922 Fn, "unsupported initializer for address space", SDLoc(Op).getDebugLoc());
Matt Arsenaultcc8d3b82014-11-13 19:56:13 +0000923 DAG.getContext()->diagnose(BadInit);
924 return SDValue();
Tom Stellardc026e8b2013-06-28 15:47:08 +0000925}
926
Tom Stellardd86003e2013-08-14 23:25:00 +0000927SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
928 SelectionDAG &DAG) const {
929 SmallVector<SDValue, 8> Args;
Tom Stellardd86003e2013-08-14 23:25:00 +0000930
Tom Stellardff5cf0e2015-04-23 22:59:24 +0000931 for (const SDUse &U : Op->ops())
932 DAG.ExtractVectorElements(U.get(), Args);
Tom Stellardd86003e2013-08-14 23:25:00 +0000933
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000934 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
Tom Stellardd86003e2013-08-14 23:25:00 +0000935}
936
937SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
938 SelectionDAG &DAG) const {
939
940 SmallVector<SDValue, 8> Args;
Tom Stellardd86003e2013-08-14 23:25:00 +0000941 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Matt Arsenault9ec3cf22014-04-11 17:47:30 +0000942 EVT VT = Op.getValueType();
943 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start,
944 VT.getVectorNumElements());
Tom Stellardd86003e2013-08-14 23:25:00 +0000945
Ahmed Bougacha128f8732016-04-26 21:15:30 +0000946 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
Tom Stellardd86003e2013-08-14 23:25:00 +0000947}
948
Tom Stellard75aadc22012-12-11 21:25:42 +0000949SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
950 SelectionDAG &DAG) const {
951 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000952 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +0000953 EVT VT = Op.getValueType();
954
955 switch (IntrinsicID) {
956 default: return Op;
Matt Arsenaultf0711022016-07-13 19:42:06 +0000957 case AMDGPUIntrinsic::AMDGPU_clamp: // Legacy name.
Matt Arsenault5d47d4a2014-06-12 21:15:44 +0000958 return DAG.getNode(AMDGPUISD::CLAMP, DL, VT,
959 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
960
Matt Arsenault4c537172014-03-31 18:21:18 +0000961 case AMDGPUIntrinsic::AMDGPU_bfe_i32:
962 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT,
963 Op.getOperand(1),
964 Op.getOperand(2),
965 Op.getOperand(3));
966
967 case AMDGPUIntrinsic::AMDGPU_bfe_u32:
968 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT,
969 Op.getOperand(1),
970 Op.getOperand(2),
971 Op.getOperand(3));
Tom Stellard75aadc22012-12-11 21:25:42 +0000972 }
973}
974
Tom Stellard75aadc22012-12-11 21:25:42 +0000975/// \brief Generate Min/Max node
Matt Arsenaultda7a6562017-02-01 00:42:40 +0000976SDValue AMDGPUTargetLowering::combineFMinMaxLegacy(const SDLoc &DL, EVT VT,
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000977 SDValue LHS, SDValue RHS,
978 SDValue True, SDValue False,
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000979 SDValue CC,
980 DAGCombinerInfo &DCI) const {
Matt Arsenaultda59f3d2014-11-13 23:03:09 +0000981 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True))
982 return SDValue();
Tom Stellard75aadc22012-12-11 21:25:42 +0000983
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +0000984 SelectionDAG &DAG = DCI.DAG;
Tom Stellard75aadc22012-12-11 21:25:42 +0000985 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
986 switch (CCOpcode) {
987 case ISD::SETOEQ:
988 case ISD::SETONE:
989 case ISD::SETUNE:
990 case ISD::SETNE:
991 case ISD::SETUEQ:
992 case ISD::SETEQ:
993 case ISD::SETFALSE:
994 case ISD::SETFALSE2:
995 case ISD::SETTRUE:
996 case ISD::SETTRUE2:
997 case ISD::SETUO:
998 case ISD::SETO:
Matt Arsenaultda59f3d2014-11-13 23:03:09 +0000999 break;
Tom Stellard75aadc22012-12-11 21:25:42 +00001000 case ISD::SETULE:
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001001 case ISD::SETULT: {
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001002 if (LHS == True)
1003 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1004 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1005 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001006 case ISD::SETOLE:
1007 case ISD::SETOLT:
1008 case ISD::SETLE:
1009 case ISD::SETLT: {
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001010 // Ordered. Assume ordered for undefined.
1011
1012 // Only do this after legalization to avoid interfering with other combines
1013 // which might occur.
1014 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1015 !DCI.isCalledByLegalizer())
1016 return SDValue();
Marek Olsakbe047802014-12-07 12:19:03 +00001017
Matt Arsenault36094d72014-11-15 05:02:57 +00001018 // We need to permute the operands to get the correct NaN behavior. The
1019 // selected operand is the second one based on the failing compare with NaN,
1020 // so permute it based on the compare type the hardware uses.
1021 if (LHS == True)
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001022 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
1023 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
Tom Stellard75aadc22012-12-11 21:25:42 +00001024 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001025 case ISD::SETUGE:
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001026 case ISD::SETUGT: {
Matt Arsenault36094d72014-11-15 05:02:57 +00001027 if (LHS == True)
1028 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
1029 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
Tom Stellard75aadc22012-12-11 21:25:42 +00001030 }
Matt Arsenault1e3a4eb2014-12-12 02:30:37 +00001031 case ISD::SETGT:
1032 case ISD::SETGE:
1033 case ISD::SETOGE:
1034 case ISD::SETOGT: {
1035 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1036 !DCI.isCalledByLegalizer())
1037 return SDValue();
1038
1039 if (LHS == True)
1040 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1041 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1042 }
Tom Stellard75aadc22012-12-11 21:25:42 +00001043 case ISD::SETCC_INVALID:
Matt Arsenaulteaa3a7e2013-12-10 21:37:42 +00001044 llvm_unreachable("Invalid setcc condcode!");
Tom Stellard75aadc22012-12-11 21:25:42 +00001045 }
Tom Stellardafa8b532014-05-09 16:42:16 +00001046 return SDValue();
Tom Stellard75aadc22012-12-11 21:25:42 +00001047}
1048
Matt Arsenault6e3a4512016-01-18 22:01:13 +00001049std::pair<SDValue, SDValue>
1050AMDGPUTargetLowering::split64BitValue(SDValue Op, SelectionDAG &DAG) const {
1051 SDLoc SL(Op);
1052
1053 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1054
1055 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1056 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1057
1058 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1059 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1060
1061 return std::make_pair(Lo, Hi);
1062}
1063
Matt Arsenault33e3ece2016-01-18 22:09:04 +00001064SDValue AMDGPUTargetLowering::getLoHalf64(SDValue Op, SelectionDAG &DAG) const {
1065 SDLoc SL(Op);
1066
1067 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1068 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1069 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1070}
1071
1072SDValue AMDGPUTargetLowering::getHiHalf64(SDValue Op, SelectionDAG &DAG) const {
1073 SDLoc SL(Op);
1074
1075 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1076 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1077 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1078}
1079
Matt Arsenault83e60582014-07-24 17:10:35 +00001080SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue Op,
1081 SelectionDAG &DAG) const {
Matt Arsenault9c499c32016-04-14 23:31:26 +00001082 LoadSDNode *Load = cast<LoadSDNode>(Op);
Matt Arsenault83e60582014-07-24 17:10:35 +00001083 EVT VT = Op.getValueType();
1084
Matt Arsenault9c499c32016-04-14 23:31:26 +00001085
Matt Arsenault83e60582014-07-24 17:10:35 +00001086 // If this is a 2 element vector, we really want to scalarize and not create
1087 // weird 1 element vectors.
1088 if (VT.getVectorNumElements() == 2)
Matt Arsenault9c499c32016-04-14 23:31:26 +00001089 return scalarizeVectorLoad(Load, DAG);
Matt Arsenault83e60582014-07-24 17:10:35 +00001090
Matt Arsenault83e60582014-07-24 17:10:35 +00001091 SDValue BasePtr = Load->getBasePtr();
1092 EVT PtrVT = BasePtr.getValueType();
1093 EVT MemVT = Load->getMemoryVT();
1094 SDLoc SL(Op);
Matt Arsenault52a52a52015-12-14 16:59:40 +00001095
1096 const MachinePointerInfo &SrcValue = Load->getMemOperand()->getPointerInfo();
Matt Arsenault83e60582014-07-24 17:10:35 +00001097
1098 EVT LoVT, HiVT;
1099 EVT LoMemVT, HiMemVT;
1100 SDValue Lo, Hi;
1101
1102 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1103 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1104 std::tie(Lo, Hi) = DAG.SplitVector(Op, SL, LoVT, HiVT);
Matt Arsenault52a52a52015-12-14 16:59:40 +00001105
1106 unsigned Size = LoMemVT.getStoreSize();
1107 unsigned BaseAlign = Load->getAlignment();
1108 unsigned HiAlign = MinAlign(BaseAlign, Size);
1109
Justin Lebar9c375812016-07-15 18:27:10 +00001110 SDValue LoLoad = DAG.getExtLoad(Load->getExtensionType(), SL, LoVT,
1111 Load->getChain(), BasePtr, SrcValue, LoMemVT,
1112 BaseAlign, Load->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001113 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
Matt Arsenault52a52a52015-12-14 16:59:40 +00001114 DAG.getConstant(Size, SL, PtrVT));
Justin Lebar9c375812016-07-15 18:27:10 +00001115 SDValue HiLoad =
1116 DAG.getExtLoad(Load->getExtensionType(), SL, HiVT, Load->getChain(),
1117 HiPtr, SrcValue.getWithOffset(LoMemVT.getStoreSize()),
1118 HiMemVT, HiAlign, Load->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001119
1120 SDValue Ops[] = {
1121 DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, LoLoad, HiLoad),
1122 DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
1123 LoLoad.getValue(1), HiLoad.getValue(1))
1124 };
1125
1126 return DAG.getMergeValues(Ops, SL);
1127}
1128
Matt Arsenault83e60582014-07-24 17:10:35 +00001129SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
1130 SelectionDAG &DAG) const {
1131 StoreSDNode *Store = cast<StoreSDNode>(Op);
1132 SDValue Val = Store->getValue();
1133 EVT VT = Val.getValueType();
1134
1135 // If this is a 2 element vector, we really want to scalarize and not create
1136 // weird 1 element vectors.
1137 if (VT.getVectorNumElements() == 2)
Matt Arsenault9c499c32016-04-14 23:31:26 +00001138 return scalarizeVectorStore(Store, DAG);
Matt Arsenault83e60582014-07-24 17:10:35 +00001139
1140 EVT MemVT = Store->getMemoryVT();
1141 SDValue Chain = Store->getChain();
1142 SDValue BasePtr = Store->getBasePtr();
1143 SDLoc SL(Op);
1144
1145 EVT LoVT, HiVT;
1146 EVT LoMemVT, HiMemVT;
1147 SDValue Lo, Hi;
1148
1149 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1150 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1151 std::tie(Lo, Hi) = DAG.SplitVector(Val, SL, LoVT, HiVT);
1152
1153 EVT PtrVT = BasePtr.getValueType();
1154 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001155 DAG.getConstant(LoMemVT.getStoreSize(), SL,
1156 PtrVT));
Matt Arsenault83e60582014-07-24 17:10:35 +00001157
Matt Arsenault52a52a52015-12-14 16:59:40 +00001158 const MachinePointerInfo &SrcValue = Store->getMemOperand()->getPointerInfo();
1159 unsigned BaseAlign = Store->getAlignment();
1160 unsigned Size = LoMemVT.getStoreSize();
1161 unsigned HiAlign = MinAlign(BaseAlign, Size);
1162
Justin Lebar9c375812016-07-15 18:27:10 +00001163 SDValue LoStore =
1164 DAG.getTruncStore(Chain, SL, Lo, BasePtr, SrcValue, LoMemVT, BaseAlign,
1165 Store->getMemOperand()->getFlags());
1166 SDValue HiStore =
1167 DAG.getTruncStore(Chain, SL, Hi, HiPtr, SrcValue.getWithOffset(Size),
1168 HiMemVT, HiAlign, Store->getMemOperand()->getFlags());
Matt Arsenault83e60582014-07-24 17:10:35 +00001169
1170 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoStore, HiStore);
1171}
1172
Matt Arsenault0daeb632014-07-24 06:59:20 +00001173// This is a shortcut for integer division because we have fast i32<->f32
1174// conversions, and fast f32 reciprocal instructions. The fractional part of a
Matt Arsenault81a70952016-05-21 01:53:33 +00001175// float is enough to accurately represent up to a 24-bit signed integer.
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001176SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG,
1177 bool Sign) const {
Matt Arsenault1578aa72014-06-15 20:08:02 +00001178 SDLoc DL(Op);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001179 EVT VT = Op.getValueType();
Matt Arsenault1578aa72014-06-15 20:08:02 +00001180 SDValue LHS = Op.getOperand(0);
1181 SDValue RHS = Op.getOperand(1);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001182 MVT IntVT = MVT::i32;
1183 MVT FltVT = MVT::f32;
1184
Matt Arsenault81a70952016-05-21 01:53:33 +00001185 unsigned LHSSignBits = DAG.ComputeNumSignBits(LHS);
1186 if (LHSSignBits < 9)
1187 return SDValue();
1188
1189 unsigned RHSSignBits = DAG.ComputeNumSignBits(RHS);
1190 if (RHSSignBits < 9)
1191 return SDValue();
Jan Veselye5ca27d2014-08-12 17:31:20 +00001192
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001193 unsigned BitSize = VT.getSizeInBits();
Matt Arsenault81a70952016-05-21 01:53:33 +00001194 unsigned SignBits = std::min(LHSSignBits, RHSSignBits);
1195 unsigned DivBits = BitSize - SignBits;
1196 if (Sign)
1197 ++DivBits;
1198
1199 ISD::NodeType ToFp = Sign ? ISD::SINT_TO_FP : ISD::UINT_TO_FP;
1200 ISD::NodeType ToInt = Sign ? ISD::FP_TO_SINT : ISD::FP_TO_UINT;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001201
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001202 SDValue jq = DAG.getConstant(1, DL, IntVT);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001203
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001204 if (Sign) {
Jan Veselye5ca27d2014-08-12 17:31:20 +00001205 // char|short jq = ia ^ ib;
1206 jq = DAG.getNode(ISD::XOR, DL, VT, LHS, RHS);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001207
Jan Veselye5ca27d2014-08-12 17:31:20 +00001208 // jq = jq >> (bitsize - 2)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001209 jq = DAG.getNode(ISD::SRA, DL, VT, jq,
1210 DAG.getConstant(BitSize - 2, DL, VT));
Matt Arsenault1578aa72014-06-15 20:08:02 +00001211
Jan Veselye5ca27d2014-08-12 17:31:20 +00001212 // jq = jq | 0x1
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001213 jq = DAG.getNode(ISD::OR, DL, VT, jq, DAG.getConstant(1, DL, VT));
Jan Veselye5ca27d2014-08-12 17:31:20 +00001214 }
Matt Arsenault1578aa72014-06-15 20:08:02 +00001215
1216 // int ia = (int)LHS;
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001217 SDValue ia = LHS;
Matt Arsenault1578aa72014-06-15 20:08:02 +00001218
1219 // int ib, (int)RHS;
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001220 SDValue ib = RHS;
Matt Arsenault1578aa72014-06-15 20:08:02 +00001221
1222 // float fa = (float)ia;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001223 SDValue fa = DAG.getNode(ToFp, DL, FltVT, ia);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001224
1225 // float fb = (float)ib;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001226 SDValue fb = DAG.getNode(ToFp, DL, FltVT, ib);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001227
Matt Arsenault0daeb632014-07-24 06:59:20 +00001228 SDValue fq = DAG.getNode(ISD::FMUL, DL, FltVT,
1229 fa, DAG.getNode(AMDGPUISD::RCP, DL, FltVT, fb));
Matt Arsenault1578aa72014-06-15 20:08:02 +00001230
1231 // fq = trunc(fq);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001232 fq = DAG.getNode(ISD::FTRUNC, DL, FltVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001233
1234 // float fqneg = -fq;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001235 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FltVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001236
1237 // float fr = mad(fqneg, fb, fa);
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001238 SDValue fr = DAG.getNode(ISD::FMAD, DL, FltVT, fqneg, fb, fa);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001239
1240 // int iq = (int)fq;
Jan Veselye5ca27d2014-08-12 17:31:20 +00001241 SDValue iq = DAG.getNode(ToInt, DL, IntVT, fq);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001242
1243 // fr = fabs(fr);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001244 fr = DAG.getNode(ISD::FABS, DL, FltVT, fr);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001245
1246 // fb = fabs(fb);
Matt Arsenault0daeb632014-07-24 06:59:20 +00001247 fb = DAG.getNode(ISD::FABS, DL, FltVT, fb);
1248
Mehdi Amini44ede332015-07-09 02:09:04 +00001249 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001250
1251 // int cv = fr >= fb;
Matt Arsenault0daeb632014-07-24 06:59:20 +00001252 SDValue cv = DAG.getSetCC(DL, SetCCVT, fr, fb, ISD::SETOGE);
1253
Matt Arsenault1578aa72014-06-15 20:08:02 +00001254 // jq = (cv ? jq : 0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001255 jq = DAG.getNode(ISD::SELECT, DL, VT, cv, jq, DAG.getConstant(0, DL, VT));
Matt Arsenault0daeb632014-07-24 06:59:20 +00001256
Jan Veselye5ca27d2014-08-12 17:31:20 +00001257 // dst = iq + jq;
Jan Vesely4a33bc62014-08-12 17:31:17 +00001258 SDValue Div = DAG.getNode(ISD::ADD, DL, VT, iq, jq);
1259
Jan Veselye5ca27d2014-08-12 17:31:20 +00001260 // Rem needs compensation, it's easier to recompute it
Jan Vesely4a33bc62014-08-12 17:31:17 +00001261 SDValue Rem = DAG.getNode(ISD::MUL, DL, VT, Div, RHS);
1262 Rem = DAG.getNode(ISD::SUB, DL, VT, LHS, Rem);
1263
Matt Arsenault81a70952016-05-21 01:53:33 +00001264 // Truncate to number of bits this divide really is.
1265 if (Sign) {
1266 SDValue InRegSize
1267 = DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), DivBits));
1268 Div = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Div, InRegSize);
1269 Rem = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Rem, InRegSize);
1270 } else {
1271 SDValue TruncMask = DAG.getConstant((UINT64_C(1) << DivBits) - 1, DL, VT);
1272 Div = DAG.getNode(ISD::AND, DL, VT, Div, TruncMask);
1273 Rem = DAG.getNode(ISD::AND, DL, VT, Rem, TruncMask);
1274 }
1275
Matt Arsenault4e3d3832016-05-19 21:09:58 +00001276 return DAG.getMergeValues({ Div, Rem }, DL);
Matt Arsenault1578aa72014-06-15 20:08:02 +00001277}
1278
Tom Stellardbf69d762014-11-15 01:07:53 +00001279void AMDGPUTargetLowering::LowerUDIVREM64(SDValue Op,
1280 SelectionDAG &DAG,
1281 SmallVectorImpl<SDValue> &Results) const {
1282 assert(Op.getValueType() == MVT::i64);
1283
1284 SDLoc DL(Op);
1285 EVT VT = Op.getValueType();
1286 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1287
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001288 SDValue one = DAG.getConstant(1, DL, HalfVT);
1289 SDValue zero = DAG.getConstant(0, DL, HalfVT);
Tom Stellardbf69d762014-11-15 01:07:53 +00001290
1291 //HiLo split
1292 SDValue LHS = Op.getOperand(0);
1293 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero);
1294 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one);
1295
1296 SDValue RHS = Op.getOperand(1);
1297 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero);
1298 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one);
1299
Jan Vesely5f715d32015-01-22 23:42:43 +00001300 if (VT == MVT::i64 &&
1301 DAG.MaskedValueIsZero(RHS, APInt::getHighBitsSet(64, 32)) &&
1302 DAG.MaskedValueIsZero(LHS, APInt::getHighBitsSet(64, 32))) {
1303
1304 SDValue Res = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1305 LHS_Lo, RHS_Lo);
1306
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001307 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(0), zero});
1308 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(1), zero});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001309
1310 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV));
1311 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM));
Jan Vesely5f715d32015-01-22 23:42:43 +00001312 return;
1313 }
1314
Tom Stellardbf69d762014-11-15 01:07:53 +00001315 // Get Speculative values
1316 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo);
1317 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo);
1318
Tom Stellardbf69d762014-11-15 01:07:53 +00001319 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ);
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001320 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {REM_Lo, zero});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001321 REM = DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM);
Tom Stellardbf69d762014-11-15 01:07:53 +00001322
1323 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ);
1324 SDValue DIV_Lo = zero;
1325
1326 const unsigned halfBitWidth = HalfVT.getSizeInBits();
1327
1328 for (unsigned i = 0; i < halfBitWidth; ++i) {
Jan Veselyf7987ca2015-01-22 23:42:39 +00001329 const unsigned bitPos = halfBitWidth - i - 1;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001330 SDValue POS = DAG.getConstant(bitPos, DL, HalfVT);
Jan Veselyf7987ca2015-01-22 23:42:39 +00001331 // Get value of high bit
Jan Vesely811ef522015-04-12 23:45:01 +00001332 SDValue HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS);
1333 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one);
Jan Veselyf7987ca2015-01-22 23:42:39 +00001334 HBit = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, HBit);
Tom Stellardbf69d762014-11-15 01:07:53 +00001335
Jan Veselyf7987ca2015-01-22 23:42:39 +00001336 // Shift
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001337 REM = DAG.getNode(ISD::SHL, DL, VT, REM, DAG.getConstant(1, DL, VT));
Jan Veselyf7987ca2015-01-22 23:42:39 +00001338 // Add LHS high bit
1339 REM = DAG.getNode(ISD::OR, DL, VT, REM, HBit);
Tom Stellardbf69d762014-11-15 01:07:53 +00001340
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +00001341 SDValue BIT = DAG.getConstant(1ULL << bitPos, DL, HalfVT);
Tom Stellard83171b32014-11-15 01:07:57 +00001342 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETUGE);
Tom Stellardbf69d762014-11-15 01:07:53 +00001343
1344 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT);
1345
1346 // Update REM
Tom Stellardbf69d762014-11-15 01:07:53 +00001347 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS);
Tom Stellard83171b32014-11-15 01:07:57 +00001348 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETUGE);
Tom Stellardbf69d762014-11-15 01:07:53 +00001349 }
1350
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001351 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {DIV_Lo, DIV_Hi});
Matt Arsenaultd275fca2016-03-01 05:06:05 +00001352 DIV = DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV);
Tom Stellardbf69d762014-11-15 01:07:53 +00001353 Results.push_back(DIV);
1354 Results.push_back(REM);
1355}
1356
Tom Stellard75aadc22012-12-11 21:25:42 +00001357SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
Matt Arsenault46013d92014-05-11 21:24:41 +00001358 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001359 SDLoc DL(Op);
Tom Stellard75aadc22012-12-11 21:25:42 +00001360 EVT VT = Op.getValueType();
1361
Tom Stellardbf69d762014-11-15 01:07:53 +00001362 if (VT == MVT::i64) {
1363 SmallVector<SDValue, 2> Results;
1364 LowerUDIVREM64(Op, DAG, Results);
1365 return DAG.getMergeValues(Results, DL);
1366 }
1367
Matt Arsenault81a70952016-05-21 01:53:33 +00001368 if (VT == MVT::i32) {
1369 if (SDValue Res = LowerDIVREM24(Op, DAG, false))
1370 return Res;
1371 }
1372
Tom Stellard75aadc22012-12-11 21:25:42 +00001373 SDValue Num = Op.getOperand(0);
1374 SDValue Den = Op.getOperand(1);
1375
Tom Stellard75aadc22012-12-11 21:25:42 +00001376 // RCP = URECIP(Den) = 2^32 / Den + e
1377 // e is rounding error.
1378 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
1379
Tom Stellard4349b192014-09-22 15:35:30 +00001380 // RCP_LO = mul(RCP, Den) */
1381 SDValue RCP_LO = DAG.getNode(ISD::MUL, DL, VT, RCP, Den);
Tom Stellard75aadc22012-12-11 21:25:42 +00001382
1383 // RCP_HI = mulhu (RCP, Den) */
1384 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
1385
1386 // NEG_RCP_LO = -RCP_LO
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001387 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001388 RCP_LO);
1389
1390 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001391 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001392 NEG_RCP_LO, RCP_LO,
1393 ISD::SETEQ);
1394 // Calculate the rounding error from the URECIP instruction
1395 // E = mulhu(ABS_RCP_LO, RCP)
1396 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
1397
1398 // RCP_A_E = RCP + E
1399 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
1400
1401 // RCP_S_E = RCP - E
1402 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
1403
1404 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001405 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001406 RCP_A_E, RCP_S_E,
1407 ISD::SETEQ);
1408 // Quotient = mulhu(Tmp0, Num)
1409 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
1410
1411 // Num_S_Remainder = Quotient * Den
Tom Stellard4349b192014-09-22 15:35:30 +00001412 SDValue Num_S_Remainder = DAG.getNode(ISD::MUL, DL, VT, Quotient, Den);
Tom Stellard75aadc22012-12-11 21:25:42 +00001413
1414 // Remainder = Num - Num_S_Remainder
1415 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
1416
1417 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
1418 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001419 DAG.getConstant(-1, DL, VT),
1420 DAG.getConstant(0, DL, VT),
Vincent Lejeune4f3751f2013-11-06 17:36:04 +00001421 ISD::SETUGE);
1422 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
1423 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
1424 Num_S_Remainder,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001425 DAG.getConstant(-1, DL, VT),
1426 DAG.getConstant(0, DL, VT),
Vincent Lejeune4f3751f2013-11-06 17:36:04 +00001427 ISD::SETUGE);
Tom Stellard75aadc22012-12-11 21:25:42 +00001428 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
1429 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
1430 Remainder_GE_Zero);
1431
1432 // Calculate Division result:
1433
1434 // Quotient_A_One = Quotient + 1
1435 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001436 DAG.getConstant(1, DL, VT));
Tom Stellard75aadc22012-12-11 21:25:42 +00001437
1438 // Quotient_S_One = Quotient - 1
1439 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001440 DAG.getConstant(1, DL, VT));
Tom Stellard75aadc22012-12-11 21:25:42 +00001441
1442 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001443 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001444 Quotient, Quotient_A_One, ISD::SETEQ);
1445
1446 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001447 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001448 Quotient_S_One, Div, ISD::SETEQ);
1449
1450 // Calculate Rem result:
1451
1452 // Remainder_S_Den = Remainder - Den
1453 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
1454
1455 // Remainder_A_Den = Remainder + Den
1456 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
1457
1458 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001459 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001460 Remainder, Remainder_S_Den, ISD::SETEQ);
1461
1462 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001463 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
Tom Stellard75aadc22012-12-11 21:25:42 +00001464 Remainder_A_Den, Rem, ISD::SETEQ);
Matt Arsenault7939acd2014-04-07 16:44:24 +00001465 SDValue Ops[2] = {
1466 Div,
1467 Rem
1468 };
Craig Topper64941d92014-04-27 19:20:57 +00001469 return DAG.getMergeValues(Ops, DL);
Tom Stellard75aadc22012-12-11 21:25:42 +00001470}
1471
Jan Vesely109efdf2014-06-22 21:43:00 +00001472SDValue AMDGPUTargetLowering::LowerSDIVREM(SDValue Op,
1473 SelectionDAG &DAG) const {
1474 SDLoc DL(Op);
1475 EVT VT = Op.getValueType();
1476
Jan Vesely109efdf2014-06-22 21:43:00 +00001477 SDValue LHS = Op.getOperand(0);
1478 SDValue RHS = Op.getOperand(1);
1479
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001480 SDValue Zero = DAG.getConstant(0, DL, VT);
1481 SDValue NegOne = DAG.getConstant(-1, DL, VT);
Jan Vesely4a33bc62014-08-12 17:31:17 +00001482
Matt Arsenault81a70952016-05-21 01:53:33 +00001483 if (VT == MVT::i32) {
1484 if (SDValue Res = LowerDIVREM24(Op, DAG, true))
1485 return Res;
Jan Vesely5f715d32015-01-22 23:42:43 +00001486 }
Matt Arsenault81a70952016-05-21 01:53:33 +00001487
Jan Vesely5f715d32015-01-22 23:42:43 +00001488 if (VT == MVT::i64 &&
1489 DAG.ComputeNumSignBits(LHS) > 32 &&
1490 DAG.ComputeNumSignBits(RHS) > 32) {
1491 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1492
1493 //HiLo split
1494 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero);
1495 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero);
1496 SDValue DIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1497 LHS_Lo, RHS_Lo);
1498 SDValue Res[2] = {
1499 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(0)),
1500 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(1))
1501 };
1502 return DAG.getMergeValues(Res, DL);
1503 }
1504
Jan Vesely109efdf2014-06-22 21:43:00 +00001505 SDValue LHSign = DAG.getSelectCC(DL, LHS, Zero, NegOne, Zero, ISD::SETLT);
1506 SDValue RHSign = DAG.getSelectCC(DL, RHS, Zero, NegOne, Zero, ISD::SETLT);
1507 SDValue DSign = DAG.getNode(ISD::XOR, DL, VT, LHSign, RHSign);
1508 SDValue RSign = LHSign; // Remainder sign is the same as LHS
1509
1510 LHS = DAG.getNode(ISD::ADD, DL, VT, LHS, LHSign);
1511 RHS = DAG.getNode(ISD::ADD, DL, VT, RHS, RHSign);
1512
1513 LHS = DAG.getNode(ISD::XOR, DL, VT, LHS, LHSign);
1514 RHS = DAG.getNode(ISD::XOR, DL, VT, RHS, RHSign);
1515
1516 SDValue Div = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT), LHS, RHS);
1517 SDValue Rem = Div.getValue(1);
1518
1519 Div = DAG.getNode(ISD::XOR, DL, VT, Div, DSign);
1520 Rem = DAG.getNode(ISD::XOR, DL, VT, Rem, RSign);
1521
1522 Div = DAG.getNode(ISD::SUB, DL, VT, Div, DSign);
1523 Rem = DAG.getNode(ISD::SUB, DL, VT, Rem, RSign);
1524
1525 SDValue Res[2] = {
1526 Div,
1527 Rem
1528 };
1529 return DAG.getMergeValues(Res, DL);
1530}
1531
Matt Arsenault16e31332014-09-10 21:44:27 +00001532// (frem x, y) -> (fsub x, (fmul (ftrunc (fdiv x, y)), y))
1533SDValue AMDGPUTargetLowering::LowerFREM(SDValue Op, SelectionDAG &DAG) const {
1534 SDLoc SL(Op);
1535 EVT VT = Op.getValueType();
1536 SDValue X = Op.getOperand(0);
1537 SDValue Y = Op.getOperand(1);
1538
Sanjay Patela2607012015-09-16 16:31:21 +00001539 // TODO: Should this propagate fast-math-flags?
1540
Matt Arsenault16e31332014-09-10 21:44:27 +00001541 SDValue Div = DAG.getNode(ISD::FDIV, SL, VT, X, Y);
1542 SDValue Floor = DAG.getNode(ISD::FTRUNC, SL, VT, Div);
1543 SDValue Mul = DAG.getNode(ISD::FMUL, SL, VT, Floor, Y);
1544
1545 return DAG.getNode(ISD::FSUB, SL, VT, X, Mul);
1546}
1547
Matt Arsenault46010932014-06-18 17:05:30 +00001548SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const {
1549 SDLoc SL(Op);
1550 SDValue Src = Op.getOperand(0);
1551
1552 // result = trunc(src)
1553 // if (src > 0.0 && src != result)
1554 // result += 1.0
1555
1556 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1557
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001558 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1559 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001560
Mehdi Amini44ede332015-07-09 02:09:04 +00001561 EVT SetCCVT =
1562 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001563
1564 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT);
1565 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1566 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1567
1568 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero);
Sanjay Patela2607012015-09-16 16:31:21 +00001569 // TODO: Should this propagate fast-math-flags?
Matt Arsenault46010932014-06-18 17:05:30 +00001570 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1571}
1572
Benjamin Kramerbdc49562016-06-12 15:39:02 +00001573static SDValue extractF64Exponent(SDValue Hi, const SDLoc &SL,
1574 SelectionDAG &DAG) {
Matt Arsenaultb0055482015-01-21 18:18:25 +00001575 const unsigned FractBits = 52;
1576 const unsigned ExpBits = 11;
1577
1578 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32,
1579 Hi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001580 DAG.getConstant(FractBits - 32, SL, MVT::i32),
1581 DAG.getConstant(ExpBits, SL, MVT::i32));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001582 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001583 DAG.getConstant(1023, SL, MVT::i32));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001584
1585 return Exp;
1586}
1587
Matt Arsenault46010932014-06-18 17:05:30 +00001588SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const {
1589 SDLoc SL(Op);
1590 SDValue Src = Op.getOperand(0);
1591
1592 assert(Op.getValueType() == MVT::f64);
1593
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001594 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1595 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001596
1597 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1598
1599 // Extract the upper half, since this is where we will find the sign and
1600 // exponent.
1601 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One);
1602
Matt Arsenaultb0055482015-01-21 18:18:25 +00001603 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
Matt Arsenault46010932014-06-18 17:05:30 +00001604
Matt Arsenaultb0055482015-01-21 18:18:25 +00001605 const unsigned FractBits = 52;
Matt Arsenault46010932014-06-18 17:05:30 +00001606
1607 // Extract the sign bit.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001608 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001609 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask);
1610
1611 // Extend back to to 64-bits.
Ahmed Bougacha128f8732016-04-26 21:15:30 +00001612 SDValue SignBit64 = DAG.getBuildVector(MVT::v2i32, SL, {Zero, SignBit});
Matt Arsenault46010932014-06-18 17:05:30 +00001613 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64);
1614
1615 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src);
Matt Arsenault2b0fa432014-06-18 22:11:03 +00001616 const SDValue FractMask
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001617 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, SL, MVT::i64);
Matt Arsenault46010932014-06-18 17:05:30 +00001618
1619 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp);
1620 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64);
1621 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not);
1622
Mehdi Amini44ede332015-07-09 02:09:04 +00001623 EVT SetCCVT =
1624 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001625
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001626 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, SL, MVT::i32);
Matt Arsenault46010932014-06-18 17:05:30 +00001627
1628 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1629 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1630
1631 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0);
1632 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1);
1633
1634 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2);
1635}
1636
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001637SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const {
1638 SDLoc SL(Op);
1639 SDValue Src = Op.getOperand(0);
1640
1641 assert(Op.getValueType() == MVT::f64);
1642
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001643 APFloat C1Val(APFloat::IEEEdouble(), "0x1.0p+52");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001644 SDValue C1 = DAG.getConstantFP(C1Val, SL, MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001645 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src);
1646
Sanjay Patela2607012015-09-16 16:31:21 +00001647 // TODO: Should this propagate fast-math-flags?
1648
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001649 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign);
1650 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign);
1651
1652 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src);
Matt Arsenaultd22626f2014-06-18 17:45:58 +00001653
Stephan Bergmann17c7f702016-12-14 11:57:17 +00001654 APFloat C2Val(APFloat::IEEEdouble(), "0x1.fffffffffffffp+51");
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001655 SDValue C2 = DAG.getConstantFP(C2Val, SL, MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001656
Mehdi Amini44ede332015-07-09 02:09:04 +00001657 EVT SetCCVT =
1658 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenaulte8208ec2014-06-18 17:05:26 +00001659 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT);
1660
1661 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2);
1662}
1663
Matt Arsenault692bd5e2014-06-18 22:03:45 +00001664SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const {
1665 // FNEARBYINT and FRINT are the same, except in their handling of FP
1666 // exceptions. Those aren't really meaningful for us, and OpenCL only has
1667 // rint, so just treat them as equivalent.
1668 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0));
1669}
1670
Matt Arsenaultb0055482015-01-21 18:18:25 +00001671// XXX - May require not supporting f32 denormals?
1672SDValue AMDGPUTargetLowering::LowerFROUND32(SDValue Op, SelectionDAG &DAG) const {
1673 SDLoc SL(Op);
1674 SDValue X = Op.getOperand(0);
1675
1676 SDValue T = DAG.getNode(ISD::FTRUNC, SL, MVT::f32, X);
1677
Sanjay Patela2607012015-09-16 16:31:21 +00001678 // TODO: Should this propagate fast-math-flags?
1679
Matt Arsenaultb0055482015-01-21 18:18:25 +00001680 SDValue Diff = DAG.getNode(ISD::FSUB, SL, MVT::f32, X, T);
1681
1682 SDValue AbsDiff = DAG.getNode(ISD::FABS, SL, MVT::f32, Diff);
1683
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001684 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f32);
1685 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
1686 const SDValue Half = DAG.getConstantFP(0.5, SL, MVT::f32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001687
1688 SDValue SignOne = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f32, One, X);
1689
Mehdi Amini44ede332015-07-09 02:09:04 +00001690 EVT SetCCVT =
1691 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001692
1693 SDValue Cmp = DAG.getSetCC(SL, SetCCVT, AbsDiff, Half, ISD::SETOGE);
1694
1695 SDValue Sel = DAG.getNode(ISD::SELECT, SL, MVT::f32, Cmp, SignOne, Zero);
1696
1697 return DAG.getNode(ISD::FADD, SL, MVT::f32, T, Sel);
1698}
1699
1700SDValue AMDGPUTargetLowering::LowerFROUND64(SDValue Op, SelectionDAG &DAG) const {
1701 SDLoc SL(Op);
1702 SDValue X = Op.getOperand(0);
1703
1704 SDValue L = DAG.getNode(ISD::BITCAST, SL, MVT::i64, X);
1705
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001706 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1707 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1708 const SDValue NegOne = DAG.getConstant(-1, SL, MVT::i32);
1709 const SDValue FiftyOne = DAG.getConstant(51, SL, MVT::i32);
Mehdi Amini44ede332015-07-09 02:09:04 +00001710 EVT SetCCVT =
1711 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001712
1713 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1714
1715 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, One);
1716
1717 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
1718
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001719 const SDValue Mask = DAG.getConstant(INT64_C(0x000fffffffffffff), SL,
1720 MVT::i64);
Matt Arsenaultb0055482015-01-21 18:18:25 +00001721
1722 SDValue M = DAG.getNode(ISD::SRA, SL, MVT::i64, Mask, Exp);
1723 SDValue D = DAG.getNode(ISD::SRA, SL, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001724 DAG.getConstant(INT64_C(0x0008000000000000), SL,
1725 MVT::i64),
Matt Arsenaultb0055482015-01-21 18:18:25 +00001726 Exp);
1727
1728 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, L, M);
1729 SDValue Tmp1 = DAG.getSetCC(SL, SetCCVT,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001730 DAG.getConstant(0, SL, MVT::i64), Tmp0,
Matt Arsenaultb0055482015-01-21 18:18:25 +00001731 ISD::SETNE);
1732
1733 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, Tmp1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001734 D, DAG.getConstant(0, SL, MVT::i64));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001735 SDValue K = DAG.getNode(ISD::ADD, SL, MVT::i64, L, Tmp2);
1736
1737 K = DAG.getNode(ISD::AND, SL, MVT::i64, K, DAG.getNOT(SL, M, MVT::i64));
1738 K = DAG.getNode(ISD::BITCAST, SL, MVT::f64, K);
1739
1740 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1741 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1742 SDValue ExpEqNegOne = DAG.getSetCC(SL, SetCCVT, NegOne, Exp, ISD::SETEQ);
1743
1744 SDValue Mag = DAG.getNode(ISD::SELECT, SL, MVT::f64,
1745 ExpEqNegOne,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001746 DAG.getConstantFP(1.0, SL, MVT::f64),
1747 DAG.getConstantFP(0.0, SL, MVT::f64));
Matt Arsenaultb0055482015-01-21 18:18:25 +00001748
1749 SDValue S = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, Mag, X);
1750
1751 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpLt0, S, K);
1752 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpGt51, X, K);
1753
1754 return K;
1755}
1756
1757SDValue AMDGPUTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const {
1758 EVT VT = Op.getValueType();
1759
1760 if (VT == MVT::f32)
1761 return LowerFROUND32(Op, DAG);
1762
1763 if (VT == MVT::f64)
1764 return LowerFROUND64(Op, DAG);
1765
1766 llvm_unreachable("unhandled type");
1767}
1768
Matt Arsenault46010932014-06-18 17:05:30 +00001769SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const {
1770 SDLoc SL(Op);
1771 SDValue Src = Op.getOperand(0);
1772
1773 // result = trunc(src);
1774 // if (src < 0.0 && src != result)
1775 // result += -1.0.
1776
1777 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1778
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001779 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1780 const SDValue NegOne = DAG.getConstantFP(-1.0, SL, MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001781
Mehdi Amini44ede332015-07-09 02:09:04 +00001782 EVT SetCCVT =
1783 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
Matt Arsenault46010932014-06-18 17:05:30 +00001784
1785 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT);
1786 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1787 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1788
1789 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero);
Sanjay Patela2607012015-09-16 16:31:21 +00001790 // TODO: Should this propagate fast-math-flags?
Matt Arsenault46010932014-06-18 17:05:30 +00001791 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1792}
1793
Matt Arsenaultf058d672016-01-11 16:50:29 +00001794SDValue AMDGPUTargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
1795 SDLoc SL(Op);
1796 SDValue Src = Op.getOperand(0);
Matt Arsenaultf058d672016-01-11 16:50:29 +00001797 bool ZeroUndef = Op.getOpcode() == ISD::CTLZ_ZERO_UNDEF;
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00001798
1799 if (ZeroUndef && Src.getValueType() == MVT::i32)
1800 return DAG.getNode(AMDGPUISD::FFBH_U32, SL, MVT::i32, Src);
1801
Matt Arsenaultf058d672016-01-11 16:50:29 +00001802 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1803
1804 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1805 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1806
1807 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1808 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1809
1810 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1811 *DAG.getContext(), MVT::i32);
1812
1813 SDValue Hi0 = DAG.getSetCC(SL, SetCCVT, Hi, Zero, ISD::SETEQ);
1814
1815 SDValue CtlzLo = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Lo);
1816 SDValue CtlzHi = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Hi);
1817
1818 const SDValue Bits32 = DAG.getConstant(32, SL, MVT::i32);
1819 SDValue Add = DAG.getNode(ISD::ADD, SL, MVT::i32, CtlzLo, Bits32);
1820
1821 // ctlz(x) = hi_32(x) == 0 ? ctlz(lo_32(x)) + 32 : ctlz(hi_32(x))
1822 SDValue NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0, Add, CtlzHi);
1823
1824 if (!ZeroUndef) {
1825 // Test if the full 64-bit input is zero.
1826
1827 // FIXME: DAG combines turn what should be an s_and_b64 into a v_or_b32,
1828 // which we probably don't want.
1829 SDValue Lo0 = DAG.getSetCC(SL, SetCCVT, Lo, Zero, ISD::SETEQ);
1830 SDValue SrcIsZero = DAG.getNode(ISD::AND, SL, SetCCVT, Lo0, Hi0);
1831
1832 // TODO: If i64 setcc is half rate, it can result in 1 fewer instruction
1833 // with the same cycles, otherwise it is slower.
1834 // SDValue SrcIsZero = DAG.getSetCC(SL, SetCCVT, Src,
1835 // DAG.getConstant(0, SL, MVT::i64), ISD::SETEQ);
1836
1837 const SDValue Bits32 = DAG.getConstant(64, SL, MVT::i32);
1838
1839 // The instruction returns -1 for 0 input, but the defined intrinsic
1840 // behavior is to return the number of bits.
1841 NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32,
1842 SrcIsZero, Bits32, NewCtlz);
1843 }
1844
1845 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, NewCtlz);
1846}
1847
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001848SDValue AMDGPUTargetLowering::LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG,
1849 bool Signed) const {
1850 // Unsigned
1851 // cul2f(ulong u)
1852 //{
1853 // uint lz = clz(u);
1854 // uint e = (u != 0) ? 127U + 63U - lz : 0;
1855 // u = (u << lz) & 0x7fffffffffffffffUL;
1856 // ulong t = u & 0xffffffffffUL;
1857 // uint v = (e << 23) | (uint)(u >> 40);
1858 // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U);
1859 // return as_float(v + r);
1860 //}
1861 // Signed
1862 // cl2f(long l)
1863 //{
1864 // long s = l >> 63;
1865 // float r = cul2f((l + s) ^ s);
1866 // return s ? -r : r;
1867 //}
1868
1869 SDLoc SL(Op);
1870 SDValue Src = Op.getOperand(0);
1871 SDValue L = Src;
1872
1873 SDValue S;
1874 if (Signed) {
1875 const SDValue SignBit = DAG.getConstant(63, SL, MVT::i64);
1876 S = DAG.getNode(ISD::SRA, SL, MVT::i64, L, SignBit);
1877
1878 SDValue LPlusS = DAG.getNode(ISD::ADD, SL, MVT::i64, L, S);
1879 L = DAG.getNode(ISD::XOR, SL, MVT::i64, LPlusS, S);
1880 }
1881
1882 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1883 *DAG.getContext(), MVT::f32);
1884
1885
1886 SDValue ZeroI32 = DAG.getConstant(0, SL, MVT::i32);
1887 SDValue ZeroI64 = DAG.getConstant(0, SL, MVT::i64);
1888 SDValue LZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i64, L);
1889 LZ = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LZ);
1890
1891 SDValue K = DAG.getConstant(127U + 63U, SL, MVT::i32);
1892 SDValue E = DAG.getSelect(SL, MVT::i32,
1893 DAG.getSetCC(SL, SetCCVT, L, ZeroI64, ISD::SETNE),
1894 DAG.getNode(ISD::SUB, SL, MVT::i32, K, LZ),
1895 ZeroI32);
1896
1897 SDValue U = DAG.getNode(ISD::AND, SL, MVT::i64,
1898 DAG.getNode(ISD::SHL, SL, MVT::i64, L, LZ),
1899 DAG.getConstant((-1ULL) >> 1, SL, MVT::i64));
1900
1901 SDValue T = DAG.getNode(ISD::AND, SL, MVT::i64, U,
1902 DAG.getConstant(0xffffffffffULL, SL, MVT::i64));
1903
1904 SDValue UShl = DAG.getNode(ISD::SRL, SL, MVT::i64,
1905 U, DAG.getConstant(40, SL, MVT::i64));
1906
1907 SDValue V = DAG.getNode(ISD::OR, SL, MVT::i32,
1908 DAG.getNode(ISD::SHL, SL, MVT::i32, E, DAG.getConstant(23, SL, MVT::i32)),
1909 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, UShl));
1910
1911 SDValue C = DAG.getConstant(0x8000000000ULL, SL, MVT::i64);
1912 SDValue RCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETUGT);
1913 SDValue TCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETEQ);
1914
1915 SDValue One = DAG.getConstant(1, SL, MVT::i32);
1916
1917 SDValue VTrunc1 = DAG.getNode(ISD::AND, SL, MVT::i32, V, One);
1918
1919 SDValue R = DAG.getSelect(SL, MVT::i32,
1920 RCmp,
1921 One,
1922 DAG.getSelect(SL, MVT::i32, TCmp, VTrunc1, ZeroI32));
1923 R = DAG.getNode(ISD::ADD, SL, MVT::i32, V, R);
1924 R = DAG.getNode(ISD::BITCAST, SL, MVT::f32, R);
1925
1926 if (!Signed)
1927 return R;
1928
1929 SDValue RNeg = DAG.getNode(ISD::FNEG, SL, MVT::f32, R);
1930 return DAG.getSelect(SL, MVT::f32, DAG.getSExtOrTrunc(S, SL, SetCCVT), RNeg, R);
1931}
1932
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001933SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG,
1934 bool Signed) const {
1935 SDLoc SL(Op);
1936 SDValue Src = Op.getOperand(0);
1937
1938 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1939
1940 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001941 DAG.getConstant(0, SL, MVT::i32));
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001942 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001943 DAG.getConstant(1, SL, MVT::i32));
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001944
1945 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
1946 SL, MVT::f64, Hi);
1947
1948 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo);
1949
1950 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001951 DAG.getConstant(32, SL, MVT::i32));
Sanjay Patela2607012015-09-16 16:31:21 +00001952 // TODO: Should this propagate fast-math-flags?
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001953 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo);
1954}
1955
Tom Stellardc947d8c2013-10-30 17:22:05 +00001956SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
1957 SelectionDAG &DAG) const {
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001958 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
1959 "operation should be legal");
Tom Stellardc947d8c2013-10-30 17:22:05 +00001960
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001961 // TODO: Factor out code common with LowerSINT_TO_FP.
1962
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001963 EVT DestVT = Op.getValueType();
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001964 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
1965 SDLoc DL(Op);
1966 SDValue Src = Op.getOperand(0);
1967
1968 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
1969 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
1970 SDValue FPRound =
1971 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
1972
1973 return FPRound;
1974 }
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001975
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001976 if (DestVT == MVT::f32)
1977 return LowerINT_TO_FP32(Op, DAG, false);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001978
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00001979 assert(DestVT == MVT::f64);
1980 return LowerINT_TO_FP64(Op, DAG, false);
Tom Stellardc947d8c2013-10-30 17:22:05 +00001981}
Tom Stellardfbab8272013-08-16 01:12:11 +00001982
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00001983SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op,
1984 SelectionDAG &DAG) const {
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001985 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
1986 "operation should be legal");
1987
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001988 // TODO: Factor out code common with LowerUINT_TO_FP.
1989
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00001990 EVT DestVT = Op.getValueType();
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00001991 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
1992 SDLoc DL(Op);
1993 SDValue Src = Op.getOperand(0);
1994
1995 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
1996 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
1997 SDValue FPRound =
1998 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
1999
2000 return FPRound;
2001 }
2002
Matt Arsenault5e0bdb82016-01-11 22:01:48 +00002003 if (DestVT == MVT::f32)
2004 return LowerINT_TO_FP32(Op, DAG, true);
2005
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00002006 assert(DestVT == MVT::f64);
2007 return LowerINT_TO_FP64(Op, DAG, true);
Matt Arsenaultf7c95e32014-10-03 23:54:41 +00002008}
2009
Matt Arsenaultc9961752014-10-03 23:54:56 +00002010SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG,
2011 bool Signed) const {
2012 SDLoc SL(Op);
2013
2014 SDValue Src = Op.getOperand(0);
2015
2016 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
2017
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002018 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL,
2019 MVT::f64);
2020 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL,
2021 MVT::f64);
Sanjay Patela2607012015-09-16 16:31:21 +00002022 // TODO: Should this propagate fast-math-flags?
Matt Arsenaultc9961752014-10-03 23:54:56 +00002023 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0);
2024
2025 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul);
2026
2027
2028 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc);
2029
2030 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL,
2031 MVT::i32, FloorMul);
2032 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma);
2033
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002034 SDValue Result = DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi});
Matt Arsenaultc9961752014-10-03 23:54:56 +00002035
2036 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result);
2037}
2038
Tom Stellard94c21bc2016-11-01 16:31:48 +00002039SDValue AMDGPUTargetLowering::LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const {
2040
2041 if (getTargetMachine().Options.UnsafeFPMath) {
2042 // There is a generic expand for FP_TO_FP16 with unsafe fast math.
2043 return SDValue();
2044 }
2045
2046 SDLoc DL(Op);
2047 SDValue N0 = Op.getOperand(0);
Tom Stellard9677b602016-11-01 17:20:03 +00002048 assert (N0.getSimpleValueType() == MVT::f64);
Tom Stellard94c21bc2016-11-01 16:31:48 +00002049
2050 // f64 -> f16 conversion using round-to-nearest-even rounding mode.
2051 const unsigned ExpMask = 0x7ff;
2052 const unsigned ExpBiasf64 = 1023;
2053 const unsigned ExpBiasf16 = 15;
2054 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2055 SDValue One = DAG.getConstant(1, DL, MVT::i32);
2056 SDValue U = DAG.getNode(ISD::BITCAST, DL, MVT::i64, N0);
2057 SDValue UH = DAG.getNode(ISD::SRL, DL, MVT::i64, U,
2058 DAG.getConstant(32, DL, MVT::i64));
2059 UH = DAG.getZExtOrTrunc(UH, DL, MVT::i32);
2060 U = DAG.getZExtOrTrunc(U, DL, MVT::i32);
2061 SDValue E = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2062 DAG.getConstant(20, DL, MVT::i64));
2063 E = DAG.getNode(ISD::AND, DL, MVT::i32, E,
2064 DAG.getConstant(ExpMask, DL, MVT::i32));
2065 // Subtract the fp64 exponent bias (1023) to get the real exponent and
2066 // add the f16 bias (15) to get the biased exponent for the f16 format.
2067 E = DAG.getNode(ISD::ADD, DL, MVT::i32, E,
2068 DAG.getConstant(-ExpBiasf64 + ExpBiasf16, DL, MVT::i32));
2069
2070 SDValue M = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2071 DAG.getConstant(8, DL, MVT::i32));
2072 M = DAG.getNode(ISD::AND, DL, MVT::i32, M,
2073 DAG.getConstant(0xffe, DL, MVT::i32));
2074
2075 SDValue MaskedSig = DAG.getNode(ISD::AND, DL, MVT::i32, UH,
2076 DAG.getConstant(0x1ff, DL, MVT::i32));
2077 MaskedSig = DAG.getNode(ISD::OR, DL, MVT::i32, MaskedSig, U);
2078
2079 SDValue Lo40Set = DAG.getSelectCC(DL, MaskedSig, Zero, Zero, One, ISD::SETEQ);
2080 M = DAG.getNode(ISD::OR, DL, MVT::i32, M, Lo40Set);
2081
2082 // (M != 0 ? 0x0200 : 0) | 0x7c00;
2083 SDValue I = DAG.getNode(ISD::OR, DL, MVT::i32,
2084 DAG.getSelectCC(DL, M, Zero, DAG.getConstant(0x0200, DL, MVT::i32),
2085 Zero, ISD::SETNE), DAG.getConstant(0x7c00, DL, MVT::i32));
2086
2087 // N = M | (E << 12);
2088 SDValue N = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2089 DAG.getNode(ISD::SHL, DL, MVT::i32, E,
2090 DAG.getConstant(12, DL, MVT::i32)));
2091
2092 // B = clamp(1-E, 0, 13);
2093 SDValue OneSubExp = DAG.getNode(ISD::SUB, DL, MVT::i32,
2094 One, E);
2095 SDValue B = DAG.getNode(ISD::SMAX, DL, MVT::i32, OneSubExp, Zero);
2096 B = DAG.getNode(ISD::SMIN, DL, MVT::i32, B,
2097 DAG.getConstant(13, DL, MVT::i32));
2098
2099 SDValue SigSetHigh = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2100 DAG.getConstant(0x1000, DL, MVT::i32));
2101
2102 SDValue D = DAG.getNode(ISD::SRL, DL, MVT::i32, SigSetHigh, B);
2103 SDValue D0 = DAG.getNode(ISD::SHL, DL, MVT::i32, D, B);
2104 SDValue D1 = DAG.getSelectCC(DL, D0, SigSetHigh, One, Zero, ISD::SETNE);
2105 D = DAG.getNode(ISD::OR, DL, MVT::i32, D, D1);
2106
2107 SDValue V = DAG.getSelectCC(DL, E, One, D, N, ISD::SETLT);
2108 SDValue VLow3 = DAG.getNode(ISD::AND, DL, MVT::i32, V,
2109 DAG.getConstant(0x7, DL, MVT::i32));
2110 V = DAG.getNode(ISD::SRL, DL, MVT::i32, V,
2111 DAG.getConstant(2, DL, MVT::i32));
2112 SDValue V0 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(3, DL, MVT::i32),
2113 One, Zero, ISD::SETEQ);
2114 SDValue V1 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(5, DL, MVT::i32),
2115 One, Zero, ISD::SETGT);
2116 V1 = DAG.getNode(ISD::OR, DL, MVT::i32, V0, V1);
2117 V = DAG.getNode(ISD::ADD, DL, MVT::i32, V, V1);
2118
2119 V = DAG.getSelectCC(DL, E, DAG.getConstant(30, DL, MVT::i32),
2120 DAG.getConstant(0x7c00, DL, MVT::i32), V, ISD::SETGT);
2121 V = DAG.getSelectCC(DL, E, DAG.getConstant(1039, DL, MVT::i32),
2122 I, V, ISD::SETEQ);
2123
2124 // Extract the sign bit.
2125 SDValue Sign = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2126 DAG.getConstant(16, DL, MVT::i32));
2127 Sign = DAG.getNode(ISD::AND, DL, MVT::i32, Sign,
2128 DAG.getConstant(0x8000, DL, MVT::i32));
2129
2130 V = DAG.getNode(ISD::OR, DL, MVT::i32, Sign, V);
2131 return DAG.getZExtOrTrunc(V, DL, Op.getValueType());
2132}
2133
Matt Arsenaultc9961752014-10-03 23:54:56 +00002134SDValue AMDGPUTargetLowering::LowerFP_TO_SINT(SDValue Op,
2135 SelectionDAG &DAG) const {
2136 SDValue Src = Op.getOperand(0);
2137
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002138 // TODO: Factor out code common with LowerFP_TO_UINT.
2139
2140 EVT SrcVT = Src.getValueType();
2141 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2142 SDLoc DL(Op);
2143
2144 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2145 SDValue FpToInt32 =
2146 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2147
2148 return FpToInt32;
2149 }
2150
Matt Arsenaultc9961752014-10-03 23:54:56 +00002151 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2152 return LowerFP64_TO_INT(Op, DAG, true);
2153
2154 return SDValue();
2155}
2156
2157SDValue AMDGPUTargetLowering::LowerFP_TO_UINT(SDValue Op,
2158 SelectionDAG &DAG) const {
2159 SDValue Src = Op.getOperand(0);
2160
Konstantin Zhuravlyovf86e4b72016-11-13 07:01:11 +00002161 // TODO: Factor out code common with LowerFP_TO_SINT.
2162
2163 EVT SrcVT = Src.getValueType();
2164 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2165 SDLoc DL(Op);
2166
2167 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2168 SDValue FpToInt32 =
2169 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2170
2171 return FpToInt32;
2172 }
2173
Matt Arsenaultc9961752014-10-03 23:54:56 +00002174 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2175 return LowerFP64_TO_INT(Op, DAG, false);
2176
2177 return SDValue();
2178}
2179
Matt Arsenaultfae02982014-03-17 18:58:11 +00002180SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
2181 SelectionDAG &DAG) const {
2182 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2183 MVT VT = Op.getSimpleValueType();
2184 MVT ScalarVT = VT.getScalarType();
2185
Matt Arsenaultedc7dcb2016-07-28 00:32:05 +00002186 assert(VT.isVector());
Matt Arsenaultfae02982014-03-17 18:58:11 +00002187
2188 SDValue Src = Op.getOperand(0);
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002189 SDLoc DL(Op);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002190
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002191 // TODO: Don't scalarize on Evergreen?
2192 unsigned NElts = VT.getVectorNumElements();
2193 SmallVector<SDValue, 8> Args;
2194 DAG.ExtractVectorElements(Src, Args, 0, NElts);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002195
Matt Arsenault5dbd5db2014-04-22 03:49:30 +00002196 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType());
2197 for (unsigned I = 0; I < NElts; ++I)
2198 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002199
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002200 return DAG.getBuildVector(VT, DL, Args);
Matt Arsenaultfae02982014-03-17 18:58:11 +00002201}
2202
Tom Stellard75aadc22012-12-11 21:25:42 +00002203//===----------------------------------------------------------------------===//
Tom Stellard50122a52014-04-07 19:45:41 +00002204// Custom DAG optimizations
2205//===----------------------------------------------------------------------===//
2206
2207static bool isU24(SDValue Op, SelectionDAG &DAG) {
2208 APInt KnownZero, KnownOne;
2209 EVT VT = Op.getValueType();
Jay Foada0653a32014-05-14 21:14:37 +00002210 DAG.computeKnownBits(Op, KnownZero, KnownOne);
Tom Stellard50122a52014-04-07 19:45:41 +00002211
2212 return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24;
2213}
2214
2215static bool isI24(SDValue Op, SelectionDAG &DAG) {
2216 EVT VT = Op.getValueType();
2217
2218 // In order for this to be a signed 24-bit value, bit 23, must
2219 // be a sign bit.
2220 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
2221 // as unsigned 24-bit values.
2222 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24;
2223}
2224
Tom Stellard09c2bd62016-10-14 19:14:29 +00002225static bool simplifyI24(SDNode *Node24, unsigned OpIdx,
2226 TargetLowering::DAGCombinerInfo &DCI) {
Tom Stellard50122a52014-04-07 19:45:41 +00002227
2228 SelectionDAG &DAG = DCI.DAG;
Tom Stellard09c2bd62016-10-14 19:14:29 +00002229 SDValue Op = Node24->getOperand(OpIdx);
Tom Stellard50122a52014-04-07 19:45:41 +00002230 EVT VT = Op.getValueType();
2231
2232 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24);
2233 APInt KnownZero, KnownOne;
2234 TargetLowering::TargetLoweringOpt TLO(DAG, true, true);
Tom Stellard09c2bd62016-10-14 19:14:29 +00002235 if (TLO.SimplifyDemandedBits(Node24, OpIdx, Demanded, DCI))
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002236 return true;
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002237
2238 return false;
Tom Stellard50122a52014-04-07 19:45:41 +00002239}
2240
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002241template <typename IntTy>
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002242static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, uint32_t Offset,
2243 uint32_t Width, const SDLoc &DL) {
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002244 if (Width + Offset < 32) {
Matt Arsenault46cbc432014-09-19 00:42:06 +00002245 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width);
2246 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002247 return DAG.getConstant(Result, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002248 }
2249
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002250 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00002251}
2252
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002253static bool hasVolatileUser(SDNode *Val) {
2254 for (SDNode *U : Val->uses()) {
2255 if (MemSDNode *M = dyn_cast<MemSDNode>(U)) {
2256 if (M->isVolatile())
2257 return true;
2258 }
2259 }
2260
2261 return false;
2262}
2263
Matt Arsenault8af47a02016-07-01 22:55:55 +00002264bool AMDGPUTargetLowering::shouldCombineMemoryType(EVT VT) const {
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002265 // i32 vectors are the canonical memory type.
2266 if (VT.getScalarType() == MVT::i32 || isTypeLegal(VT))
2267 return false;
2268
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002269 if (!VT.isByteSized())
2270 return false;
2271
2272 unsigned Size = VT.getStoreSize();
2273
2274 if ((Size == 1 || Size == 2 || Size == 4) && !VT.isVector())
2275 return false;
2276
2277 if (Size == 3 || (Size > 4 && (Size % 4 != 0)))
2278 return false;
2279
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002280 return true;
2281}
2282
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002283// Replace load of an illegal type with a store of a bitcast to a friendlier
2284// type.
2285SDValue AMDGPUTargetLowering::performLoadCombine(SDNode *N,
2286 DAGCombinerInfo &DCI) const {
2287 if (!DCI.isBeforeLegalize())
2288 return SDValue();
2289
2290 LoadSDNode *LN = cast<LoadSDNode>(N);
2291 if (LN->isVolatile() || !ISD::isNormalLoad(LN) || hasVolatileUser(LN))
2292 return SDValue();
2293
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002294 SDLoc SL(N);
2295 SelectionDAG &DAG = DCI.DAG;
2296 EVT VT = LN->getMemoryVT();
Matt Arsenault8af47a02016-07-01 22:55:55 +00002297
2298 unsigned Size = VT.getStoreSize();
2299 unsigned Align = LN->getAlignment();
2300 if (Align < Size && isTypeLegal(VT)) {
2301 bool IsFast;
2302 unsigned AS = LN->getAddressSpace();
2303
2304 // Expand unaligned loads earlier than legalization. Due to visitation order
2305 // problems during legalization, the emitted instructions to pack and unpack
2306 // the bytes again are not eliminated in the case of an unaligned copy.
2307 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002308 if (VT.isVector())
2309 return scalarizeVectorLoad(LN, DAG);
2310
Matt Arsenault8af47a02016-07-01 22:55:55 +00002311 SDValue Ops[2];
2312 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(LN, DAG);
2313 return DAG.getMergeValues(Ops, SDLoc(N));
2314 }
2315
2316 if (!IsFast)
2317 return SDValue();
2318 }
2319
2320 if (!shouldCombineMemoryType(VT))
2321 return SDValue();
2322
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002323 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
2324
2325 SDValue NewLoad
2326 = DAG.getLoad(NewVT, SL, LN->getChain(),
2327 LN->getBasePtr(), LN->getMemOperand());
2328
2329 SDValue BC = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad);
2330 DCI.CombineTo(N, BC, NewLoad.getValue(1));
2331 return SDValue(N, 0);
2332}
2333
2334// Replace store of an illegal type with a store of a bitcast to a friendlier
2335// type.
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002336SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N,
2337 DAGCombinerInfo &DCI) const {
2338 if (!DCI.isBeforeLegalize())
2339 return SDValue();
2340
2341 StoreSDNode *SN = cast<StoreSDNode>(N);
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002342 if (SN->isVolatile() || !ISD::isNormalStore(SN))
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002343 return SDValue();
2344
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002345 EVT VT = SN->getMemoryVT();
Matt Arsenault8af47a02016-07-01 22:55:55 +00002346 unsigned Size = VT.getStoreSize();
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002347
2348 SDLoc SL(N);
2349 SelectionDAG &DAG = DCI.DAG;
Matt Arsenault8af47a02016-07-01 22:55:55 +00002350 unsigned Align = SN->getAlignment();
2351 if (Align < Size && isTypeLegal(VT)) {
2352 bool IsFast;
2353 unsigned AS = SN->getAddressSpace();
2354
2355 // Expand unaligned stores earlier than legalization. Due to visitation
2356 // order problems during legalization, the emitted instructions to pack and
2357 // unpack the bytes again are not eliminated in the case of an unaligned
2358 // copy.
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002359 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
2360 if (VT.isVector())
2361 return scalarizeVectorStore(SN, DAG);
2362
Matt Arsenault8af47a02016-07-01 22:55:55 +00002363 return expandUnalignedStore(SN, DAG);
Matt Arsenaultb50eb8d2016-08-31 21:52:27 +00002364 }
Matt Arsenault8af47a02016-07-01 22:55:55 +00002365
2366 if (!IsFast)
2367 return SDValue();
2368 }
2369
2370 if (!shouldCombineMemoryType(VT))
2371 return SDValue();
2372
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002373 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
Matt Arsenault8af47a02016-07-01 22:55:55 +00002374 SDValue Val = SN->getValue();
2375
2376 //DCI.AddToWorklist(Val.getNode());
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002377
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002378 bool OtherUses = !Val.hasOneUse();
2379 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NewVT, Val);
2380 if (OtherUses) {
2381 SDValue CastBack = DAG.getNode(ISD::BITCAST, SL, VT, CastVal);
2382 DAG.ReplaceAllUsesOfValueWith(Val, CastBack);
2383 }
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002384
Matt Arsenault327bb5a2016-07-01 22:47:50 +00002385 return DAG.getStore(SN->getChain(), SL, CastVal,
Matt Arsenaultca3976f2014-07-15 02:06:31 +00002386 SN->getBasePtr(), SN->getMemOperand());
2387}
2388
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002389/// Split the 64-bit value \p LHS into two 32-bit components, and perform the
2390/// binary operation \p Opc to it with the corresponding constant operands.
2391SDValue AMDGPUTargetLowering::splitBinaryBitConstantOpImpl(
2392 DAGCombinerInfo &DCI, const SDLoc &SL,
2393 unsigned Opc, SDValue LHS,
2394 uint32_t ValLo, uint32_t ValHi) const {
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002395 SelectionDAG &DAG = DCI.DAG;
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002396 SDValue Lo, Hi;
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002397 std::tie(Lo, Hi) = split64BitValue(LHS, DAG);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002398
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002399 SDValue LoRHS = DAG.getConstant(ValLo, SL, MVT::i32);
2400 SDValue HiRHS = DAG.getConstant(ValHi, SL, MVT::i32);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002401
Matt Arsenaultfa5f7672016-09-14 15:19:03 +00002402 SDValue LoAnd = DAG.getNode(Opc, SL, MVT::i32, Lo, LoRHS);
2403 SDValue HiAnd = DAG.getNode(Opc, SL, MVT::i32, Hi, HiRHS);
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002404
Matt Arsenaultefa3fe12016-04-22 22:48:38 +00002405 // Re-visit the ands. It's possible we eliminated one of them and it could
2406 // simplify the vector.
2407 DCI.AddToWorklist(Lo.getNode());
2408 DCI.AddToWorklist(Hi.getNode());
2409
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002410 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd});
Matt Arsenault6e3a4512016-01-18 22:01:13 +00002411 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2412}
2413
Matt Arsenault24692112015-07-14 18:20:33 +00002414SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N,
2415 DAGCombinerInfo &DCI) const {
2416 if (N->getValueType(0) != MVT::i64)
2417 return SDValue();
2418
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002419 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32))
Matt Arsenault24692112015-07-14 18:20:33 +00002420
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002421 // On some subtargets, 64-bit shift is a quarter rate instruction. In the
2422 // common case, splitting this into a move and a 32-bit shift is faster and
2423 // the same code size.
Matt Arsenault24692112015-07-14 18:20:33 +00002424 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002425 if (!RHS)
2426 return SDValue();
2427
2428 unsigned RHSVal = RHS->getZExtValue();
2429 if (RHSVal < 32)
Matt Arsenault24692112015-07-14 18:20:33 +00002430 return SDValue();
2431
2432 SDValue LHS = N->getOperand(0);
2433
2434 SDLoc SL(N);
2435 SelectionDAG &DAG = DCI.DAG;
2436
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002437 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32);
2438
Matt Arsenault24692112015-07-14 18:20:33 +00002439 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS);
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002440 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt);
Matt Arsenault24692112015-07-14 18:20:33 +00002441
2442 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
Matt Arsenault80edab92016-01-18 21:43:36 +00002443
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002444 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift});
Matt Arsenault3cbbc102016-01-18 21:55:14 +00002445 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
Matt Arsenault24692112015-07-14 18:20:33 +00002446}
2447
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002448SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N,
2449 DAGCombinerInfo &DCI) const {
2450 if (N->getValueType(0) != MVT::i64)
2451 return SDValue();
2452
2453 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2454 if (!RHS)
2455 return SDValue();
2456
2457 SelectionDAG &DAG = DCI.DAG;
2458 SDLoc SL(N);
2459 unsigned RHSVal = RHS->getZExtValue();
2460
2461 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31)
2462 if (RHSVal == 32) {
2463 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2464 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2465 DAG.getConstant(31, SL, MVT::i32));
2466
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002467 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift});
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002468 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2469 }
2470
2471 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31)
2472 if (RHSVal == 63) {
2473 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2474 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2475 DAG.getConstant(31, SL, MVT::i32));
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002476 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift});
Matt Arsenault33e3ece2016-01-18 22:09:04 +00002477 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2478 }
2479
2480 return SDValue();
2481}
2482
Matt Arsenault80edab92016-01-18 21:43:36 +00002483SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N,
2484 DAGCombinerInfo &DCI) const {
2485 if (N->getValueType(0) != MVT::i64)
2486 return SDValue();
2487
2488 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2489 if (!RHS)
2490 return SDValue();
2491
2492 unsigned ShiftAmt = RHS->getZExtValue();
2493 if (ShiftAmt < 32)
2494 return SDValue();
2495
2496 // srl i64:x, C for C >= 32
2497 // =>
2498 // build_pair (srl hi_32(x), C - 32), 0
2499
2500 SelectionDAG &DAG = DCI.DAG;
2501 SDLoc SL(N);
2502
2503 SDValue One = DAG.getConstant(1, SL, MVT::i32);
2504 SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
2505
2506 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, N->getOperand(0));
2507 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32,
2508 VecOp, One);
2509
2510 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32);
2511 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst);
2512
Ahmed Bougacha128f8732016-04-26 21:15:30 +00002513 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero});
Matt Arsenault80edab92016-01-18 21:43:36 +00002514
2515 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair);
2516}
2517
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002518// We need to specifically handle i64 mul here to avoid unnecessary conversion
2519// instructions. If we only match on the legalized i64 mul expansion,
2520// SimplifyDemandedBits will be unable to remove them because there will be
2521// multiple uses due to the separate mul + mulh[su].
2522static SDValue getMul24(SelectionDAG &DAG, const SDLoc &SL,
2523 SDValue N0, SDValue N1, unsigned Size, bool Signed) {
2524 if (Size <= 32) {
2525 unsigned MulOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2526 return DAG.getNode(MulOpc, SL, MVT::i32, N0, N1);
2527 }
2528
2529 // Because we want to eliminate extension instructions before the
2530 // operation, we need to create a single user here (i.e. not the separate
2531 // mul_lo + mul_hi) so that SimplifyDemandedBits will deal with it.
2532
2533 unsigned MulOpc = Signed ? AMDGPUISD::MUL_LOHI_I24 : AMDGPUISD::MUL_LOHI_U24;
2534
2535 SDValue Mul = DAG.getNode(MulOpc, SL,
2536 DAG.getVTList(MVT::i32, MVT::i32), N0, N1);
2537
2538 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64,
2539 Mul.getValue(0), Mul.getValue(1));
2540}
2541
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002542SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N,
2543 DAGCombinerInfo &DCI) const {
2544 EVT VT = N->getValueType(0);
2545
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002546 unsigned Size = VT.getSizeInBits();
2547 if (VT.isVector() || Size > 64)
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002548 return SDValue();
2549
Tom Stellard115a6152016-11-10 16:02:37 +00002550 // There are i16 integer mul/mad.
2551 if (Subtarget->has16BitInsts() && VT.getScalarType().bitsLE(MVT::i16))
2552 return SDValue();
2553
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002554 SelectionDAG &DAG = DCI.DAG;
2555 SDLoc DL(N);
2556
2557 SDValue N0 = N->getOperand(0);
2558 SDValue N1 = N->getOperand(1);
2559 SDValue Mul;
2560
2561 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) {
2562 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2563 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002564 Mul = getMul24(DAG, DL, N0, N1, Size, false);
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002565 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) {
2566 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2567 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002568 Mul = getMul24(DAG, DL, N0, N1, Size, true);
Matt Arsenaultd0e0f0a2014-06-30 17:55:48 +00002569 } else {
2570 return SDValue();
2571 }
2572
2573 // We need to use sext even for MUL_U24, because MUL_U24 is used
2574 // for signed multiply of 8 and 16-bit types.
2575 return DAG.getSExtOrTrunc(Mul, DL, VT);
2576}
2577
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002578SDValue AMDGPUTargetLowering::performMulhsCombine(SDNode *N,
2579 DAGCombinerInfo &DCI) const {
2580 EVT VT = N->getValueType(0);
2581
2582 if (!Subtarget->hasMulI24() || VT.isVector())
2583 return SDValue();
2584
2585 SelectionDAG &DAG = DCI.DAG;
2586 SDLoc DL(N);
2587
2588 SDValue N0 = N->getOperand(0);
2589 SDValue N1 = N->getOperand(1);
2590
2591 if (!isI24(N0, DAG) || !isI24(N1, DAG))
2592 return SDValue();
2593
2594 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2595 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
2596
2597 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_I24, DL, MVT::i32, N0, N1);
2598 DCI.AddToWorklist(Mulhi.getNode());
2599 return DAG.getSExtOrTrunc(Mulhi, DL, VT);
2600}
2601
2602SDValue AMDGPUTargetLowering::performMulhuCombine(SDNode *N,
2603 DAGCombinerInfo &DCI) const {
2604 EVT VT = N->getValueType(0);
2605
2606 if (!Subtarget->hasMulU24() || VT.isVector() || VT.getSizeInBits() > 32)
2607 return SDValue();
2608
2609 SelectionDAG &DAG = DCI.DAG;
2610 SDLoc DL(N);
2611
2612 SDValue N0 = N->getOperand(0);
2613 SDValue N1 = N->getOperand(1);
2614
2615 if (!isU24(N0, DAG) || !isU24(N1, DAG))
2616 return SDValue();
2617
2618 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2619 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
2620
2621 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_U24, DL, MVT::i32, N0, N1);
2622 DCI.AddToWorklist(Mulhi.getNode());
2623 return DAG.getZExtOrTrunc(Mulhi, DL, VT);
2624}
2625
2626SDValue AMDGPUTargetLowering::performMulLoHi24Combine(
2627 SDNode *N, DAGCombinerInfo &DCI) const {
2628 SelectionDAG &DAG = DCI.DAG;
2629
Tom Stellard09c2bd62016-10-14 19:14:29 +00002630 // Simplify demanded bits before splitting into multiple users.
2631 if (simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI))
2632 return SDValue();
2633
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002634 SDValue N0 = N->getOperand(0);
2635 SDValue N1 = N->getOperand(1);
2636
Matt Arsenault2712d4a2016-08-27 01:32:27 +00002637 bool Signed = (N->getOpcode() == AMDGPUISD::MUL_LOHI_I24);
2638
2639 unsigned MulLoOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2640 unsigned MulHiOpc = Signed ? AMDGPUISD::MULHI_I24 : AMDGPUISD::MULHI_U24;
2641
2642 SDLoc SL(N);
2643
2644 SDValue MulLo = DAG.getNode(MulLoOpc, SL, MVT::i32, N0, N1);
2645 SDValue MulHi = DAG.getNode(MulHiOpc, SL, MVT::i32, N0, N1);
2646 return DAG.getMergeValues({ MulLo, MulHi }, SL);
2647}
2648
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002649static bool isNegativeOne(SDValue Val) {
2650 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val))
2651 return C->isAllOnesValue();
2652 return false;
2653}
2654
2655static bool isCtlzOpc(unsigned Opc) {
2656 return Opc == ISD::CTLZ || Opc == ISD::CTLZ_ZERO_UNDEF;
2657}
2658
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002659SDValue AMDGPUTargetLowering::getFFBH_U32(SelectionDAG &DAG,
2660 SDValue Op,
2661 const SDLoc &DL) const {
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002662 EVT VT = Op.getValueType();
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002663 EVT LegalVT = getTypeToTransformTo(*DAG.getContext(), VT);
2664 if (LegalVT != MVT::i32 && (Subtarget->has16BitInsts() &&
2665 LegalVT != MVT::i16))
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002666 return SDValue();
2667
2668 if (VT != MVT::i32)
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002669 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Op);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002670
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002671 SDValue FFBH = DAG.getNode(AMDGPUISD::FFBH_U32, DL, MVT::i32, Op);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002672 if (VT != MVT::i32)
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002673 FFBH = DAG.getNode(ISD::TRUNCATE, DL, VT, FFBH);
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002674
2675 return FFBH;
2676}
2677
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002678// The native instructions return -1 on 0 input. Optimize out a select that
2679// produces -1 on 0.
2680//
2681// TODO: If zero is not undef, we could also do this if the output is compared
2682// against the bitwidth.
2683//
2684// TODO: Should probably combine against FFBH_U32 instead of ctlz directly.
Benjamin Kramerbdc49562016-06-12 15:39:02 +00002685SDValue AMDGPUTargetLowering::performCtlzCombine(const SDLoc &SL, SDValue Cond,
2686 SDValue LHS, SDValue RHS,
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002687 DAGCombinerInfo &DCI) const {
2688 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2689 if (!CmpRhs || !CmpRhs->isNullValue())
2690 return SDValue();
2691
2692 SelectionDAG &DAG = DCI.DAG;
2693 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
2694 SDValue CmpLHS = Cond.getOperand(0);
2695
2696 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x
2697 if (CCOpcode == ISD::SETEQ &&
2698 isCtlzOpc(RHS.getOpcode()) &&
2699 RHS.getOperand(0) == CmpLHS &&
2700 isNegativeOne(LHS)) {
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002701 return getFFBH_U32(DAG, CmpLHS, SL);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002702 }
2703
2704 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x
2705 if (CCOpcode == ISD::SETNE &&
2706 isCtlzOpc(LHS.getOpcode()) &&
2707 LHS.getOperand(0) == CmpLHS &&
2708 isNegativeOne(RHS)) {
Konstantin Zhuravlyovd971a112016-11-01 17:49:33 +00002709 return getFFBH_U32(DAG, CmpLHS, SL);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002710 }
2711
2712 return SDValue();
2713}
2714
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002715static SDValue distributeOpThroughSelect(TargetLowering::DAGCombinerInfo &DCI,
2716 unsigned Op,
2717 const SDLoc &SL,
2718 SDValue Cond,
2719 SDValue N1,
2720 SDValue N2) {
2721 SelectionDAG &DAG = DCI.DAG;
2722 EVT VT = N1.getValueType();
2723
2724 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, Cond,
2725 N1.getOperand(0), N2.getOperand(0));
2726 DCI.AddToWorklist(NewSelect.getNode());
2727 return DAG.getNode(Op, SL, VT, NewSelect);
2728}
2729
2730// Pull a free FP operation out of a select so it may fold into uses.
2731//
2732// select c, (fneg x), (fneg y) -> fneg (select c, x, y)
2733// select c, (fneg x), k -> fneg (select c, x, (fneg k))
2734//
2735// select c, (fabs x), (fabs y) -> fabs (select c, x, y)
2736// select c, (fabs x), +k -> fabs (select c, x, k)
2737static SDValue foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI,
2738 SDValue N) {
2739 SelectionDAG &DAG = DCI.DAG;
2740 SDValue Cond = N.getOperand(0);
2741 SDValue LHS = N.getOperand(1);
2742 SDValue RHS = N.getOperand(2);
2743
2744 EVT VT = N.getValueType();
2745 if ((LHS.getOpcode() == ISD::FABS && RHS.getOpcode() == ISD::FABS) ||
2746 (LHS.getOpcode() == ISD::FNEG && RHS.getOpcode() == ISD::FNEG)) {
2747 return distributeOpThroughSelect(DCI, LHS.getOpcode(),
2748 SDLoc(N), Cond, LHS, RHS);
2749 }
2750
2751 bool Inv = false;
2752 if (RHS.getOpcode() == ISD::FABS || RHS.getOpcode() == ISD::FNEG) {
2753 std::swap(LHS, RHS);
2754 Inv = true;
2755 }
2756
2757 // TODO: Support vector constants.
2758 ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2759 if ((LHS.getOpcode() == ISD::FNEG || LHS.getOpcode() == ISD::FABS) && CRHS) {
2760 SDLoc SL(N);
2761 // If one side is an fneg/fabs and the other is a constant, we can push the
2762 // fneg/fabs down. If it's an fabs, the constant needs to be non-negative.
2763 SDValue NewLHS = LHS.getOperand(0);
2764 SDValue NewRHS = RHS;
2765
Matt Arsenault45337df2017-01-12 18:58:15 +00002766 // Careful: if the neg can be folded up, don't try to pull it back down.
2767 bool ShouldFoldNeg = true;
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002768
Matt Arsenault45337df2017-01-12 18:58:15 +00002769 if (NewLHS.hasOneUse()) {
2770 unsigned Opc = NewLHS.getOpcode();
2771 if (LHS.getOpcode() == ISD::FNEG && fnegFoldsIntoOp(Opc))
2772 ShouldFoldNeg = false;
2773 if (LHS.getOpcode() == ISD::FABS && Opc == ISD::FMUL)
2774 ShouldFoldNeg = false;
2775 }
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002776
Matt Arsenault45337df2017-01-12 18:58:15 +00002777 if (ShouldFoldNeg) {
2778 if (LHS.getOpcode() == ISD::FNEG)
2779 NewRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2780 else if (CRHS->isNegative())
2781 return SDValue();
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002782
Matt Arsenault45337df2017-01-12 18:58:15 +00002783 if (Inv)
2784 std::swap(NewLHS, NewRHS);
2785
2786 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT,
2787 Cond, NewLHS, NewRHS);
2788 DCI.AddToWorklist(NewSelect.getNode());
2789 return DAG.getNode(LHS.getOpcode(), SL, VT, NewSelect);
2790 }
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002791 }
2792
2793 return SDValue();
2794}
2795
2796
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002797SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N,
2798 DAGCombinerInfo &DCI) const {
Matt Arsenault2a04ff92017-01-11 23:57:38 +00002799 if (SDValue Folded = foldFreeOpFromSelect(DCI, SDValue(N, 0)))
2800 return Folded;
2801
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002802 SDValue Cond = N->getOperand(0);
2803 if (Cond.getOpcode() != ISD::SETCC)
2804 return SDValue();
2805
2806 EVT VT = N->getValueType(0);
2807 SDValue LHS = Cond.getOperand(0);
2808 SDValue RHS = Cond.getOperand(1);
2809 SDValue CC = Cond.getOperand(2);
2810
2811 SDValue True = N->getOperand(1);
2812 SDValue False = N->getOperand(2);
2813
Matt Arsenault0b26e472016-12-22 21:40:08 +00002814 if (Cond.hasOneUse()) { // TODO: Look for multiple select uses.
2815 SelectionDAG &DAG = DCI.DAG;
2816 if ((DAG.isConstantValueOfAnyType(True) ||
2817 DAG.isConstantValueOfAnyType(True)) &&
2818 (!DAG.isConstantValueOfAnyType(False) &&
2819 !DAG.isConstantValueOfAnyType(False))) {
2820 // Swap cmp + select pair to move constant to false input.
2821 // This will allow using VOPC cndmasks more often.
2822 // select (setcc x, y), k, x -> select (setcc y, x) x, x
2823
2824 SDLoc SL(N);
2825 ISD::CondCode NewCC = getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2826 LHS.getValueType().isInteger());
2827
2828 SDValue NewCond = DAG.getSetCC(SL, Cond.getValueType(), LHS, RHS, NewCC);
2829 return DAG.getNode(ISD::SELECT, SL, VT, NewCond, False, True);
2830 }
Matt Arsenault0b26e472016-12-22 21:40:08 +00002831
Matt Arsenaultda7a6562017-02-01 00:42:40 +00002832 if (VT == MVT::f32 && Subtarget->hasFminFmaxLegacy()) {
2833 SDValue MinMax
2834 = combineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI);
2835 // Revisit this node so we can catch min3/max3/med3 patterns.
2836 //DCI.AddToWorklist(MinMax.getNode());
2837 return MinMax;
2838 }
Matt Arsenault5b39b342016-01-28 20:53:48 +00002839 }
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002840
2841 // There's no reason to not do this if the condition has other uses.
Matt Arsenault5319b0a2016-01-11 17:02:06 +00002842 return performCtlzCombine(SDLoc(N), Cond, True, False, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00002843}
2844
Matt Arsenault2529fba2017-01-12 00:09:34 +00002845SDValue AMDGPUTargetLowering::performFNegCombine(SDNode *N,
2846 DAGCombinerInfo &DCI) const {
2847 SelectionDAG &DAG = DCI.DAG;
2848 SDValue N0 = N->getOperand(0);
2849 EVT VT = N->getValueType(0);
2850
2851 unsigned Opc = N0.getOpcode();
2852
2853 // If the input has multiple uses and we can either fold the negate down, or
2854 // the other uses cannot, give up. This both prevents unprofitable
2855 // transformations and infinite loops: we won't repeatedly try to fold around
2856 // a negate that has no 'good' form.
2857 //
2858 // TODO: Check users can fold
2859 if (fnegFoldsIntoOp(Opc) && !N0.hasOneUse())
2860 return SDValue();
2861
2862 SDLoc SL(N);
2863 switch (Opc) {
2864 case ISD::FADD: {
Matt Arsenault3e6f9b52017-01-19 06:35:27 +00002865 if (!mayIgnoreSignedZero(N0))
2866 return SDValue();
2867
Matt Arsenault2529fba2017-01-12 00:09:34 +00002868 // (fneg (fadd x, y)) -> (fadd (fneg x), (fneg y))
2869 SDValue LHS = N0.getOperand(0);
2870 SDValue RHS = N0.getOperand(1);
2871
2872 if (LHS.getOpcode() != ISD::FNEG)
2873 LHS = DAG.getNode(ISD::FNEG, SL, VT, LHS);
2874 else
2875 LHS = LHS.getOperand(0);
2876
2877 if (RHS.getOpcode() != ISD::FNEG)
2878 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2879 else
2880 RHS = RHS.getOperand(0);
2881
Matt Arsenault7b49ad72017-01-23 19:08:34 +00002882 SDValue Res = DAG.getNode(ISD::FADD, SL, VT, LHS, RHS, N0->getFlags());
Matt Arsenault2529fba2017-01-12 00:09:34 +00002883 if (!N0.hasOneUse())
2884 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
2885 return Res;
2886 }
Matt Arsenaulta8c325e2017-01-12 18:26:30 +00002887 case ISD::FMUL:
2888 case AMDGPUISD::FMUL_LEGACY: {
Matt Arsenault4103a812017-01-12 00:23:20 +00002889 // (fneg (fmul x, y)) -> (fmul x, (fneg y))
Matt Arsenaulta8c325e2017-01-12 18:26:30 +00002890 // (fneg (fmul_legacy x, y)) -> (fmul_legacy x, (fneg y))
Matt Arsenault4103a812017-01-12 00:23:20 +00002891 SDValue LHS = N0.getOperand(0);
2892 SDValue RHS = N0.getOperand(1);
2893
2894 if (LHS.getOpcode() == ISD::FNEG)
2895 LHS = LHS.getOperand(0);
2896 else if (RHS.getOpcode() == ISD::FNEG)
2897 RHS = RHS.getOperand(0);
2898 else
2899 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2900
Matt Arsenault7b49ad72017-01-23 19:08:34 +00002901 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, RHS, N0->getFlags());
Matt Arsenault4103a812017-01-12 00:23:20 +00002902 if (!N0.hasOneUse())
2903 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
2904 return Res;
2905 }
Matt Arsenault63f95372017-01-12 00:32:16 +00002906 case ISD::FMA:
2907 case ISD::FMAD: {
Matt Arsenault3e6f9b52017-01-19 06:35:27 +00002908 if (!mayIgnoreSignedZero(N0))
2909 return SDValue();
2910
Matt Arsenault63f95372017-01-12 00:32:16 +00002911 // (fneg (fma x, y, z)) -> (fma x, (fneg y), (fneg z))
2912 SDValue LHS = N0.getOperand(0);
2913 SDValue MHS = N0.getOperand(1);
2914 SDValue RHS = N0.getOperand(2);
2915
2916 if (LHS.getOpcode() == ISD::FNEG)
2917 LHS = LHS.getOperand(0);
2918 else if (MHS.getOpcode() == ISD::FNEG)
2919 MHS = MHS.getOperand(0);
2920 else
2921 MHS = DAG.getNode(ISD::FNEG, SL, VT, MHS);
2922
2923 if (RHS.getOpcode() != ISD::FNEG)
2924 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2925 else
2926 RHS = RHS.getOperand(0);
2927
2928 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, MHS, RHS);
2929 if (!N0.hasOneUse())
2930 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
2931 return Res;
2932 }
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00002933 case ISD::FP_EXTEND:
Matt Arsenault53f0cc22017-01-26 01:25:36 +00002934 case ISD::FTRUNC:
2935 case ISD::FRINT:
2936 case ISD::FNEARBYINT: // XXX - Should fround be handled?
2937 case ISD::FSIN:
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00002938 case AMDGPUISD::RCP:
Matt Arsenault31c039e2017-01-12 18:48:09 +00002939 case AMDGPUISD::RCP_LEGACY:
Matt Arsenault31c039e2017-01-12 18:48:09 +00002940 case AMDGPUISD::SIN_HW: {
Matt Arsenault98d2bf102017-01-12 17:46:28 +00002941 SDValue CvtSrc = N0.getOperand(0);
2942 if (CvtSrc.getOpcode() == ISD::FNEG) {
2943 // (fneg (fp_extend (fneg x))) -> (fp_extend x)
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00002944 // (fneg (rcp (fneg x))) -> (rcp x)
Matt Arsenault4242d482017-01-12 17:46:33 +00002945 return DAG.getNode(Opc, SL, VT, CvtSrc.getOperand(0));
Matt Arsenault98d2bf102017-01-12 17:46:28 +00002946 }
2947
2948 if (!N0.hasOneUse())
2949 return SDValue();
2950
2951 // (fneg (fp_extend x)) -> (fp_extend (fneg x))
Matt Arsenaultff7e5aa2017-01-12 17:46:35 +00002952 // (fneg (rcp x)) -> (rcp (fneg x))
Matt Arsenault98d2bf102017-01-12 17:46:28 +00002953 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
Matt Arsenault7b49ad72017-01-23 19:08:34 +00002954 return DAG.getNode(Opc, SL, VT, Neg, N0->getFlags());
Matt Arsenault4242d482017-01-12 17:46:33 +00002955 }
2956 case ISD::FP_ROUND: {
2957 SDValue CvtSrc = N0.getOperand(0);
2958
2959 if (CvtSrc.getOpcode() == ISD::FNEG) {
2960 // (fneg (fp_round (fneg x))) -> (fp_round x)
2961 return DAG.getNode(ISD::FP_ROUND, SL, VT,
2962 CvtSrc.getOperand(0), N0.getOperand(1));
2963 }
2964
2965 if (!N0.hasOneUse())
2966 return SDValue();
2967
2968 // (fneg (fp_round x)) -> (fp_round (fneg x))
2969 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
2970 return DAG.getNode(ISD::FP_ROUND, SL, VT, Neg, N0.getOperand(1));
Matt Arsenault98d2bf102017-01-12 17:46:28 +00002971 }
Matt Arsenault9dba9bd2017-02-02 02:27:04 +00002972 case ISD::FP16_TO_FP: {
2973 // v_cvt_f32_f16 supports source modifiers on pre-VI targets without legal
2974 // f16, but legalization of f16 fneg ends up pulling it out of the source.
2975 // Put the fneg back as a legal source operation that can be matched later.
2976 SDLoc SL(N);
2977
2978 SDValue Src = N0.getOperand(0);
2979 EVT SrcVT = Src.getValueType();
2980
2981 // fneg (fp16_to_fp x) -> fp16_to_fp (xor x, 0x8000)
2982 SDValue IntFNeg = DAG.getNode(ISD::XOR, SL, SrcVT, Src,
2983 DAG.getConstant(0x8000, SL, SrcVT));
2984 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFNeg);
2985 }
2986 default:
2987 return SDValue();
2988 }
2989}
2990
2991SDValue AMDGPUTargetLowering::performFAbsCombine(SDNode *N,
2992 DAGCombinerInfo &DCI) const {
2993 SelectionDAG &DAG = DCI.DAG;
2994 SDValue N0 = N->getOperand(0);
2995
2996 if (!N0.hasOneUse())
2997 return SDValue();
2998
2999 switch (N0.getOpcode()) {
3000 case ISD::FP16_TO_FP: {
3001 assert(!Subtarget->has16BitInsts() && "should only see if f16 is illegal");
3002 SDLoc SL(N);
3003 SDValue Src = N0.getOperand(0);
3004 EVT SrcVT = Src.getValueType();
3005
3006 // fabs (fp16_to_fp x) -> fp16_to_fp (and x, 0x7fff)
3007 SDValue IntFAbs = DAG.getNode(ISD::AND, SL, SrcVT, Src,
3008 DAG.getConstant(0x7fff, SL, SrcVT));
3009 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFAbs);
3010 }
Matt Arsenault2529fba2017-01-12 00:09:34 +00003011 default:
3012 return SDValue();
3013 }
3014}
3015
Tom Stellard50122a52014-04-07 19:45:41 +00003016SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N,
Matt Arsenaultca3976f2014-07-15 02:06:31 +00003017 DAGCombinerInfo &DCI) const {
Tom Stellard50122a52014-04-07 19:45:41 +00003018 SelectionDAG &DAG = DCI.DAG;
3019 SDLoc DL(N);
3020
3021 switch(N->getOpcode()) {
Matt Arsenault24e33d12015-07-03 23:33:38 +00003022 default:
3023 break;
Matt Arsenault79003342016-04-14 21:58:07 +00003024 case ISD::BITCAST: {
3025 EVT DestVT = N->getValueType(0);
Matt Arsenaultd99ef112016-09-17 15:44:16 +00003026
3027 // Push casts through vector builds. This helps avoid emitting a large
3028 // number of copies when materializing floating point vector constants.
3029 //
3030 // vNt1 bitcast (vNt0 (build_vector t0:x, t0:y)) =>
3031 // vnt1 = build_vector (t1 (bitcast t0:x)), (t1 (bitcast t0:y))
3032 if (DestVT.isVector()) {
3033 SDValue Src = N->getOperand(0);
3034 if (Src.getOpcode() == ISD::BUILD_VECTOR) {
3035 EVT SrcVT = Src.getValueType();
3036 unsigned NElts = DestVT.getVectorNumElements();
3037
3038 if (SrcVT.getVectorNumElements() == NElts) {
3039 EVT DestEltVT = DestVT.getVectorElementType();
3040
3041 SmallVector<SDValue, 8> CastedElts;
3042 SDLoc SL(N);
3043 for (unsigned I = 0, E = SrcVT.getVectorNumElements(); I != E; ++I) {
3044 SDValue Elt = Src.getOperand(I);
3045 CastedElts.push_back(DAG.getNode(ISD::BITCAST, DL, DestEltVT, Elt));
3046 }
3047
3048 return DAG.getBuildVector(DestVT, SL, CastedElts);
3049 }
3050 }
3051 }
3052
Matt Arsenault79003342016-04-14 21:58:07 +00003053 if (DestVT.getSizeInBits() != 64 && !DestVT.isVector())
3054 break;
3055
3056 // Fold bitcasts of constants.
3057 //
3058 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k)
3059 // TODO: Generalize and move to DAGCombiner
3060 SDValue Src = N->getOperand(0);
3061 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) {
3062 assert(Src.getValueType() == MVT::i64);
3063 SDLoc SL(N);
3064 uint64_t CVal = C->getZExtValue();
3065 return DAG.getNode(ISD::BUILD_VECTOR, SL, DestVT,
3066 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3067 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3068 }
3069
3070 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) {
3071 const APInt &Val = C->getValueAPF().bitcastToAPInt();
3072 SDLoc SL(N);
3073 uint64_t CVal = Val.getZExtValue();
3074 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3075 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3076 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3077
3078 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec);
3079 }
3080
3081 break;
3082 }
Matt Arsenault24692112015-07-14 18:20:33 +00003083 case ISD::SHL: {
3084 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3085 break;
3086
3087 return performShlCombine(N, DCI);
3088 }
Matt Arsenault80edab92016-01-18 21:43:36 +00003089 case ISD::SRL: {
3090 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3091 break;
3092
3093 return performSrlCombine(N, DCI);
3094 }
Matt Arsenault33e3ece2016-01-18 22:09:04 +00003095 case ISD::SRA: {
3096 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3097 break;
3098
3099 return performSraCombine(N, DCI);
3100 }
Matt Arsenault24e33d12015-07-03 23:33:38 +00003101 case ISD::MUL:
3102 return performMulCombine(N, DCI);
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003103 case ISD::MULHS:
3104 return performMulhsCombine(N, DCI);
3105 case ISD::MULHU:
3106 return performMulhuCombine(N, DCI);
Matt Arsenault24e33d12015-07-03 23:33:38 +00003107 case AMDGPUISD::MUL_I24:
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003108 case AMDGPUISD::MUL_U24:
3109 case AMDGPUISD::MULHI_I24:
3110 case AMDGPUISD::MULHI_U24: {
Tom Stellard6c7dd982016-10-21 20:25:11 +00003111 // If the first call to simplify is successfull, then N may end up being
3112 // deleted, so we shouldn't call simplifyI24 again.
3113 simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI);
Matt Arsenault24e33d12015-07-03 23:33:38 +00003114 return SDValue();
3115 }
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003116 case AMDGPUISD::MUL_LOHI_I24:
3117 case AMDGPUISD::MUL_LOHI_U24:
3118 return performMulLoHi24Combine(N, DCI);
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003119 case ISD::SELECT:
3120 return performSelectCombine(N, DCI);
Matt Arsenault2529fba2017-01-12 00:09:34 +00003121 case ISD::FNEG:
3122 return performFNegCombine(N, DCI);
Matt Arsenault9dba9bd2017-02-02 02:27:04 +00003123 case ISD::FABS:
3124 return performFAbsCombine(N, DCI);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003125 case AMDGPUISD::BFE_I32:
3126 case AMDGPUISD::BFE_U32: {
3127 assert(!N->getValueType(0).isVector() &&
3128 "Vector handling of BFE not implemented");
3129 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
3130 if (!Width)
3131 break;
3132
3133 uint32_t WidthVal = Width->getZExtValue() & 0x1f;
3134 if (WidthVal == 0)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003135 return DAG.getConstant(0, DL, MVT::i32);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003136
3137 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
3138 if (!Offset)
3139 break;
3140
3141 SDValue BitsFrom = N->getOperand(0);
3142 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f;
3143
3144 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32;
3145
3146 if (OffsetVal == 0) {
3147 // This is already sign / zero extended, so try to fold away extra BFEs.
3148 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal);
3149
3150 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom);
3151 if (OpSignBits >= SignBits)
3152 return BitsFrom;
Matt Arsenault05e96f42014-05-22 18:09:12 +00003153
3154 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal);
3155 if (Signed) {
3156 // This is a sign_extend_inreg. Replace it to take advantage of existing
3157 // DAG Combines. If not eliminated, we will match back to BFE during
3158 // selection.
3159
3160 // TODO: The sext_inreg of extended types ends, although we can could
3161 // handle them in a single BFE.
3162 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom,
3163 DAG.getValueType(SmallVT));
3164 }
3165
3166 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003167 }
3168
Matt Arsenaultf1794202014-10-15 05:07:00 +00003169 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) {
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003170 if (Signed) {
3171 return constantFoldBFE<int32_t>(DAG,
Matt Arsenault46cbc432014-09-19 00:42:06 +00003172 CVal->getSExtValue(),
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003173 OffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003174 WidthVal,
3175 DL);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003176 }
3177
3178 return constantFoldBFE<uint32_t>(DAG,
Matt Arsenault6462f942014-09-18 15:52:26 +00003179 CVal->getZExtValue(),
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003180 OffsetVal,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003181 WidthVal,
3182 DL);
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003183 }
3184
Matt Arsenault05e96f42014-05-22 18:09:12 +00003185 if ((OffsetVal + WidthVal) >= 32) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00003186 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32);
Matt Arsenault05e96f42014-05-22 18:09:12 +00003187 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32,
3188 BitsFrom, ShiftVal);
3189 }
3190
Matt Arsenault7b68fdf2014-10-15 17:58:34 +00003191 if (BitsFrom.hasOneUse()) {
Matt Arsenault6de7af42014-10-15 23:37:42 +00003192 APInt Demanded = APInt::getBitsSet(32,
3193 OffsetVal,
3194 OffsetVal + WidthVal);
3195
Matt Arsenault7b68fdf2014-10-15 17:58:34 +00003196 APInt KnownZero, KnownOne;
3197 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3198 !DCI.isBeforeLegalizeOps());
3199 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3200 if (TLO.ShrinkDemandedConstant(BitsFrom, Demanded) ||
3201 TLI.SimplifyDemandedBits(BitsFrom, Demanded,
3202 KnownZero, KnownOne, TLO)) {
3203 DCI.CommitTargetLoweringOpt(TLO);
3204 }
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003205 }
3206
3207 break;
3208 }
Matt Arsenault327bb5a2016-07-01 22:47:50 +00003209 case ISD::LOAD:
3210 return performLoadCombine(N, DCI);
Matt Arsenaultca3976f2014-07-15 02:06:31 +00003211 case ISD::STORE:
3212 return performStoreCombine(N, DCI);
Tom Stellard50122a52014-04-07 19:45:41 +00003213 }
3214 return SDValue();
3215}
3216
3217//===----------------------------------------------------------------------===//
Tom Stellard75aadc22012-12-11 21:25:42 +00003218// Helper functions
3219//===----------------------------------------------------------------------===//
3220
Tom Stellard75aadc22012-12-11 21:25:42 +00003221SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3222 const TargetRegisterClass *RC,
3223 unsigned Reg, EVT VT) const {
3224 MachineFunction &MF = DAG.getMachineFunction();
3225 MachineRegisterInfo &MRI = MF.getRegInfo();
3226 unsigned VirtualRegister;
3227 if (!MRI.isLiveIn(Reg)) {
3228 VirtualRegister = MRI.createVirtualRegister(RC);
3229 MRI.addLiveIn(Reg, VirtualRegister);
3230 } else {
3231 VirtualRegister = MRI.getLiveInVirtReg(Reg);
3232 }
3233 return DAG.getRegister(VirtualRegister, VT);
3234}
3235
Tom Stellarddcb9f092015-07-09 21:20:37 +00003236uint32_t AMDGPUTargetLowering::getImplicitParameterOffset(
3237 const AMDGPUMachineFunction *MFI, const ImplicitParameter Param) const {
Tom Stellardb2869eb2016-09-09 19:28:00 +00003238 unsigned Alignment = Subtarget->getAlignmentForImplicitArgPtr();
3239 uint64_t ArgOffset = alignTo(MFI->getABIArgOffset(), Alignment);
Tom Stellarddcb9f092015-07-09 21:20:37 +00003240 switch (Param) {
3241 case GRID_DIM:
3242 return ArgOffset;
3243 case GRID_OFFSET:
3244 return ArgOffset + 4;
3245 }
3246 llvm_unreachable("unexpected implicit parameter type");
3247}
3248
Tom Stellard75aadc22012-12-11 21:25:42 +00003249#define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
3250
3251const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
Matthias Braund04893f2015-05-07 21:33:59 +00003252 switch ((AMDGPUISD::NodeType)Opcode) {
3253 case AMDGPUISD::FIRST_NUMBER: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00003254 // AMDIL DAG nodes
Tom Stellard75aadc22012-12-11 21:25:42 +00003255 NODE_NAME_CASE(CALL);
3256 NODE_NAME_CASE(UMUL);
Tom Stellard75aadc22012-12-11 21:25:42 +00003257 NODE_NAME_CASE(BRANCH_COND);
3258
3259 // AMDGPU DAG nodes
Matt Arsenault9babdf42016-06-22 20:15:28 +00003260 NODE_NAME_CASE(ENDPGM)
3261 NODE_NAME_CASE(RETURN)
Tom Stellard75aadc22012-12-11 21:25:42 +00003262 NODE_NAME_CASE(DWORDADDR)
3263 NODE_NAME_CASE(FRACT)
Wei Ding07e03712016-07-28 16:42:13 +00003264 NODE_NAME_CASE(SETCC)
Tom Stellard8485fa02016-12-07 02:42:15 +00003265 NODE_NAME_CASE(SETREG)
3266 NODE_NAME_CASE(FMA_W_CHAIN)
3267 NODE_NAME_CASE(FMUL_W_CHAIN)
Matt Arsenault5d47d4a2014-06-12 21:15:44 +00003268 NODE_NAME_CASE(CLAMP)
Matthias Braund04893f2015-05-07 21:33:59 +00003269 NODE_NAME_CASE(COS_HW)
3270 NODE_NAME_CASE(SIN_HW)
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00003271 NODE_NAME_CASE(FMAX_LEGACY)
Matt Arsenaultda59f3d2014-11-13 23:03:09 +00003272 NODE_NAME_CASE(FMIN_LEGACY)
Matt Arsenaultcc3c2b32014-11-14 20:08:52 +00003273 NODE_NAME_CASE(FMAX3)
3274 NODE_NAME_CASE(SMAX3)
3275 NODE_NAME_CASE(UMAX3)
3276 NODE_NAME_CASE(FMIN3)
3277 NODE_NAME_CASE(SMIN3)
3278 NODE_NAME_CASE(UMIN3)
Matt Arsenaultf639c322016-01-28 20:53:42 +00003279 NODE_NAME_CASE(FMED3)
3280 NODE_NAME_CASE(SMED3)
3281 NODE_NAME_CASE(UMED3)
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003282 NODE_NAME_CASE(URECIP)
3283 NODE_NAME_CASE(DIV_SCALE)
3284 NODE_NAME_CASE(DIV_FMAS)
3285 NODE_NAME_CASE(DIV_FIXUP)
3286 NODE_NAME_CASE(TRIG_PREOP)
3287 NODE_NAME_CASE(RCP)
3288 NODE_NAME_CASE(RSQ)
Matt Arsenault32fc5272016-07-26 16:45:45 +00003289 NODE_NAME_CASE(RCP_LEGACY)
Matt Arsenault257d48d2014-06-24 22:13:39 +00003290 NODE_NAME_CASE(RSQ_LEGACY)
Matt Arsenault32fc5272016-07-26 16:45:45 +00003291 NODE_NAME_CASE(FMUL_LEGACY)
Matt Arsenault79963e82016-02-13 01:03:00 +00003292 NODE_NAME_CASE(RSQ_CLAMP)
Matt Arsenault2e7cc482014-08-15 17:30:25 +00003293 NODE_NAME_CASE(LDEXP)
Matt Arsenault4831ce52015-01-06 23:00:37 +00003294 NODE_NAME_CASE(FP_CLASS)
Matt Arsenaulta0050b02014-06-19 01:19:19 +00003295 NODE_NAME_CASE(DOT4)
Matthias Braund04893f2015-05-07 21:33:59 +00003296 NODE_NAME_CASE(CARRY)
3297 NODE_NAME_CASE(BORROW)
Matt Arsenaultfae02982014-03-17 18:58:11 +00003298 NODE_NAME_CASE(BFE_U32)
3299 NODE_NAME_CASE(BFE_I32)
Matt Arsenaultb3458362014-03-31 18:21:13 +00003300 NODE_NAME_CASE(BFI)
3301 NODE_NAME_CASE(BFM)
Matt Arsenaultde5fbe92016-01-11 17:02:00 +00003302 NODE_NAME_CASE(FFBH_U32)
Matt Arsenaultb51dcb92016-07-18 18:40:51 +00003303 NODE_NAME_CASE(FFBH_I32)
Tom Stellard50122a52014-04-07 19:45:41 +00003304 NODE_NAME_CASE(MUL_U24)
3305 NODE_NAME_CASE(MUL_I24)
Matt Arsenault2712d4a2016-08-27 01:32:27 +00003306 NODE_NAME_CASE(MULHI_U24)
3307 NODE_NAME_CASE(MULHI_I24)
3308 NODE_NAME_CASE(MUL_LOHI_U24)
3309 NODE_NAME_CASE(MUL_LOHI_I24)
Matt Arsenaulteb260202014-05-22 18:00:15 +00003310 NODE_NAME_CASE(MAD_U24)
3311 NODE_NAME_CASE(MAD_I24)
Matthias Braund04893f2015-05-07 21:33:59 +00003312 NODE_NAME_CASE(TEXTURE_FETCH)
Tom Stellard75aadc22012-12-11 21:25:42 +00003313 NODE_NAME_CASE(EXPORT)
Matt Arsenault7bee6ac2016-12-05 20:23:10 +00003314 NODE_NAME_CASE(EXPORT_DONE)
3315 NODE_NAME_CASE(R600_EXPORT)
Tom Stellardff62c352013-01-23 02:09:03 +00003316 NODE_NAME_CASE(CONST_ADDRESS)
Tom Stellardf3b2a1e2013-02-06 17:32:29 +00003317 NODE_NAME_CASE(REGISTER_LOAD)
3318 NODE_NAME_CASE(REGISTER_STORE)
Tom Stellard9fa17912013-08-14 23:24:45 +00003319 NODE_NAME_CASE(LOAD_INPUT)
3320 NODE_NAME_CASE(SAMPLE)
3321 NODE_NAME_CASE(SAMPLEB)
3322 NODE_NAME_CASE(SAMPLED)
3323 NODE_NAME_CASE(SAMPLEL)
Matt Arsenault364a6742014-06-11 17:50:44 +00003324 NODE_NAME_CASE(CVT_F32_UBYTE0)
3325 NODE_NAME_CASE(CVT_F32_UBYTE1)
3326 NODE_NAME_CASE(CVT_F32_UBYTE2)
3327 NODE_NAME_CASE(CVT_F32_UBYTE3)
Tom Stellard880a80a2014-06-17 16:53:14 +00003328 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR)
Tom Stellard067c8152014-07-21 14:01:14 +00003329 NODE_NAME_CASE(CONST_DATA_PTR)
Tom Stellardbf3e6e52016-06-14 20:29:59 +00003330 NODE_NAME_CASE(PC_ADD_REL_OFFSET)
Matt Arsenault03006fd2016-07-19 16:27:56 +00003331 NODE_NAME_CASE(KILL)
Jan Veselyf1705042017-01-20 21:24:26 +00003332 NODE_NAME_CASE(DUMMY_CHAIN)
Matthias Braund04893f2015-05-07 21:33:59 +00003333 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break;
Tom Stellardfc92e772015-05-12 14:18:14 +00003334 NODE_NAME_CASE(SENDMSG)
Jan Veselyd48445d2017-01-04 18:06:55 +00003335 NODE_NAME_CASE(SENDMSGHALT)
Tom Stellard2a9d9472015-05-12 15:00:46 +00003336 NODE_NAME_CASE(INTERP_MOV)
3337 NODE_NAME_CASE(INTERP_P1)
3338 NODE_NAME_CASE(INTERP_P2)
Tom Stellardd3ee8c12013-08-16 01:12:06 +00003339 NODE_NAME_CASE(STORE_MSKOR)
Matt Arsenaultdfaf4262016-04-25 19:27:09 +00003340 NODE_NAME_CASE(LOAD_CONSTANT)
Tom Stellardafcf12f2013-09-12 02:55:14 +00003341 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
Tom Stellard354a43c2016-04-01 18:27:37 +00003342 NODE_NAME_CASE(ATOMIC_CMP_SWAP)
Matt Arsenaulta9dbdca2016-04-12 14:05:04 +00003343 NODE_NAME_CASE(ATOMIC_INC)
3344 NODE_NAME_CASE(ATOMIC_DEC)
Tom Stellard6f9ef142016-12-20 17:19:44 +00003345 NODE_NAME_CASE(BUFFER_LOAD)
3346 NODE_NAME_CASE(BUFFER_LOAD_FORMAT)
Matthias Braund04893f2015-05-07 21:33:59 +00003347 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break;
Tom Stellard75aadc22012-12-11 21:25:42 +00003348 }
Matthias Braund04893f2015-05-07 21:33:59 +00003349 return nullptr;
Tom Stellard75aadc22012-12-11 21:25:42 +00003350}
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003351
Evandro Menezes21f9ce12016-11-10 23:31:06 +00003352SDValue AMDGPUTargetLowering::getSqrtEstimate(SDValue Operand,
3353 SelectionDAG &DAG, int Enabled,
3354 int &RefinementSteps,
3355 bool &UseOneConstNR,
3356 bool Reciprocal) const {
Matt Arsenaulte93d06a2015-01-13 20:53:18 +00003357 EVT VT = Operand.getValueType();
3358
3359 if (VT == MVT::f32) {
3360 RefinementSteps = 0;
3361 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand);
3362 }
3363
3364 // TODO: There is also f64 rsq instruction, but the documentation is less
3365 // clear on its precision.
3366
3367 return SDValue();
3368}
3369
Matt Arsenaultbf0db912015-01-13 20:53:23 +00003370SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand,
Sanjay Patel0051efc2016-10-20 16:55:45 +00003371 SelectionDAG &DAG, int Enabled,
3372 int &RefinementSteps) const {
Matt Arsenaultbf0db912015-01-13 20:53:23 +00003373 EVT VT = Operand.getValueType();
3374
3375 if (VT == MVT::f32) {
3376 // Reciprocal, < 1 ulp error.
3377 //
3378 // This reciprocal approximation converges to < 0.5 ulp error with one
3379 // newton rhapson performed with two fused multiple adds (FMAs).
3380
3381 RefinementSteps = 0;
3382 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand);
3383 }
3384
3385 // TODO: There is also f64 rcp instruction, but the documentation is less
3386 // clear on its precision.
3387
3388 return SDValue();
3389}
3390
Jay Foada0653a32014-05-14 21:14:37 +00003391void AMDGPUTargetLowering::computeKnownBitsForTargetNode(
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003392 const SDValue Op,
3393 APInt &KnownZero,
3394 APInt &KnownOne,
3395 const SelectionDAG &DAG,
3396 unsigned Depth) const {
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003397
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003398 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything.
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003399
3400 APInt KnownZero2;
3401 APInt KnownOne2;
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003402 unsigned Opc = Op.getOpcode();
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003403
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003404 switch (Opc) {
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003405 default:
3406 break;
Jan Vesely808fff52015-04-30 17:15:56 +00003407 case AMDGPUISD::CARRY:
3408 case AMDGPUISD::BORROW: {
3409 KnownZero = APInt::getHighBitsSet(32, 31);
3410 break;
3411 }
3412
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003413 case AMDGPUISD::BFE_I32:
3414 case AMDGPUISD::BFE_U32: {
3415 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3416 if (!CWidth)
3417 return;
3418
3419 unsigned BitWidth = 32;
3420 uint32_t Width = CWidth->getZExtValue() & 0x1f;
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003421
Matt Arsenaulta3fe7c62014-10-16 20:07:40 +00003422 if (Opc == AMDGPUISD::BFE_U32)
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003423 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - Width);
3424
Matt Arsenault378bf9c2014-03-31 19:35:33 +00003425 break;
3426 }
Matt Arsenaultaf6df9d2014-05-22 18:09:00 +00003427 }
Matt Arsenault0c274fe2014-03-25 18:18:27 +00003428}
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003429
3430unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode(
3431 SDValue Op,
3432 const SelectionDAG &DAG,
3433 unsigned Depth) const {
3434 switch (Op.getOpcode()) {
3435 case AMDGPUISD::BFE_I32: {
3436 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3437 if (!Width)
3438 return 1;
3439
3440 unsigned SignBits = 32 - Width->getZExtValue() + 1;
Artyom Skrobov314ee042015-11-25 19:41:11 +00003441 if (!isNullConstant(Op.getOperand(1)))
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003442 return SignBits;
3443
3444 // TODO: Could probably figure something out with non-0 offsets.
3445 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1);
3446 return std::max(SignBits, Op0SignBits);
3447 }
3448
Matt Arsenault5565f65e2014-05-22 18:09:07 +00003449 case AMDGPUISD::BFE_U32: {
3450 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3451 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1;
3452 }
3453
Jan Vesely808fff52015-04-30 17:15:56 +00003454 case AMDGPUISD::CARRY:
3455 case AMDGPUISD::BORROW:
3456 return 31;
3457
Matt Arsenaultbf8694d2014-05-22 18:09:03 +00003458 default:
3459 return 1;
3460 }
3461}