1 //===-- AMDGPUISelLowering.cpp - AMDGPU Common DAG lowering functions -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This is the parent TargetLowering class for hardware code gen
14 //===----------------------------------------------------------------------===//
16 #include "AMDGPUISelLowering.h"
18 #include "AMDGPUFrameLowering.h"
19 #include "AMDGPUIntrinsicInfo.h"
20 #include "AMDGPURegisterInfo.h"
21 #include "AMDGPUSubtarget.h"
22 #include "R600MachineFunctionInfo.h"
23 #include "SIMachineFunctionInfo.h"
24 #include "llvm/CodeGen/CallingConvLower.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/SelectionDAG.h"
28 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/DiagnosticInfo.h"
31 #include "SIInstrInfo.h"
34 static bool allocateKernArg(unsigned ValNo, MVT ValVT, MVT LocVT,
35 CCValAssign::LocInfo LocInfo,
36 ISD::ArgFlagsTy ArgFlags, CCState &State) {
37 MachineFunction &MF = State.getMachineFunction();
38 AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>();
40 uint64_t Offset = MFI->allocateKernArg(LocVT.getStoreSize(),
41 ArgFlags.getOrigAlign());
42 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT, Offset, LocVT, LocInfo));
46 #include "AMDGPUGenCallingConv.inc"
48 // Find a larger type to do a load / store of a vector with.
49 EVT AMDGPUTargetLowering::getEquivalentMemType(LLVMContext &Ctx, EVT VT) {
50 unsigned StoreSize = VT.getStoreSizeInBits();
52 return EVT::getIntegerVT(Ctx, StoreSize);
54 assert(StoreSize % 32 == 0 && "Store size not a multiple of 32");
55 return EVT::getVectorVT(Ctx, MVT::i32, StoreSize / 32);
58 AMDGPUTargetLowering::AMDGPUTargetLowering(const TargetMachine &TM,
59 const AMDGPUSubtarget &STI)
60 : TargetLowering(TM), Subtarget(&STI) {
61 // Lower floating point store/load to integer store/load to reduce the number
62 // of patterns in tablegen.
63 setOperationAction(ISD::LOAD, MVT::f32, Promote);
64 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
66 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
67 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
69 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
70 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
72 setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
73 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
75 setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
76 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
78 setOperationAction(ISD::LOAD, MVT::i64, Promote);
79 AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
81 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
82 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
84 setOperationAction(ISD::LOAD, MVT::f64, Promote);
85 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::v2i32);
87 setOperationAction(ISD::LOAD, MVT::v2f64, Promote);
88 AddPromotedToType(ISD::LOAD, MVT::v2f64, MVT::v4i32);
90 // There are no 64-bit extloads. These should be done as a 32-bit extload and
91 // an extension to 64-bit.
92 for (MVT VT : MVT::integer_valuetypes()) {
93 setLoadExtAction(ISD::EXTLOAD, MVT::i64, VT, Expand);
94 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, VT, Expand);
95 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, VT, Expand);
98 for (MVT VT : MVT::integer_valuetypes()) {
102 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
103 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
104 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
105 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
107 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
108 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
109 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
110 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
112 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
113 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
114 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
115 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
118 for (MVT VT : MVT::integer_vector_valuetypes()) {
119 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i8, Expand);
120 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i8, Expand);
121 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i8, Expand);
122 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i8, Expand);
123 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i8, Expand);
124 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i8, Expand);
125 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i16, Expand);
126 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i16, Expand);
127 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i16, Expand);
128 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i16, Expand);
129 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i16, Expand);
130 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i16, Expand);
133 setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
134 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, MVT::v2f16, Expand);
135 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, MVT::v4f16, Expand);
136 setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, MVT::v8f16, Expand);
138 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
139 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
140 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Expand);
141 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f32, Expand);
143 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
144 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
145 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f16, Expand);
146 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f16, Expand);
148 setOperationAction(ISD::STORE, MVT::f32, Promote);
149 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
151 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
152 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
154 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
155 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
157 setOperationAction(ISD::STORE, MVT::v8f32, Promote);
158 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
160 setOperationAction(ISD::STORE, MVT::v16f32, Promote);
161 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
163 setOperationAction(ISD::STORE, MVT::i64, Promote);
164 AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
166 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
167 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
169 setOperationAction(ISD::STORE, MVT::f64, Promote);
170 AddPromotedToType(ISD::STORE, MVT::f64, MVT::v2i32);
172 setOperationAction(ISD::STORE, MVT::v2f64, Promote);
173 AddPromotedToType(ISD::STORE, MVT::v2f64, MVT::v4i32);
175 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
176 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
178 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
179 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
181 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
182 setTruncStoreAction(MVT::v16i32, MVT::v16i8, Expand);
183 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
185 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
186 setTruncStoreAction(MVT::i64, MVT::i8, Expand);
187 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
188 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
190 setTruncStoreAction(MVT::v2i64, MVT::v2i1, Expand);
191 setTruncStoreAction(MVT::v2i64, MVT::v2i8, Expand);
192 setTruncStoreAction(MVT::v2i64, MVT::v2i16, Expand);
193 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
195 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
196 setTruncStoreAction(MVT::v2f32, MVT::v2f16, Expand);
197 setTruncStoreAction(MVT::v4f32, MVT::v4f16, Expand);
198 setTruncStoreAction(MVT::v8f32, MVT::v8f16, Expand);
200 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
201 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
203 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
204 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
206 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Expand);
207 setTruncStoreAction(MVT::v4f64, MVT::v4f16, Expand);
209 setTruncStoreAction(MVT::v8f64, MVT::v8f32, Expand);
210 setTruncStoreAction(MVT::v8f64, MVT::v8f16, Expand);
213 setOperationAction(ISD::Constant, MVT::i32, Legal);
214 setOperationAction(ISD::Constant, MVT::i64, Legal);
215 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
216 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
218 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
219 setOperationAction(ISD::BRIND, MVT::Other, Expand);
221 // This is totally unsupported, just custom lower to produce an error.
222 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
224 // We need to custom lower some of the intrinsics
225 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
226 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
228 // Library functions. These default to Expand, but we have instructions
230 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
231 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
232 setOperationAction(ISD::FPOW, MVT::f32, Legal);
233 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
234 setOperationAction(ISD::FABS, MVT::f32, Legal);
235 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
236 setOperationAction(ISD::FRINT, MVT::f32, Legal);
237 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
238 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
239 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
241 setOperationAction(ISD::FROUND, MVT::f32, Custom);
242 setOperationAction(ISD::FROUND, MVT::f64, Custom);
244 setOperationAction(ISD::FNEARBYINT, MVT::f32, Custom);
245 setOperationAction(ISD::FNEARBYINT, MVT::f64, Custom);
247 setOperationAction(ISD::FREM, MVT::f32, Custom);
248 setOperationAction(ISD::FREM, MVT::f64, Custom);
250 // v_mad_f32 does not support denormals according to some sources.
251 if (!Subtarget->hasFP32Denormals())
252 setOperationAction(ISD::FMAD, MVT::f32, Legal);
254 // Expand to fneg + fadd.
255 setOperationAction(ISD::FSUB, MVT::f64, Expand);
257 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
258 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
259 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
260 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
261 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
262 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
263 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f32, Custom);
264 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
265 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f32, Custom);
266 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i32, Custom);
268 if (Subtarget->getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
269 setOperationAction(ISD::FCEIL, MVT::f64, Custom);
270 setOperationAction(ISD::FTRUNC, MVT::f64, Custom);
271 setOperationAction(ISD::FRINT, MVT::f64, Custom);
272 setOperationAction(ISD::FFLOOR, MVT::f64, Custom);
275 if (!Subtarget->hasBFI()) {
276 // fcopysign can be done in a single instruction with BFI.
277 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
278 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
281 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
282 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Custom);
284 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
285 for (MVT VT : ScalarIntVTs) {
286 // These should use [SU]DIVREM, so set them to expand
287 setOperationAction(ISD::SDIV, VT, Expand);
288 setOperationAction(ISD::UDIV, VT, Expand);
289 setOperationAction(ISD::SREM, VT, Expand);
290 setOperationAction(ISD::UREM, VT, Expand);
292 // GPU does not have divrem function for signed or unsigned.
293 setOperationAction(ISD::SDIVREM, VT, Custom);
294 setOperationAction(ISD::UDIVREM, VT, Custom);
296 // GPU does not have [S|U]MUL_LOHI functions as a single instruction.
297 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
298 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
300 setOperationAction(ISD::BSWAP, VT, Expand);
301 setOperationAction(ISD::CTTZ, VT, Expand);
302 setOperationAction(ISD::CTLZ, VT, Expand);
305 if (!Subtarget->hasBCNT(32))
306 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
308 if (!Subtarget->hasBCNT(64))
309 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
311 // The hardware supports 32-bit ROTR, but not ROTL.
312 setOperationAction(ISD::ROTL, MVT::i32, Expand);
313 setOperationAction(ISD::ROTL, MVT::i64, Expand);
314 setOperationAction(ISD::ROTR, MVT::i64, Expand);
316 setOperationAction(ISD::MUL, MVT::i64, Expand);
317 setOperationAction(ISD::MULHU, MVT::i64, Expand);
318 setOperationAction(ISD::MULHS, MVT::i64, Expand);
319 setOperationAction(ISD::UDIV, MVT::i32, Expand);
320 setOperationAction(ISD::UREM, MVT::i32, Expand);
321 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
322 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
323 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
324 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
325 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
327 setOperationAction(ISD::SMIN, MVT::i32, Legal);
328 setOperationAction(ISD::UMIN, MVT::i32, Legal);
329 setOperationAction(ISD::SMAX, MVT::i32, Legal);
330 setOperationAction(ISD::UMAX, MVT::i32, Legal);
332 if (Subtarget->hasFFBH())
333 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom);
335 if (Subtarget->hasFFBL())
336 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Legal);
338 setOperationAction(ISD::CTLZ, MVT::i64, Custom);
339 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
341 // We only really have 32-bit BFE instructions (and 16-bit on VI).
343 // On SI+ there are 64-bit BFEs, but they are scalar only and there isn't any
344 // effort to match them now. We want this to be false for i64 cases when the
345 // extraction isn't restricted to the upper or lower half. Ideally we would
346 // have some pass reduce 64-bit extracts to 32-bit if possible. Extracts that
347 // span the midpoint are probably relatively rare, so don't worry about them
349 if (Subtarget->hasBFE())
350 setHasExtractBitsInsn(true);
352 static const MVT::SimpleValueType VectorIntTypes[] = {
353 MVT::v2i32, MVT::v4i32
356 for (MVT VT : VectorIntTypes) {
357 // Expand the following operations for the current type by default.
358 setOperationAction(ISD::ADD, VT, Expand);
359 setOperationAction(ISD::AND, VT, Expand);
360 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
361 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
362 setOperationAction(ISD::MUL, VT, Expand);
363 setOperationAction(ISD::MULHU, VT, Expand);
364 setOperationAction(ISD::MULHS, VT, Expand);
365 setOperationAction(ISD::OR, VT, Expand);
366 setOperationAction(ISD::SHL, VT, Expand);
367 setOperationAction(ISD::SRA, VT, Expand);
368 setOperationAction(ISD::SRL, VT, Expand);
369 setOperationAction(ISD::ROTL, VT, Expand);
370 setOperationAction(ISD::ROTR, VT, Expand);
371 setOperationAction(ISD::SUB, VT, Expand);
372 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
373 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
374 setOperationAction(ISD::SDIV, VT, Expand);
375 setOperationAction(ISD::UDIV, VT, Expand);
376 setOperationAction(ISD::SREM, VT, Expand);
377 setOperationAction(ISD::UREM, VT, Expand);
378 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
379 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
380 setOperationAction(ISD::SDIVREM, VT, Custom);
381 setOperationAction(ISD::UDIVREM, VT, Expand);
382 setOperationAction(ISD::ADDC, VT, Expand);
383 setOperationAction(ISD::SUBC, VT, Expand);
384 setOperationAction(ISD::ADDE, VT, Expand);
385 setOperationAction(ISD::SUBE, VT, Expand);
386 setOperationAction(ISD::SELECT, VT, Expand);
387 setOperationAction(ISD::VSELECT, VT, Expand);
388 setOperationAction(ISD::SELECT_CC, VT, Expand);
389 setOperationAction(ISD::XOR, VT, Expand);
390 setOperationAction(ISD::BSWAP, VT, Expand);
391 setOperationAction(ISD::CTPOP, VT, Expand);
392 setOperationAction(ISD::CTTZ, VT, Expand);
393 setOperationAction(ISD::CTLZ, VT, Expand);
394 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
397 static const MVT::SimpleValueType FloatVectorTypes[] = {
398 MVT::v2f32, MVT::v4f32
401 for (MVT VT : FloatVectorTypes) {
402 setOperationAction(ISD::FABS, VT, Expand);
403 setOperationAction(ISD::FMINNUM, VT, Expand);
404 setOperationAction(ISD::FMAXNUM, VT, Expand);
405 setOperationAction(ISD::FADD, VT, Expand);
406 setOperationAction(ISD::FCEIL, VT, Expand);
407 setOperationAction(ISD::FCOS, VT, Expand);
408 setOperationAction(ISD::FDIV, VT, Expand);
409 setOperationAction(ISD::FEXP2, VT, Expand);
410 setOperationAction(ISD::FLOG2, VT, Expand);
411 setOperationAction(ISD::FREM, VT, Expand);
412 setOperationAction(ISD::FPOW, VT, Expand);
413 setOperationAction(ISD::FFLOOR, VT, Expand);
414 setOperationAction(ISD::FTRUNC, VT, Expand);
415 setOperationAction(ISD::FMUL, VT, Expand);
416 setOperationAction(ISD::FMA, VT, Expand);
417 setOperationAction(ISD::FRINT, VT, Expand);
418 setOperationAction(ISD::FNEARBYINT, VT, Expand);
419 setOperationAction(ISD::FSQRT, VT, Expand);
420 setOperationAction(ISD::FSIN, VT, Expand);
421 setOperationAction(ISD::FSUB, VT, Expand);
422 setOperationAction(ISD::FNEG, VT, Expand);
423 setOperationAction(ISD::VSELECT, VT, Expand);
424 setOperationAction(ISD::SELECT_CC, VT, Expand);
425 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
426 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
429 // This causes using an unrolled select operation rather than expansion with
430 // bit operations. This is in general better, but the alternative using BFI
431 // instructions may be better if the select sources are SGPRs.
432 setOperationAction(ISD::SELECT, MVT::v2f32, Promote);
433 AddPromotedToType(ISD::SELECT, MVT::v2f32, MVT::v2i32);
435 setOperationAction(ISD::SELECT, MVT::v4f32, Promote);
436 AddPromotedToType(ISD::SELECT, MVT::v4f32, MVT::v4i32);
438 // There are no libcalls of any kind.
439 for (int I = 0; I < RTLIB::UNKNOWN_LIBCALL; ++I)
440 setLibcallName(static_cast<RTLIB::Libcall>(I), nullptr);
442 setBooleanContents(ZeroOrNegativeOneBooleanContent);
443 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
445 setSchedulingPreference(Sched::RegPressure);
446 setJumpIsExpensive(true);
447 setHasMultipleConditionRegisters(true);
449 // SI at least has hardware support for floating point exceptions, but no way
450 // of using or handling them is implemented. They are also optional in OpenCL
452 setHasFloatingPointExceptions(Subtarget->hasFPExceptions());
454 PredictableSelectIsExpensive = false;
456 // We want to find all load dependencies for long chains of stores to enable
457 // merging into very wide vectors. The problem is with vectors with > 4
458 // elements. MergeConsecutiveStores will attempt to merge these because x8/x16
459 // vectors are a legal type, even though we have to split the loads
460 // usually. When we can more precisely specify load legality per address
461 // space, we should be able to make FindBetterChain/MergeConsecutiveStores
462 // smarter so that they can figure out what to do in 2 iterations without all
463 // N > 4 stores on the same chain.
464 GatherAllAliasesMaxDepth = 16;
466 // FIXME: Need to really handle these.
467 MaxStoresPerMemcpy = 4096;
468 MaxStoresPerMemmove = 4096;
469 MaxStoresPerMemset = 4096;
471 setTargetDAGCombine(ISD::BITCAST);
472 setTargetDAGCombine(ISD::SHL);
473 setTargetDAGCombine(ISD::SRA);
474 setTargetDAGCombine(ISD::SRL);
475 setTargetDAGCombine(ISD::MUL);
476 setTargetDAGCombine(ISD::MULHU);
477 setTargetDAGCombine(ISD::MULHS);
478 setTargetDAGCombine(ISD::SELECT);
479 setTargetDAGCombine(ISD::SELECT_CC);
480 setTargetDAGCombine(ISD::STORE);
481 setTargetDAGCombine(ISD::FADD);
482 setTargetDAGCombine(ISD::FSUB);
485 //===----------------------------------------------------------------------===//
486 // Target Information
487 //===----------------------------------------------------------------------===//
489 MVT AMDGPUTargetLowering::getVectorIdxTy(const DataLayout &) const {
493 bool AMDGPUTargetLowering::isSelectSupported(SelectSupportKind SelType) const {
497 // The backend supports 32 and 64 bit floating point immediates.
498 // FIXME: Why are we reporting vectors of FP immediates as legal?
499 bool AMDGPUTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
500 EVT ScalarVT = VT.getScalarType();
501 return (ScalarVT == MVT::f32 || ScalarVT == MVT::f64 ||
502 (ScalarVT == MVT::f16 && Subtarget->has16BitInsts()));
505 // We don't want to shrink f64 / f32 constants.
506 bool AMDGPUTargetLowering::ShouldShrinkFPConstant(EVT VT) const {
507 EVT ScalarVT = VT.getScalarType();
508 return (ScalarVT != MVT::f32 && ScalarVT != MVT::f64);
511 bool AMDGPUTargetLowering::shouldReduceLoadWidth(SDNode *N,
515 unsigned NewSize = NewVT.getStoreSizeInBits();
517 // If we are reducing to a 32-bit load, this is always better.
521 EVT OldVT = N->getValueType(0);
522 unsigned OldSize = OldVT.getStoreSizeInBits();
524 // Don't produce extloads from sub 32-bit types. SI doesn't have scalar
525 // extloads, so doing one requires using a buffer_load. In cases where we
526 // still couldn't use a scalar load, using the wider load shouldn't really
529 // If the old size already had to be an extload, there's no harm in continuing
530 // to reduce the width.
531 return (OldSize < 32);
534 bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy,
537 assert(LoadTy.getSizeInBits() == CastTy.getSizeInBits());
539 if (LoadTy.getScalarType() == MVT::i32)
542 unsigned LScalarSize = LoadTy.getScalarSizeInBits();
543 unsigned CastScalarSize = CastTy.getScalarSizeInBits();
545 return (LScalarSize < CastScalarSize) ||
546 (CastScalarSize >= 32);
549 // SI+ has instructions for cttz / ctlz for 32-bit values. This is probably also
550 // profitable with the expansion for 64-bit since it's generally good to
552 // FIXME: These should really have the size as a parameter.
553 bool AMDGPUTargetLowering::isCheapToSpeculateCttz() const {
557 bool AMDGPUTargetLowering::isCheapToSpeculateCtlz() const {
561 //===---------------------------------------------------------------------===//
563 //===---------------------------------------------------------------------===//
565 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
566 assert(VT.isFloatingPoint());
567 return VT == MVT::f32 || VT == MVT::f64 || (Subtarget->has16BitInsts() &&
571 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
572 return isFAbsFree(VT);
575 bool AMDGPUTargetLowering:: storeOfVectorConstantIsCheap(EVT MemVT,
581 bool AMDGPUTargetLowering::aggressivelyPreferBuildVectorSources(EVT VecVT) const {
582 // There are few operations which truly have vector input operands. Any vector
583 // operation is going to involve operations on each component, and a
584 // build_vector will be a copy per element, so it always makes sense to use a
585 // build_vector input in place of the extracted element to avoid a copy into a
588 // We should probably only do this if all users are extracts only, but this
589 // should be the common case.
593 bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const {
594 // Truncate is just accessing a subregister.
596 unsigned SrcSize = Source.getSizeInBits();
597 unsigned DestSize = Dest.getSizeInBits();
599 return DestSize < SrcSize && DestSize % 32 == 0 ;
602 bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const {
603 // Truncate is just accessing a subregister.
605 unsigned SrcSize = Source->getScalarSizeInBits();
606 unsigned DestSize = Dest->getScalarSizeInBits();
608 if (DestSize== 16 && Subtarget->has16BitInsts())
609 return SrcSize >= 32;
611 return DestSize < SrcSize && DestSize % 32 == 0;
614 bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const {
615 unsigned SrcSize = Src->getScalarSizeInBits();
616 unsigned DestSize = Dest->getScalarSizeInBits();
618 if (SrcSize == 16 && Subtarget->has16BitInsts())
619 return DestSize >= 32;
621 return SrcSize == 32 && DestSize == 64;
624 bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const {
625 // Any register load of a 64-bit value really requires 2 32-bit moves. For all
626 // practical purposes, the extra mov 0 to load a 64-bit is free. As used,
627 // this will enable reducing 64-bit operations the 32-bit, which is always
631 return Dest == MVT::i32 ||Dest == MVT::i64 ;
633 return Src == MVT::i32 && Dest == MVT::i64;
636 bool AMDGPUTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
637 return isZExtFree(Val.getValueType(), VT2);
640 bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const {
641 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a
642 // limited number of native 64-bit operations. Shrinking an operation to fit
643 // in a single 32-bit register should always be helpful. As currently used,
644 // this is much less general than the name suggests, and is only used in
645 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is
646 // not profitable, and may actually be harmful.
647 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32;
650 //===---------------------------------------------------------------------===//
651 // TargetLowering Callbacks
652 //===---------------------------------------------------------------------===//
654 /// The SelectionDAGBuilder will automatically promote function arguments
655 /// with illegal types. However, this does not work for the AMDGPU targets
656 /// since the function arguments are stored in memory as these illegal types.
657 /// In order to handle this properly we need to get the original types sizes
658 /// from the LLVM IR Function and fixup the ISD:InputArg values before
659 /// passing them to AnalyzeFormalArguments()
661 /// When the SelectionDAGBuilder computes the Ins, it takes care of splitting
662 /// input values across multiple registers. Each item in the Ins array
663 /// represents a single value that will be stored in regsters. Ins[x].VT is
664 /// the value type of the value that will be stored in the register, so
665 /// whatever SDNode we lower the argument to needs to be this type.
667 /// In order to correctly lower the arguments we need to know the size of each
668 /// argument. Since Ins[x].VT gives us the size of the register that will
669 /// hold the value, we need to look at Ins[x].ArgVT to see the 'real' type
670 /// for the orignal function argument so that we can deduce the correct memory
671 /// type to use for Ins[x]. In most cases the correct memory type will be
672 /// Ins[x].ArgVT. However, this will not always be the case. If, for example,
673 /// we have a kernel argument of type v8i8, this argument will be split into
674 /// 8 parts and each part will be represented by its own item in the Ins array.
675 /// For each part the Ins[x].ArgVT will be the v8i8, which is the full type of
676 /// the argument before it was split. From this, we deduce that the memory type
677 /// for each individual part is i8. We pass the memory type as LocVT to the
678 /// calling convention analysis function and the register type (Ins[x].VT) as
680 void AMDGPUTargetLowering::analyzeFormalArgumentsCompute(CCState &State,
681 const SmallVectorImpl<ISD::InputArg> &Ins) const {
682 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
683 const ISD::InputArg &In = Ins[i];
686 unsigned NumRegs = getNumRegisters(State.getContext(), In.ArgVT);
688 if (!Subtarget->isAmdHsaOS() &&
689 (In.ArgVT == MVT::i16 || In.ArgVT == MVT::i8 || In.ArgVT == MVT::f16)) {
690 // The ABI says the caller will extend these values to 32-bits.
691 MemVT = In.ArgVT.isInteger() ? MVT::i32 : MVT::f32;
692 } else if (NumRegs == 1) {
693 // This argument is not split, so the IR type is the memory type.
694 assert(!In.Flags.isSplit());
695 if (In.ArgVT.isExtended()) {
696 // We have an extended type, like i24, so we should just use the register type
701 } else if (In.ArgVT.isVector() && In.VT.isVector() &&
702 In.ArgVT.getScalarType() == In.VT.getScalarType()) {
703 assert(In.ArgVT.getVectorNumElements() > In.VT.getVectorNumElements());
704 // We have a vector value which has been split into a vector with
705 // the same scalar type, but fewer elements. This should handle
706 // all the floating-point vector types.
708 } else if (In.ArgVT.isVector() &&
709 In.ArgVT.getVectorNumElements() == NumRegs) {
710 // This arg has been split so that each element is stored in a separate
712 MemVT = In.ArgVT.getScalarType();
713 } else if (In.ArgVT.isExtended()) {
714 // We have an extended type, like i65.
717 unsigned MemoryBits = In.ArgVT.getStoreSizeInBits() / NumRegs;
718 assert(In.ArgVT.getStoreSizeInBits() % NumRegs == 0);
719 if (In.VT.isInteger()) {
720 MemVT = EVT::getIntegerVT(State.getContext(), MemoryBits);
721 } else if (In.VT.isVector()) {
722 assert(!In.VT.getScalarType().isFloatingPoint());
723 unsigned NumElements = In.VT.getVectorNumElements();
724 assert(MemoryBits % NumElements == 0);
725 // This vector type has been split into another vector type with
726 // a different elements size.
727 EVT ScalarVT = EVT::getIntegerVT(State.getContext(),
728 MemoryBits / NumElements);
729 MemVT = EVT::getVectorVT(State.getContext(), ScalarVT, NumElements);
731 llvm_unreachable("cannot deduce memory type.");
735 // Convert one element vectors to scalar.
736 if (MemVT.isVector() && MemVT.getVectorNumElements() == 1)
737 MemVT = MemVT.getScalarType();
739 if (MemVT.isExtended()) {
740 // This should really only happen if we have vec3 arguments
741 assert(MemVT.isVector() && MemVT.getVectorNumElements() == 3);
742 MemVT = MemVT.getPow2VectorType(State.getContext());
745 assert(MemVT.isSimple());
746 allocateKernArg(i, In.VT, MemVT.getSimpleVT(), CCValAssign::Full, In.Flags,
751 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
752 const SmallVectorImpl<ISD::InputArg> &Ins) const {
753 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
756 void AMDGPUTargetLowering::AnalyzeReturn(CCState &State,
757 const SmallVectorImpl<ISD::OutputArg> &Outs) const {
759 State.AnalyzeReturn(Outs, RetCC_SI);
763 AMDGPUTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
765 const SmallVectorImpl<ISD::OutputArg> &Outs,
766 const SmallVectorImpl<SDValue> &OutVals,
767 const SDLoc &DL, SelectionDAG &DAG) const {
768 return DAG.getNode(AMDGPUISD::ENDPGM, DL, MVT::Other, Chain);
771 //===---------------------------------------------------------------------===//
772 // Target specific lowering
773 //===---------------------------------------------------------------------===//
775 SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI,
776 SmallVectorImpl<SDValue> &InVals) const {
777 SDValue Callee = CLI.Callee;
778 SelectionDAG &DAG = CLI.DAG;
780 const Function &Fn = *DAG.getMachineFunction().getFunction();
782 StringRef FuncName("<unknown>");
784 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee))
785 FuncName = G->getSymbol();
786 else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
787 FuncName = G->getGlobal()->getName();
789 DiagnosticInfoUnsupported NoCalls(
790 Fn, "unsupported call to function " + FuncName, CLI.DL.getDebugLoc());
791 DAG.getContext()->diagnose(NoCalls);
793 if (!CLI.IsTailCall) {
794 for (unsigned I = 0, E = CLI.Ins.size(); I != E; ++I)
795 InVals.push_back(DAG.getUNDEF(CLI.Ins[I].VT));
798 return DAG.getEntryNode();
801 SDValue AMDGPUTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
802 SelectionDAG &DAG) const {
803 const Function &Fn = *DAG.getMachineFunction().getFunction();
805 DiagnosticInfoUnsupported NoDynamicAlloca(Fn, "unsupported dynamic alloca",
806 SDLoc(Op).getDebugLoc());
807 DAG.getContext()->diagnose(NoDynamicAlloca);
808 auto Ops = {DAG.getConstant(0, SDLoc(), Op.getValueType()), Op.getOperand(0)};
809 return DAG.getMergeValues(Ops, SDLoc());
812 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op,
813 SelectionDAG &DAG) const {
814 switch (Op.getOpcode()) {
817 llvm_unreachable("Custom lowering code for this"
818 "instruction is not implemented yet!");
820 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
821 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
822 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
823 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
824 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
825 case ISD::SDIVREM: return LowerSDIVREM(Op, DAG);
826 case ISD::FREM: return LowerFREM(Op, DAG);
827 case ISD::FCEIL: return LowerFCEIL(Op, DAG);
828 case ISD::FTRUNC: return LowerFTRUNC(Op, DAG);
829 case ISD::FRINT: return LowerFRINT(Op, DAG);
830 case ISD::FNEARBYINT: return LowerFNEARBYINT(Op, DAG);
831 case ISD::FROUND: return LowerFROUND(Op, DAG);
832 case ISD::FFLOOR: return LowerFFLOOR(Op, DAG);
833 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
834 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
835 case ISD::FP_TO_FP16: return LowerFP_TO_FP16(Op, DAG);
836 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
837 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
839 case ISD::CTLZ_ZERO_UNDEF:
840 return LowerCTLZ(Op, DAG);
841 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
846 void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N,
847 SmallVectorImpl<SDValue> &Results,
848 SelectionDAG &DAG) const {
849 switch (N->getOpcode()) {
850 case ISD::SIGN_EXTEND_INREG:
851 // Different parts of legalization seem to interpret which type of
852 // sign_extend_inreg is the one to check for custom lowering. The extended
853 // from type is what really matters, but some places check for custom
854 // lowering of the result type. This results in trying to use
855 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do
856 // nothing here and let the illegal result integer be handled normally.
863 static bool hasDefinedInitializer(const GlobalValue *GV) {
864 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
865 if (!GVar || !GVar->hasInitializer())
868 return !isa<UndefValue>(GVar->getInitializer());
871 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
873 SelectionDAG &DAG) const {
875 const DataLayout &DL = DAG.getDataLayout();
876 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
877 const GlobalValue *GV = G->getGlobal();
879 switch (G->getAddressSpace()) {
880 case AMDGPUAS::LOCAL_ADDRESS: {
881 // XXX: What does the value of G->getOffset() mean?
882 assert(G->getOffset() == 0 &&
883 "Do not know what to do with an non-zero offset");
885 // TODO: We could emit code to handle the initialization somewhere.
886 if (hasDefinedInitializer(GV))
889 unsigned Offset = MFI->allocateLDSGlobal(DL, *GV);
890 return DAG.getConstant(Offset, SDLoc(Op), Op.getValueType());
894 const Function &Fn = *DAG.getMachineFunction().getFunction();
895 DiagnosticInfoUnsupported BadInit(
896 Fn, "unsupported initializer for address space", SDLoc(Op).getDebugLoc());
897 DAG.getContext()->diagnose(BadInit);
901 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
902 SelectionDAG &DAG) const {
903 SmallVector<SDValue, 8> Args;
905 for (const SDUse &U : Op->ops())
906 DAG.ExtractVectorElements(U.get(), Args);
908 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
911 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
912 SelectionDAG &DAG) const {
914 SmallVector<SDValue, 8> Args;
915 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
916 EVT VT = Op.getValueType();
917 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start,
918 VT.getVectorNumElements());
920 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
923 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
924 SelectionDAG &DAG) const {
925 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
927 EVT VT = Op.getValueType();
929 switch (IntrinsicID) {
931 case AMDGPUIntrinsic::AMDGPU_clamp: // Legacy name.
932 return DAG.getNode(AMDGPUISD::CLAMP, DL, VT,
933 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
935 case AMDGPUIntrinsic::AMDGPU_bfe_i32:
936 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT,
941 case AMDGPUIntrinsic::AMDGPU_bfe_u32:
942 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT,
949 /// \brief Generate Min/Max node
950 SDValue AMDGPUTargetLowering::CombineFMinMaxLegacy(const SDLoc &DL, EVT VT,
951 SDValue LHS, SDValue RHS,
952 SDValue True, SDValue False,
954 DAGCombinerInfo &DCI) const {
955 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
958 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True))
961 SelectionDAG &DAG = DCI.DAG;
962 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
980 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
981 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
987 // Ordered. Assume ordered for undefined.
989 // Only do this after legalization to avoid interfering with other combines
990 // which might occur.
991 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
992 !DCI.isCalledByLegalizer())
995 // We need to permute the operands to get the correct NaN behavior. The
996 // selected operand is the second one based on the failing compare with NaN,
997 // so permute it based on the compare type the hardware uses.
999 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
1000 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
1005 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
1006 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
1012 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1013 !DCI.isCalledByLegalizer())
1017 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1018 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1020 case ISD::SETCC_INVALID:
1021 llvm_unreachable("Invalid setcc condcode!");
1026 std::pair<SDValue, SDValue>
1027 AMDGPUTargetLowering::split64BitValue(SDValue Op, SelectionDAG &DAG) const {
1030 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1032 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1033 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1035 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1036 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1038 return std::make_pair(Lo, Hi);
1041 SDValue AMDGPUTargetLowering::getLoHalf64(SDValue Op, SelectionDAG &DAG) const {
1044 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1045 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1046 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1049 SDValue AMDGPUTargetLowering::getHiHalf64(SDValue Op, SelectionDAG &DAG) const {
1052 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1053 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1054 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1057 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue Op,
1058 SelectionDAG &DAG) const {
1059 LoadSDNode *Load = cast<LoadSDNode>(Op);
1060 EVT VT = Op.getValueType();
1063 // If this is a 2 element vector, we really want to scalarize and not create
1064 // weird 1 element vectors.
1065 if (VT.getVectorNumElements() == 2)
1066 return scalarizeVectorLoad(Load, DAG);
1068 SDValue BasePtr = Load->getBasePtr();
1069 EVT PtrVT = BasePtr.getValueType();
1070 EVT MemVT = Load->getMemoryVT();
1073 const MachinePointerInfo &SrcValue = Load->getMemOperand()->getPointerInfo();
1076 EVT LoMemVT, HiMemVT;
1079 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1080 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1081 std::tie(Lo, Hi) = DAG.SplitVector(Op, SL, LoVT, HiVT);
1083 unsigned Size = LoMemVT.getStoreSize();
1084 unsigned BaseAlign = Load->getAlignment();
1085 unsigned HiAlign = MinAlign(BaseAlign, Size);
1087 SDValue LoLoad = DAG.getExtLoad(Load->getExtensionType(), SL, LoVT,
1088 Load->getChain(), BasePtr, SrcValue, LoMemVT,
1089 BaseAlign, Load->getMemOperand()->getFlags());
1090 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
1091 DAG.getConstant(Size, SL, PtrVT));
1093 DAG.getExtLoad(Load->getExtensionType(), SL, HiVT, Load->getChain(),
1094 HiPtr, SrcValue.getWithOffset(LoMemVT.getStoreSize()),
1095 HiMemVT, HiAlign, Load->getMemOperand()->getFlags());
1098 DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, LoLoad, HiLoad),
1099 DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
1100 LoLoad.getValue(1), HiLoad.getValue(1))
1103 return DAG.getMergeValues(Ops, SL);
1106 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
1107 SelectionDAG &DAG) const {
1108 StoreSDNode *Store = cast<StoreSDNode>(Op);
1109 SDValue Val = Store->getValue();
1110 EVT VT = Val.getValueType();
1112 // If this is a 2 element vector, we really want to scalarize and not create
1113 // weird 1 element vectors.
1114 if (VT.getVectorNumElements() == 2)
1115 return scalarizeVectorStore(Store, DAG);
1117 EVT MemVT = Store->getMemoryVT();
1118 SDValue Chain = Store->getChain();
1119 SDValue BasePtr = Store->getBasePtr();
1123 EVT LoMemVT, HiMemVT;
1126 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1127 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1128 std::tie(Lo, Hi) = DAG.SplitVector(Val, SL, LoVT, HiVT);
1130 EVT PtrVT = BasePtr.getValueType();
1131 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
1132 DAG.getConstant(LoMemVT.getStoreSize(), SL,
1135 const MachinePointerInfo &SrcValue = Store->getMemOperand()->getPointerInfo();
1136 unsigned BaseAlign = Store->getAlignment();
1137 unsigned Size = LoMemVT.getStoreSize();
1138 unsigned HiAlign = MinAlign(BaseAlign, Size);
1141 DAG.getTruncStore(Chain, SL, Lo, BasePtr, SrcValue, LoMemVT, BaseAlign,
1142 Store->getMemOperand()->getFlags());
1144 DAG.getTruncStore(Chain, SL, Hi, HiPtr, SrcValue.getWithOffset(Size),
1145 HiMemVT, HiAlign, Store->getMemOperand()->getFlags());
1147 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoStore, HiStore);
1150 // This is a shortcut for integer division because we have fast i32<->f32
1151 // conversions, and fast f32 reciprocal instructions. The fractional part of a
1152 // float is enough to accurately represent up to a 24-bit signed integer.
1153 SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG,
1156 EVT VT = Op.getValueType();
1157 SDValue LHS = Op.getOperand(0);
1158 SDValue RHS = Op.getOperand(1);
1159 MVT IntVT = MVT::i32;
1160 MVT FltVT = MVT::f32;
1162 unsigned LHSSignBits = DAG.ComputeNumSignBits(LHS);
1163 if (LHSSignBits < 9)
1166 unsigned RHSSignBits = DAG.ComputeNumSignBits(RHS);
1167 if (RHSSignBits < 9)
1170 unsigned BitSize = VT.getSizeInBits();
1171 unsigned SignBits = std::min(LHSSignBits, RHSSignBits);
1172 unsigned DivBits = BitSize - SignBits;
1176 ISD::NodeType ToFp = Sign ? ISD::SINT_TO_FP : ISD::UINT_TO_FP;
1177 ISD::NodeType ToInt = Sign ? ISD::FP_TO_SINT : ISD::FP_TO_UINT;
1179 SDValue jq = DAG.getConstant(1, DL, IntVT);
1182 // char|short jq = ia ^ ib;
1183 jq = DAG.getNode(ISD::XOR, DL, VT, LHS, RHS);
1185 // jq = jq >> (bitsize - 2)
1186 jq = DAG.getNode(ISD::SRA, DL, VT, jq,
1187 DAG.getConstant(BitSize - 2, DL, VT));
1190 jq = DAG.getNode(ISD::OR, DL, VT, jq, DAG.getConstant(1, DL, VT));
1193 // int ia = (int)LHS;
1196 // int ib, (int)RHS;
1199 // float fa = (float)ia;
1200 SDValue fa = DAG.getNode(ToFp, DL, FltVT, ia);
1202 // float fb = (float)ib;
1203 SDValue fb = DAG.getNode(ToFp, DL, FltVT, ib);
1205 SDValue fq = DAG.getNode(ISD::FMUL, DL, FltVT,
1206 fa, DAG.getNode(AMDGPUISD::RCP, DL, FltVT, fb));
1209 fq = DAG.getNode(ISD::FTRUNC, DL, FltVT, fq);
1211 // float fqneg = -fq;
1212 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FltVT, fq);
1214 // float fr = mad(fqneg, fb, fa);
1215 SDValue fr = DAG.getNode(ISD::FMAD, DL, FltVT, fqneg, fb, fa);
1217 // int iq = (int)fq;
1218 SDValue iq = DAG.getNode(ToInt, DL, IntVT, fq);
1221 fr = DAG.getNode(ISD::FABS, DL, FltVT, fr);
1224 fb = DAG.getNode(ISD::FABS, DL, FltVT, fb);
1226 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
1228 // int cv = fr >= fb;
1229 SDValue cv = DAG.getSetCC(DL, SetCCVT, fr, fb, ISD::SETOGE);
1231 // jq = (cv ? jq : 0);
1232 jq = DAG.getNode(ISD::SELECT, DL, VT, cv, jq, DAG.getConstant(0, DL, VT));
1235 SDValue Div = DAG.getNode(ISD::ADD, DL, VT, iq, jq);
1237 // Rem needs compensation, it's easier to recompute it
1238 SDValue Rem = DAG.getNode(ISD::MUL, DL, VT, Div, RHS);
1239 Rem = DAG.getNode(ISD::SUB, DL, VT, LHS, Rem);
1241 // Truncate to number of bits this divide really is.
1244 = DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), DivBits));
1245 Div = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Div, InRegSize);
1246 Rem = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Rem, InRegSize);
1248 SDValue TruncMask = DAG.getConstant((UINT64_C(1) << DivBits) - 1, DL, VT);
1249 Div = DAG.getNode(ISD::AND, DL, VT, Div, TruncMask);
1250 Rem = DAG.getNode(ISD::AND, DL, VT, Rem, TruncMask);
1253 return DAG.getMergeValues({ Div, Rem }, DL);
1256 void AMDGPUTargetLowering::LowerUDIVREM64(SDValue Op,
1258 SmallVectorImpl<SDValue> &Results) const {
1259 assert(Op.getValueType() == MVT::i64);
1262 EVT VT = Op.getValueType();
1263 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1265 SDValue one = DAG.getConstant(1, DL, HalfVT);
1266 SDValue zero = DAG.getConstant(0, DL, HalfVT);
1269 SDValue LHS = Op.getOperand(0);
1270 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero);
1271 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one);
1273 SDValue RHS = Op.getOperand(1);
1274 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero);
1275 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one);
1277 if (VT == MVT::i64 &&
1278 DAG.MaskedValueIsZero(RHS, APInt::getHighBitsSet(64, 32)) &&
1279 DAG.MaskedValueIsZero(LHS, APInt::getHighBitsSet(64, 32))) {
1281 SDValue Res = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1284 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(0), zero});
1285 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(1), zero});
1287 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV));
1288 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM));
1292 // Get Speculative values
1293 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo);
1294 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo);
1296 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ);
1297 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {REM_Lo, zero});
1298 REM = DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM);
1300 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ);
1301 SDValue DIV_Lo = zero;
1303 const unsigned halfBitWidth = HalfVT.getSizeInBits();
1305 for (unsigned i = 0; i < halfBitWidth; ++i) {
1306 const unsigned bitPos = halfBitWidth - i - 1;
1307 SDValue POS = DAG.getConstant(bitPos, DL, HalfVT);
1308 // Get value of high bit
1309 SDValue HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS);
1310 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one);
1311 HBit = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, HBit);
1314 REM = DAG.getNode(ISD::SHL, DL, VT, REM, DAG.getConstant(1, DL, VT));
1316 REM = DAG.getNode(ISD::OR, DL, VT, REM, HBit);
1318 SDValue BIT = DAG.getConstant(1ULL << bitPos, DL, HalfVT);
1319 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETUGE);
1321 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT);
1324 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS);
1325 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETUGE);
1328 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {DIV_Lo, DIV_Hi});
1329 DIV = DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV);
1330 Results.push_back(DIV);
1331 Results.push_back(REM);
1334 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
1335 SelectionDAG &DAG) const {
1337 EVT VT = Op.getValueType();
1339 if (VT == MVT::i64) {
1340 SmallVector<SDValue, 2> Results;
1341 LowerUDIVREM64(Op, DAG, Results);
1342 return DAG.getMergeValues(Results, DL);
1345 if (VT == MVT::i32) {
1346 if (SDValue Res = LowerDIVREM24(Op, DAG, false))
1350 SDValue Num = Op.getOperand(0);
1351 SDValue Den = Op.getOperand(1);
1353 // RCP = URECIP(Den) = 2^32 / Den + e
1354 // e is rounding error.
1355 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
1357 // RCP_LO = mul(RCP, Den) */
1358 SDValue RCP_LO = DAG.getNode(ISD::MUL, DL, VT, RCP, Den);
1360 // RCP_HI = mulhu (RCP, Den) */
1361 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
1363 // NEG_RCP_LO = -RCP_LO
1364 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
1367 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
1368 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
1371 // Calculate the rounding error from the URECIP instruction
1372 // E = mulhu(ABS_RCP_LO, RCP)
1373 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
1375 // RCP_A_E = RCP + E
1376 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
1378 // RCP_S_E = RCP - E
1379 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
1381 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
1382 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
1385 // Quotient = mulhu(Tmp0, Num)
1386 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
1388 // Num_S_Remainder = Quotient * Den
1389 SDValue Num_S_Remainder = DAG.getNode(ISD::MUL, DL, VT, Quotient, Den);
1391 // Remainder = Num - Num_S_Remainder
1392 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
1394 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
1395 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
1396 DAG.getConstant(-1, DL, VT),
1397 DAG.getConstant(0, DL, VT),
1399 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
1400 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
1402 DAG.getConstant(-1, DL, VT),
1403 DAG.getConstant(0, DL, VT),
1405 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
1406 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
1409 // Calculate Division result:
1411 // Quotient_A_One = Quotient + 1
1412 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
1413 DAG.getConstant(1, DL, VT));
1415 // Quotient_S_One = Quotient - 1
1416 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
1417 DAG.getConstant(1, DL, VT));
1419 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
1420 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
1421 Quotient, Quotient_A_One, ISD::SETEQ);
1423 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
1424 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
1425 Quotient_S_One, Div, ISD::SETEQ);
1427 // Calculate Rem result:
1429 // Remainder_S_Den = Remainder - Den
1430 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
1432 // Remainder_A_Den = Remainder + Den
1433 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
1435 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
1436 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
1437 Remainder, Remainder_S_Den, ISD::SETEQ);
1439 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
1440 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
1441 Remainder_A_Den, Rem, ISD::SETEQ);
1446 return DAG.getMergeValues(Ops, DL);
1449 SDValue AMDGPUTargetLowering::LowerSDIVREM(SDValue Op,
1450 SelectionDAG &DAG) const {
1452 EVT VT = Op.getValueType();
1454 SDValue LHS = Op.getOperand(0);
1455 SDValue RHS = Op.getOperand(1);
1457 SDValue Zero = DAG.getConstant(0, DL, VT);
1458 SDValue NegOne = DAG.getConstant(-1, DL, VT);
1460 if (VT == MVT::i32) {
1461 if (SDValue Res = LowerDIVREM24(Op, DAG, true))
1465 if (VT == MVT::i64 &&
1466 DAG.ComputeNumSignBits(LHS) > 32 &&
1467 DAG.ComputeNumSignBits(RHS) > 32) {
1468 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1471 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero);
1472 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero);
1473 SDValue DIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1476 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(0)),
1477 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(1))
1479 return DAG.getMergeValues(Res, DL);
1482 SDValue LHSign = DAG.getSelectCC(DL, LHS, Zero, NegOne, Zero, ISD::SETLT);
1483 SDValue RHSign = DAG.getSelectCC(DL, RHS, Zero, NegOne, Zero, ISD::SETLT);
1484 SDValue DSign = DAG.getNode(ISD::XOR, DL, VT, LHSign, RHSign);
1485 SDValue RSign = LHSign; // Remainder sign is the same as LHS
1487 LHS = DAG.getNode(ISD::ADD, DL, VT, LHS, LHSign);
1488 RHS = DAG.getNode(ISD::ADD, DL, VT, RHS, RHSign);
1490 LHS = DAG.getNode(ISD::XOR, DL, VT, LHS, LHSign);
1491 RHS = DAG.getNode(ISD::XOR, DL, VT, RHS, RHSign);
1493 SDValue Div = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT), LHS, RHS);
1494 SDValue Rem = Div.getValue(1);
1496 Div = DAG.getNode(ISD::XOR, DL, VT, Div, DSign);
1497 Rem = DAG.getNode(ISD::XOR, DL, VT, Rem, RSign);
1499 Div = DAG.getNode(ISD::SUB, DL, VT, Div, DSign);
1500 Rem = DAG.getNode(ISD::SUB, DL, VT, Rem, RSign);
1506 return DAG.getMergeValues(Res, DL);
1509 // (frem x, y) -> (fsub x, (fmul (ftrunc (fdiv x, y)), y))
1510 SDValue AMDGPUTargetLowering::LowerFREM(SDValue Op, SelectionDAG &DAG) const {
1512 EVT VT = Op.getValueType();
1513 SDValue X = Op.getOperand(0);
1514 SDValue Y = Op.getOperand(1);
1516 // TODO: Should this propagate fast-math-flags?
1518 SDValue Div = DAG.getNode(ISD::FDIV, SL, VT, X, Y);
1519 SDValue Floor = DAG.getNode(ISD::FTRUNC, SL, VT, Div);
1520 SDValue Mul = DAG.getNode(ISD::FMUL, SL, VT, Floor, Y);
1522 return DAG.getNode(ISD::FSUB, SL, VT, X, Mul);
1525 SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const {
1527 SDValue Src = Op.getOperand(0);
1529 // result = trunc(src)
1530 // if (src > 0.0 && src != result)
1533 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1535 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1536 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
1539 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
1541 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT);
1542 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1543 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1545 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero);
1546 // TODO: Should this propagate fast-math-flags?
1547 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1550 static SDValue extractF64Exponent(SDValue Hi, const SDLoc &SL,
1551 SelectionDAG &DAG) {
1552 const unsigned FractBits = 52;
1553 const unsigned ExpBits = 11;
1555 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32,
1557 DAG.getConstant(FractBits - 32, SL, MVT::i32),
1558 DAG.getConstant(ExpBits, SL, MVT::i32));
1559 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart,
1560 DAG.getConstant(1023, SL, MVT::i32));
1565 SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const {
1567 SDValue Src = Op.getOperand(0);
1569 assert(Op.getValueType() == MVT::f64);
1571 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1572 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1574 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1576 // Extract the upper half, since this is where we will find the sign and
1578 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One);
1580 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
1582 const unsigned FractBits = 52;
1584 // Extract the sign bit.
1585 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, SL, MVT::i32);
1586 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask);
1588 // Extend back to to 64-bits.
1589 SDValue SignBit64 = DAG.getBuildVector(MVT::v2i32, SL, {Zero, SignBit});
1590 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64);
1592 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src);
1593 const SDValue FractMask
1594 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, SL, MVT::i64);
1596 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp);
1597 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64);
1598 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not);
1601 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
1603 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, SL, MVT::i32);
1605 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1606 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1608 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0);
1609 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1);
1611 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2);
1614 SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const {
1616 SDValue Src = Op.getOperand(0);
1618 assert(Op.getValueType() == MVT::f64);
1620 APFloat C1Val(APFloat::IEEEdouble(), "0x1.0p+52");
1621 SDValue C1 = DAG.getConstantFP(C1Val, SL, MVT::f64);
1622 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src);
1624 // TODO: Should this propagate fast-math-flags?
1626 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign);
1627 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign);
1629 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src);
1631 APFloat C2Val(APFloat::IEEEdouble(), "0x1.fffffffffffffp+51");
1632 SDValue C2 = DAG.getConstantFP(C2Val, SL, MVT::f64);
1635 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
1636 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT);
1638 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2);
1641 SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const {
1642 // FNEARBYINT and FRINT are the same, except in their handling of FP
1643 // exceptions. Those aren't really meaningful for us, and OpenCL only has
1644 // rint, so just treat them as equivalent.
1645 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0));
1648 // XXX - May require not supporting f32 denormals?
1649 SDValue AMDGPUTargetLowering::LowerFROUND32(SDValue Op, SelectionDAG &DAG) const {
1651 SDValue X = Op.getOperand(0);
1653 SDValue T = DAG.getNode(ISD::FTRUNC, SL, MVT::f32, X);
1655 // TODO: Should this propagate fast-math-flags?
1657 SDValue Diff = DAG.getNode(ISD::FSUB, SL, MVT::f32, X, T);
1659 SDValue AbsDiff = DAG.getNode(ISD::FABS, SL, MVT::f32, Diff);
1661 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f32);
1662 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
1663 const SDValue Half = DAG.getConstantFP(0.5, SL, MVT::f32);
1665 SDValue SignOne = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f32, One, X);
1668 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f32);
1670 SDValue Cmp = DAG.getSetCC(SL, SetCCVT, AbsDiff, Half, ISD::SETOGE);
1672 SDValue Sel = DAG.getNode(ISD::SELECT, SL, MVT::f32, Cmp, SignOne, Zero);
1674 return DAG.getNode(ISD::FADD, SL, MVT::f32, T, Sel);
1677 SDValue AMDGPUTargetLowering::LowerFROUND64(SDValue Op, SelectionDAG &DAG) const {
1679 SDValue X = Op.getOperand(0);
1681 SDValue L = DAG.getNode(ISD::BITCAST, SL, MVT::i64, X);
1683 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1684 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1685 const SDValue NegOne = DAG.getConstant(-1, SL, MVT::i32);
1686 const SDValue FiftyOne = DAG.getConstant(51, SL, MVT::i32);
1688 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
1690 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1692 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, One);
1694 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
1696 const SDValue Mask = DAG.getConstant(INT64_C(0x000fffffffffffff), SL,
1699 SDValue M = DAG.getNode(ISD::SRA, SL, MVT::i64, Mask, Exp);
1700 SDValue D = DAG.getNode(ISD::SRA, SL, MVT::i64,
1701 DAG.getConstant(INT64_C(0x0008000000000000), SL,
1705 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, L, M);
1706 SDValue Tmp1 = DAG.getSetCC(SL, SetCCVT,
1707 DAG.getConstant(0, SL, MVT::i64), Tmp0,
1710 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, Tmp1,
1711 D, DAG.getConstant(0, SL, MVT::i64));
1712 SDValue K = DAG.getNode(ISD::ADD, SL, MVT::i64, L, Tmp2);
1714 K = DAG.getNode(ISD::AND, SL, MVT::i64, K, DAG.getNOT(SL, M, MVT::i64));
1715 K = DAG.getNode(ISD::BITCAST, SL, MVT::f64, K);
1717 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1718 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1719 SDValue ExpEqNegOne = DAG.getSetCC(SL, SetCCVT, NegOne, Exp, ISD::SETEQ);
1721 SDValue Mag = DAG.getNode(ISD::SELECT, SL, MVT::f64,
1723 DAG.getConstantFP(1.0, SL, MVT::f64),
1724 DAG.getConstantFP(0.0, SL, MVT::f64));
1726 SDValue S = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, Mag, X);
1728 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpLt0, S, K);
1729 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpGt51, X, K);
1734 SDValue AMDGPUTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const {
1735 EVT VT = Op.getValueType();
1738 return LowerFROUND32(Op, DAG);
1741 return LowerFROUND64(Op, DAG);
1743 llvm_unreachable("unhandled type");
1746 SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const {
1748 SDValue Src = Op.getOperand(0);
1750 // result = trunc(src);
1751 // if (src < 0.0 && src != result)
1754 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1756 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1757 const SDValue NegOne = DAG.getConstantFP(-1.0, SL, MVT::f64);
1760 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
1762 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT);
1763 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1764 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1766 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero);
1767 // TODO: Should this propagate fast-math-flags?
1768 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1771 SDValue AMDGPUTargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
1773 SDValue Src = Op.getOperand(0);
1774 bool ZeroUndef = Op.getOpcode() == ISD::CTLZ_ZERO_UNDEF;
1776 if (ZeroUndef && Src.getValueType() == MVT::i32)
1777 return DAG.getNode(AMDGPUISD::FFBH_U32, SL, MVT::i32, Src);
1779 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1781 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1782 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1784 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1785 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1787 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1788 *DAG.getContext(), MVT::i32);
1790 SDValue Hi0 = DAG.getSetCC(SL, SetCCVT, Hi, Zero, ISD::SETEQ);
1792 SDValue CtlzLo = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Lo);
1793 SDValue CtlzHi = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Hi);
1795 const SDValue Bits32 = DAG.getConstant(32, SL, MVT::i32);
1796 SDValue Add = DAG.getNode(ISD::ADD, SL, MVT::i32, CtlzLo, Bits32);
1798 // ctlz(x) = hi_32(x) == 0 ? ctlz(lo_32(x)) + 32 : ctlz(hi_32(x))
1799 SDValue NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0, Add, CtlzHi);
1802 // Test if the full 64-bit input is zero.
1804 // FIXME: DAG combines turn what should be an s_and_b64 into a v_or_b32,
1805 // which we probably don't want.
1806 SDValue Lo0 = DAG.getSetCC(SL, SetCCVT, Lo, Zero, ISD::SETEQ);
1807 SDValue SrcIsZero = DAG.getNode(ISD::AND, SL, SetCCVT, Lo0, Hi0);
1809 // TODO: If i64 setcc is half rate, it can result in 1 fewer instruction
1810 // with the same cycles, otherwise it is slower.
1811 // SDValue SrcIsZero = DAG.getSetCC(SL, SetCCVT, Src,
1812 // DAG.getConstant(0, SL, MVT::i64), ISD::SETEQ);
1814 const SDValue Bits32 = DAG.getConstant(64, SL, MVT::i32);
1816 // The instruction returns -1 for 0 input, but the defined intrinsic
1817 // behavior is to return the number of bits.
1818 NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32,
1819 SrcIsZero, Bits32, NewCtlz);
1822 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, NewCtlz);
1825 SDValue AMDGPUTargetLowering::LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG,
1826 bool Signed) const {
1830 // uint lz = clz(u);
1831 // uint e = (u != 0) ? 127U + 63U - lz : 0;
1832 // u = (u << lz) & 0x7fffffffffffffffUL;
1833 // ulong t = u & 0xffffffffffUL;
1834 // uint v = (e << 23) | (uint)(u >> 40);
1835 // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U);
1836 // return as_float(v + r);
1841 // long s = l >> 63;
1842 // float r = cul2f((l + s) ^ s);
1843 // return s ? -r : r;
1847 SDValue Src = Op.getOperand(0);
1852 const SDValue SignBit = DAG.getConstant(63, SL, MVT::i64);
1853 S = DAG.getNode(ISD::SRA, SL, MVT::i64, L, SignBit);
1855 SDValue LPlusS = DAG.getNode(ISD::ADD, SL, MVT::i64, L, S);
1856 L = DAG.getNode(ISD::XOR, SL, MVT::i64, LPlusS, S);
1859 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1860 *DAG.getContext(), MVT::f32);
1863 SDValue ZeroI32 = DAG.getConstant(0, SL, MVT::i32);
1864 SDValue ZeroI64 = DAG.getConstant(0, SL, MVT::i64);
1865 SDValue LZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i64, L);
1866 LZ = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LZ);
1868 SDValue K = DAG.getConstant(127U + 63U, SL, MVT::i32);
1869 SDValue E = DAG.getSelect(SL, MVT::i32,
1870 DAG.getSetCC(SL, SetCCVT, L, ZeroI64, ISD::SETNE),
1871 DAG.getNode(ISD::SUB, SL, MVT::i32, K, LZ),
1874 SDValue U = DAG.getNode(ISD::AND, SL, MVT::i64,
1875 DAG.getNode(ISD::SHL, SL, MVT::i64, L, LZ),
1876 DAG.getConstant((-1ULL) >> 1, SL, MVT::i64));
1878 SDValue T = DAG.getNode(ISD::AND, SL, MVT::i64, U,
1879 DAG.getConstant(0xffffffffffULL, SL, MVT::i64));
1881 SDValue UShl = DAG.getNode(ISD::SRL, SL, MVT::i64,
1882 U, DAG.getConstant(40, SL, MVT::i64));
1884 SDValue V = DAG.getNode(ISD::OR, SL, MVT::i32,
1885 DAG.getNode(ISD::SHL, SL, MVT::i32, E, DAG.getConstant(23, SL, MVT::i32)),
1886 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, UShl));
1888 SDValue C = DAG.getConstant(0x8000000000ULL, SL, MVT::i64);
1889 SDValue RCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETUGT);
1890 SDValue TCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETEQ);
1892 SDValue One = DAG.getConstant(1, SL, MVT::i32);
1894 SDValue VTrunc1 = DAG.getNode(ISD::AND, SL, MVT::i32, V, One);
1896 SDValue R = DAG.getSelect(SL, MVT::i32,
1899 DAG.getSelect(SL, MVT::i32, TCmp, VTrunc1, ZeroI32));
1900 R = DAG.getNode(ISD::ADD, SL, MVT::i32, V, R);
1901 R = DAG.getNode(ISD::BITCAST, SL, MVT::f32, R);
1906 SDValue RNeg = DAG.getNode(ISD::FNEG, SL, MVT::f32, R);
1907 return DAG.getSelect(SL, MVT::f32, DAG.getSExtOrTrunc(S, SL, SetCCVT), RNeg, R);
1910 SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG,
1911 bool Signed) const {
1913 SDValue Src = Op.getOperand(0);
1915 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1917 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
1918 DAG.getConstant(0, SL, MVT::i32));
1919 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
1920 DAG.getConstant(1, SL, MVT::i32));
1922 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
1925 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo);
1927 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi,
1928 DAG.getConstant(32, SL, MVT::i32));
1929 // TODO: Should this propagate fast-math-flags?
1930 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo);
1933 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
1934 SelectionDAG &DAG) const {
1935 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
1936 "operation should be legal");
1938 // TODO: Factor out code common with LowerSINT_TO_FP.
1940 EVT DestVT = Op.getValueType();
1941 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
1943 SDValue Src = Op.getOperand(0);
1945 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
1946 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
1948 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
1953 if (DestVT == MVT::f32)
1954 return LowerINT_TO_FP32(Op, DAG, false);
1956 assert(DestVT == MVT::f64);
1957 return LowerINT_TO_FP64(Op, DAG, false);
1960 SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op,
1961 SelectionDAG &DAG) const {
1962 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
1963 "operation should be legal");
1965 // TODO: Factor out code common with LowerUINT_TO_FP.
1967 EVT DestVT = Op.getValueType();
1968 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
1970 SDValue Src = Op.getOperand(0);
1972 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
1973 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
1975 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
1980 if (DestVT == MVT::f32)
1981 return LowerINT_TO_FP32(Op, DAG, true);
1983 assert(DestVT == MVT::f64);
1984 return LowerINT_TO_FP64(Op, DAG, true);
1987 SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG,
1988 bool Signed) const {
1991 SDValue Src = Op.getOperand(0);
1993 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1995 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL,
1997 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL,
1999 // TODO: Should this propagate fast-math-flags?
2000 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0);
2002 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul);
2005 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc);
2007 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL,
2008 MVT::i32, FloorMul);
2009 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma);
2011 SDValue Result = DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi});
2013 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result);
2016 SDValue AMDGPUTargetLowering::LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const {
2018 if (getTargetMachine().Options.UnsafeFPMath) {
2019 // There is a generic expand for FP_TO_FP16 with unsafe fast math.
2024 SDValue N0 = Op.getOperand(0);
2025 assert (N0.getSimpleValueType() == MVT::f64);
2027 // f64 -> f16 conversion using round-to-nearest-even rounding mode.
2028 const unsigned ExpMask = 0x7ff;
2029 const unsigned ExpBiasf64 = 1023;
2030 const unsigned ExpBiasf16 = 15;
2031 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2032 SDValue One = DAG.getConstant(1, DL, MVT::i32);
2033 SDValue U = DAG.getNode(ISD::BITCAST, DL, MVT::i64, N0);
2034 SDValue UH = DAG.getNode(ISD::SRL, DL, MVT::i64, U,
2035 DAG.getConstant(32, DL, MVT::i64));
2036 UH = DAG.getZExtOrTrunc(UH, DL, MVT::i32);
2037 U = DAG.getZExtOrTrunc(U, DL, MVT::i32);
2038 SDValue E = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2039 DAG.getConstant(20, DL, MVT::i64));
2040 E = DAG.getNode(ISD::AND, DL, MVT::i32, E,
2041 DAG.getConstant(ExpMask, DL, MVT::i32));
2042 // Subtract the fp64 exponent bias (1023) to get the real exponent and
2043 // add the f16 bias (15) to get the biased exponent for the f16 format.
2044 E = DAG.getNode(ISD::ADD, DL, MVT::i32, E,
2045 DAG.getConstant(-ExpBiasf64 + ExpBiasf16, DL, MVT::i32));
2047 SDValue M = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2048 DAG.getConstant(8, DL, MVT::i32));
2049 M = DAG.getNode(ISD::AND, DL, MVT::i32, M,
2050 DAG.getConstant(0xffe, DL, MVT::i32));
2052 SDValue MaskedSig = DAG.getNode(ISD::AND, DL, MVT::i32, UH,
2053 DAG.getConstant(0x1ff, DL, MVT::i32));
2054 MaskedSig = DAG.getNode(ISD::OR, DL, MVT::i32, MaskedSig, U);
2056 SDValue Lo40Set = DAG.getSelectCC(DL, MaskedSig, Zero, Zero, One, ISD::SETEQ);
2057 M = DAG.getNode(ISD::OR, DL, MVT::i32, M, Lo40Set);
2059 // (M != 0 ? 0x0200 : 0) | 0x7c00;
2060 SDValue I = DAG.getNode(ISD::OR, DL, MVT::i32,
2061 DAG.getSelectCC(DL, M, Zero, DAG.getConstant(0x0200, DL, MVT::i32),
2062 Zero, ISD::SETNE), DAG.getConstant(0x7c00, DL, MVT::i32));
2064 // N = M | (E << 12);
2065 SDValue N = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2066 DAG.getNode(ISD::SHL, DL, MVT::i32, E,
2067 DAG.getConstant(12, DL, MVT::i32)));
2069 // B = clamp(1-E, 0, 13);
2070 SDValue OneSubExp = DAG.getNode(ISD::SUB, DL, MVT::i32,
2072 SDValue B = DAG.getNode(ISD::SMAX, DL, MVT::i32, OneSubExp, Zero);
2073 B = DAG.getNode(ISD::SMIN, DL, MVT::i32, B,
2074 DAG.getConstant(13, DL, MVT::i32));
2076 SDValue SigSetHigh = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2077 DAG.getConstant(0x1000, DL, MVT::i32));
2079 SDValue D = DAG.getNode(ISD::SRL, DL, MVT::i32, SigSetHigh, B);
2080 SDValue D0 = DAG.getNode(ISD::SHL, DL, MVT::i32, D, B);
2081 SDValue D1 = DAG.getSelectCC(DL, D0, SigSetHigh, One, Zero, ISD::SETNE);
2082 D = DAG.getNode(ISD::OR, DL, MVT::i32, D, D1);
2084 SDValue V = DAG.getSelectCC(DL, E, One, D, N, ISD::SETLT);
2085 SDValue VLow3 = DAG.getNode(ISD::AND, DL, MVT::i32, V,
2086 DAG.getConstant(0x7, DL, MVT::i32));
2087 V = DAG.getNode(ISD::SRL, DL, MVT::i32, V,
2088 DAG.getConstant(2, DL, MVT::i32));
2089 SDValue V0 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(3, DL, MVT::i32),
2090 One, Zero, ISD::SETEQ);
2091 SDValue V1 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(5, DL, MVT::i32),
2092 One, Zero, ISD::SETGT);
2093 V1 = DAG.getNode(ISD::OR, DL, MVT::i32, V0, V1);
2094 V = DAG.getNode(ISD::ADD, DL, MVT::i32, V, V1);
2096 V = DAG.getSelectCC(DL, E, DAG.getConstant(30, DL, MVT::i32),
2097 DAG.getConstant(0x7c00, DL, MVT::i32), V, ISD::SETGT);
2098 V = DAG.getSelectCC(DL, E, DAG.getConstant(1039, DL, MVT::i32),
2101 // Extract the sign bit.
2102 SDValue Sign = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2103 DAG.getConstant(16, DL, MVT::i32));
2104 Sign = DAG.getNode(ISD::AND, DL, MVT::i32, Sign,
2105 DAG.getConstant(0x8000, DL, MVT::i32));
2107 V = DAG.getNode(ISD::OR, DL, MVT::i32, Sign, V);
2108 return DAG.getZExtOrTrunc(V, DL, Op.getValueType());
2111 SDValue AMDGPUTargetLowering::LowerFP_TO_SINT(SDValue Op,
2112 SelectionDAG &DAG) const {
2113 SDValue Src = Op.getOperand(0);
2115 // TODO: Factor out code common with LowerFP_TO_UINT.
2117 EVT SrcVT = Src.getValueType();
2118 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2121 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2123 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2128 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2129 return LowerFP64_TO_INT(Op, DAG, true);
2134 SDValue AMDGPUTargetLowering::LowerFP_TO_UINT(SDValue Op,
2135 SelectionDAG &DAG) const {
2136 SDValue Src = Op.getOperand(0);
2138 // TODO: Factor out code common with LowerFP_TO_SINT.
2140 EVT SrcVT = Src.getValueType();
2141 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2144 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2146 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2151 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2152 return LowerFP64_TO_INT(Op, DAG, false);
2157 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
2158 SelectionDAG &DAG) const {
2159 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2160 MVT VT = Op.getSimpleValueType();
2161 MVT ScalarVT = VT.getScalarType();
2163 assert(VT.isVector());
2165 SDValue Src = Op.getOperand(0);
2168 // TODO: Don't scalarize on Evergreen?
2169 unsigned NElts = VT.getVectorNumElements();
2170 SmallVector<SDValue, 8> Args;
2171 DAG.ExtractVectorElements(Src, Args, 0, NElts);
2173 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType());
2174 for (unsigned I = 0; I < NElts; ++I)
2175 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp);
2177 return DAG.getBuildVector(VT, DL, Args);
2180 //===----------------------------------------------------------------------===//
2181 // Custom DAG optimizations
2182 //===----------------------------------------------------------------------===//
2184 static bool isU24(SDValue Op, SelectionDAG &DAG) {
2185 APInt KnownZero, KnownOne;
2186 EVT VT = Op.getValueType();
2187 DAG.computeKnownBits(Op, KnownZero, KnownOne);
2189 return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24;
2192 static bool isI24(SDValue Op, SelectionDAG &DAG) {
2193 EVT VT = Op.getValueType();
2195 // In order for this to be a signed 24-bit value, bit 23, must
2197 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
2198 // as unsigned 24-bit values.
2199 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24;
2202 static bool simplifyI24(SDNode *Node24, unsigned OpIdx,
2203 TargetLowering::DAGCombinerInfo &DCI) {
2205 SelectionDAG &DAG = DCI.DAG;
2206 SDValue Op = Node24->getOperand(OpIdx);
2207 EVT VT = Op.getValueType();
2209 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24);
2210 APInt KnownZero, KnownOne;
2211 TargetLowering::TargetLoweringOpt TLO(DAG, true, true);
2212 if (TLO.SimplifyDemandedBits(Node24, OpIdx, Demanded, DCI))
2218 template <typename IntTy>
2219 static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, uint32_t Offset,
2220 uint32_t Width, const SDLoc &DL) {
2221 if (Width + Offset < 32) {
2222 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width);
2223 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width);
2224 return DAG.getConstant(Result, DL, MVT::i32);
2227 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32);
2230 static bool hasVolatileUser(SDNode *Val) {
2231 for (SDNode *U : Val->uses()) {
2232 if (MemSDNode *M = dyn_cast<MemSDNode>(U)) {
2233 if (M->isVolatile())
2241 bool AMDGPUTargetLowering::shouldCombineMemoryType(EVT VT) const {
2242 // i32 vectors are the canonical memory type.
2243 if (VT.getScalarType() == MVT::i32 || isTypeLegal(VT))
2246 if (!VT.isByteSized())
2249 unsigned Size = VT.getStoreSize();
2251 if ((Size == 1 || Size == 2 || Size == 4) && !VT.isVector())
2254 if (Size == 3 || (Size > 4 && (Size % 4 != 0)))
2260 // Replace load of an illegal type with a store of a bitcast to a friendlier
2262 SDValue AMDGPUTargetLowering::performLoadCombine(SDNode *N,
2263 DAGCombinerInfo &DCI) const {
2264 if (!DCI.isBeforeLegalize())
2267 LoadSDNode *LN = cast<LoadSDNode>(N);
2268 if (LN->isVolatile() || !ISD::isNormalLoad(LN) || hasVolatileUser(LN))
2272 SelectionDAG &DAG = DCI.DAG;
2273 EVT VT = LN->getMemoryVT();
2275 unsigned Size = VT.getStoreSize();
2276 unsigned Align = LN->getAlignment();
2277 if (Align < Size && isTypeLegal(VT)) {
2279 unsigned AS = LN->getAddressSpace();
2281 // Expand unaligned loads earlier than legalization. Due to visitation order
2282 // problems during legalization, the emitted instructions to pack and unpack
2283 // the bytes again are not eliminated in the case of an unaligned copy.
2284 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
2286 return scalarizeVectorLoad(LN, DAG);
2289 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(LN, DAG);
2290 return DAG.getMergeValues(Ops, SDLoc(N));
2297 if (!shouldCombineMemoryType(VT))
2300 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
2303 = DAG.getLoad(NewVT, SL, LN->getChain(),
2304 LN->getBasePtr(), LN->getMemOperand());
2306 SDValue BC = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad);
2307 DCI.CombineTo(N, BC, NewLoad.getValue(1));
2308 return SDValue(N, 0);
2311 // Replace store of an illegal type with a store of a bitcast to a friendlier
2313 SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N,
2314 DAGCombinerInfo &DCI) const {
2315 if (!DCI.isBeforeLegalize())
2318 StoreSDNode *SN = cast<StoreSDNode>(N);
2319 if (SN->isVolatile() || !ISD::isNormalStore(SN))
2322 EVT VT = SN->getMemoryVT();
2323 unsigned Size = VT.getStoreSize();
2326 SelectionDAG &DAG = DCI.DAG;
2327 unsigned Align = SN->getAlignment();
2328 if (Align < Size && isTypeLegal(VT)) {
2330 unsigned AS = SN->getAddressSpace();
2332 // Expand unaligned stores earlier than legalization. Due to visitation
2333 // order problems during legalization, the emitted instructions to pack and
2334 // unpack the bytes again are not eliminated in the case of an unaligned
2336 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
2338 return scalarizeVectorStore(SN, DAG);
2340 return expandUnalignedStore(SN, DAG);
2347 if (!shouldCombineMemoryType(VT))
2350 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
2351 SDValue Val = SN->getValue();
2353 //DCI.AddToWorklist(Val.getNode());
2355 bool OtherUses = !Val.hasOneUse();
2356 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NewVT, Val);
2358 SDValue CastBack = DAG.getNode(ISD::BITCAST, SL, VT, CastVal);
2359 DAG.ReplaceAllUsesOfValueWith(Val, CastBack);
2362 return DAG.getStore(SN->getChain(), SL, CastVal,
2363 SN->getBasePtr(), SN->getMemOperand());
2366 /// Split the 64-bit value \p LHS into two 32-bit components, and perform the
2367 /// binary operation \p Opc to it with the corresponding constant operands.
2368 SDValue AMDGPUTargetLowering::splitBinaryBitConstantOpImpl(
2369 DAGCombinerInfo &DCI, const SDLoc &SL,
2370 unsigned Opc, SDValue LHS,
2371 uint32_t ValLo, uint32_t ValHi) const {
2372 SelectionDAG &DAG = DCI.DAG;
2374 std::tie(Lo, Hi) = split64BitValue(LHS, DAG);
2376 SDValue LoRHS = DAG.getConstant(ValLo, SL, MVT::i32);
2377 SDValue HiRHS = DAG.getConstant(ValHi, SL, MVT::i32);
2379 SDValue LoAnd = DAG.getNode(Opc, SL, MVT::i32, Lo, LoRHS);
2380 SDValue HiAnd = DAG.getNode(Opc, SL, MVT::i32, Hi, HiRHS);
2382 // Re-visit the ands. It's possible we eliminated one of them and it could
2383 // simplify the vector.
2384 DCI.AddToWorklist(Lo.getNode());
2385 DCI.AddToWorklist(Hi.getNode());
2387 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd});
2388 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2391 SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N,
2392 DAGCombinerInfo &DCI) const {
2393 if (N->getValueType(0) != MVT::i64)
2396 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32))
2398 // On some subtargets, 64-bit shift is a quarter rate instruction. In the
2399 // common case, splitting this into a move and a 32-bit shift is faster and
2400 // the same code size.
2401 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2405 unsigned RHSVal = RHS->getZExtValue();
2409 SDValue LHS = N->getOperand(0);
2412 SelectionDAG &DAG = DCI.DAG;
2414 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32);
2416 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS);
2417 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt);
2419 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
2421 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift});
2422 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2425 SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N,
2426 DAGCombinerInfo &DCI) const {
2427 if (N->getValueType(0) != MVT::i64)
2430 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2434 SelectionDAG &DAG = DCI.DAG;
2436 unsigned RHSVal = RHS->getZExtValue();
2438 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31)
2440 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2441 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2442 DAG.getConstant(31, SL, MVT::i32));
2444 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift});
2445 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2448 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31)
2450 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2451 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2452 DAG.getConstant(31, SL, MVT::i32));
2453 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift});
2454 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2460 SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N,
2461 DAGCombinerInfo &DCI) const {
2462 if (N->getValueType(0) != MVT::i64)
2465 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2469 unsigned ShiftAmt = RHS->getZExtValue();
2473 // srl i64:x, C for C >= 32
2475 // build_pair (srl hi_32(x), C - 32), 0
2477 SelectionDAG &DAG = DCI.DAG;
2480 SDValue One = DAG.getConstant(1, SL, MVT::i32);
2481 SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
2483 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, N->getOperand(0));
2484 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32,
2487 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32);
2488 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst);
2490 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero});
2492 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair);
2495 // We need to specifically handle i64 mul here to avoid unnecessary conversion
2496 // instructions. If we only match on the legalized i64 mul expansion,
2497 // SimplifyDemandedBits will be unable to remove them because there will be
2498 // multiple uses due to the separate mul + mulh[su].
2499 static SDValue getMul24(SelectionDAG &DAG, const SDLoc &SL,
2500 SDValue N0, SDValue N1, unsigned Size, bool Signed) {
2502 unsigned MulOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2503 return DAG.getNode(MulOpc, SL, MVT::i32, N0, N1);
2506 // Because we want to eliminate extension instructions before the
2507 // operation, we need to create a single user here (i.e. not the separate
2508 // mul_lo + mul_hi) so that SimplifyDemandedBits will deal with it.
2510 unsigned MulOpc = Signed ? AMDGPUISD::MUL_LOHI_I24 : AMDGPUISD::MUL_LOHI_U24;
2512 SDValue Mul = DAG.getNode(MulOpc, SL,
2513 DAG.getVTList(MVT::i32, MVT::i32), N0, N1);
2515 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64,
2516 Mul.getValue(0), Mul.getValue(1));
2519 SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N,
2520 DAGCombinerInfo &DCI) const {
2521 EVT VT = N->getValueType(0);
2523 unsigned Size = VT.getSizeInBits();
2524 if (VT.isVector() || Size > 64)
2527 // There are i16 integer mul/mad.
2528 if (Subtarget->has16BitInsts() && VT.getScalarType().bitsLE(MVT::i16))
2531 SelectionDAG &DAG = DCI.DAG;
2534 SDValue N0 = N->getOperand(0);
2535 SDValue N1 = N->getOperand(1);
2538 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) {
2539 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2540 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
2541 Mul = getMul24(DAG, DL, N0, N1, Size, false);
2542 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) {
2543 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2544 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
2545 Mul = getMul24(DAG, DL, N0, N1, Size, true);
2550 // We need to use sext even for MUL_U24, because MUL_U24 is used
2551 // for signed multiply of 8 and 16-bit types.
2552 return DAG.getSExtOrTrunc(Mul, DL, VT);
2555 SDValue AMDGPUTargetLowering::performMulhsCombine(SDNode *N,
2556 DAGCombinerInfo &DCI) const {
2557 EVT VT = N->getValueType(0);
2559 if (!Subtarget->hasMulI24() || VT.isVector())
2562 SelectionDAG &DAG = DCI.DAG;
2565 SDValue N0 = N->getOperand(0);
2566 SDValue N1 = N->getOperand(1);
2568 if (!isI24(N0, DAG) || !isI24(N1, DAG))
2571 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2572 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
2574 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_I24, DL, MVT::i32, N0, N1);
2575 DCI.AddToWorklist(Mulhi.getNode());
2576 return DAG.getSExtOrTrunc(Mulhi, DL, VT);
2579 SDValue AMDGPUTargetLowering::performMulhuCombine(SDNode *N,
2580 DAGCombinerInfo &DCI) const {
2581 EVT VT = N->getValueType(0);
2583 if (!Subtarget->hasMulU24() || VT.isVector() || VT.getSizeInBits() > 32)
2586 SelectionDAG &DAG = DCI.DAG;
2589 SDValue N0 = N->getOperand(0);
2590 SDValue N1 = N->getOperand(1);
2592 if (!isU24(N0, DAG) || !isU24(N1, DAG))
2595 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2596 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
2598 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_U24, DL, MVT::i32, N0, N1);
2599 DCI.AddToWorklist(Mulhi.getNode());
2600 return DAG.getZExtOrTrunc(Mulhi, DL, VT);
2603 SDValue AMDGPUTargetLowering::performMulLoHi24Combine(
2604 SDNode *N, DAGCombinerInfo &DCI) const {
2605 SelectionDAG &DAG = DCI.DAG;
2607 // Simplify demanded bits before splitting into multiple users.
2608 if (simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI))
2611 SDValue N0 = N->getOperand(0);
2612 SDValue N1 = N->getOperand(1);
2614 bool Signed = (N->getOpcode() == AMDGPUISD::MUL_LOHI_I24);
2616 unsigned MulLoOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2617 unsigned MulHiOpc = Signed ? AMDGPUISD::MULHI_I24 : AMDGPUISD::MULHI_U24;
2621 SDValue MulLo = DAG.getNode(MulLoOpc, SL, MVT::i32, N0, N1);
2622 SDValue MulHi = DAG.getNode(MulHiOpc, SL, MVT::i32, N0, N1);
2623 return DAG.getMergeValues({ MulLo, MulHi }, SL);
2626 static bool isNegativeOne(SDValue Val) {
2627 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val))
2628 return C->isAllOnesValue();
2632 static bool isCtlzOpc(unsigned Opc) {
2633 return Opc == ISD::CTLZ || Opc == ISD::CTLZ_ZERO_UNDEF;
2636 SDValue AMDGPUTargetLowering::getFFBH_U32(SelectionDAG &DAG,
2638 const SDLoc &DL) const {
2639 EVT VT = Op.getValueType();
2640 EVT LegalVT = getTypeToTransformTo(*DAG.getContext(), VT);
2641 if (LegalVT != MVT::i32 && (Subtarget->has16BitInsts() &&
2642 LegalVT != MVT::i16))
2646 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Op);
2648 SDValue FFBH = DAG.getNode(AMDGPUISD::FFBH_U32, DL, MVT::i32, Op);
2650 FFBH = DAG.getNode(ISD::TRUNCATE, DL, VT, FFBH);
2655 // The native instructions return -1 on 0 input. Optimize out a select that
2656 // produces -1 on 0.
2658 // TODO: If zero is not undef, we could also do this if the output is compared
2659 // against the bitwidth.
2661 // TODO: Should probably combine against FFBH_U32 instead of ctlz directly.
2662 SDValue AMDGPUTargetLowering::performCtlzCombine(const SDLoc &SL, SDValue Cond,
2663 SDValue LHS, SDValue RHS,
2664 DAGCombinerInfo &DCI) const {
2665 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2666 if (!CmpRhs || !CmpRhs->isNullValue())
2669 SelectionDAG &DAG = DCI.DAG;
2670 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
2671 SDValue CmpLHS = Cond.getOperand(0);
2673 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x
2674 if (CCOpcode == ISD::SETEQ &&
2675 isCtlzOpc(RHS.getOpcode()) &&
2676 RHS.getOperand(0) == CmpLHS &&
2677 isNegativeOne(LHS)) {
2678 return getFFBH_U32(DAG, CmpLHS, SL);
2681 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x
2682 if (CCOpcode == ISD::SETNE &&
2683 isCtlzOpc(LHS.getOpcode()) &&
2684 LHS.getOperand(0) == CmpLHS &&
2685 isNegativeOne(RHS)) {
2686 return getFFBH_U32(DAG, CmpLHS, SL);
2692 SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N,
2693 DAGCombinerInfo &DCI) const {
2694 SDValue Cond = N->getOperand(0);
2695 if (Cond.getOpcode() != ISD::SETCC)
2698 EVT VT = N->getValueType(0);
2699 SDValue LHS = Cond.getOperand(0);
2700 SDValue RHS = Cond.getOperand(1);
2701 SDValue CC = Cond.getOperand(2);
2703 SDValue True = N->getOperand(1);
2704 SDValue False = N->getOperand(2);
2706 if (Cond.hasOneUse()) { // TODO: Look for multiple select uses.
2707 SelectionDAG &DAG = DCI.DAG;
2708 if ((DAG.isConstantValueOfAnyType(True) ||
2709 DAG.isConstantValueOfAnyType(True)) &&
2710 (!DAG.isConstantValueOfAnyType(False) &&
2711 !DAG.isConstantValueOfAnyType(False))) {
2712 // Swap cmp + select pair to move constant to false input.
2713 // This will allow using VOPC cndmasks more often.
2714 // select (setcc x, y), k, x -> select (setcc y, x) x, x
2717 ISD::CondCode NewCC = getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
2718 LHS.getValueType().isInteger());
2720 SDValue NewCond = DAG.getSetCC(SL, Cond.getValueType(), LHS, RHS, NewCC);
2721 return DAG.getNode(ISD::SELECT, SL, VT, NewCond, False, True);
2725 if (VT == MVT::f32 && Cond.hasOneUse()) {
2727 = CombineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI);
2728 // Revisit this node so we can catch min3/max3/med3 patterns.
2729 //DCI.AddToWorklist(MinMax.getNode());
2733 // There's no reason to not do this if the condition has other uses.
2734 return performCtlzCombine(SDLoc(N), Cond, True, False, DCI);
2737 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N,
2738 DAGCombinerInfo &DCI) const {
2739 SelectionDAG &DAG = DCI.DAG;
2742 switch(N->getOpcode()) {
2745 case ISD::BITCAST: {
2746 EVT DestVT = N->getValueType(0);
2748 // Push casts through vector builds. This helps avoid emitting a large
2749 // number of copies when materializing floating point vector constants.
2751 // vNt1 bitcast (vNt0 (build_vector t0:x, t0:y)) =>
2752 // vnt1 = build_vector (t1 (bitcast t0:x)), (t1 (bitcast t0:y))
2753 if (DestVT.isVector()) {
2754 SDValue Src = N->getOperand(0);
2755 if (Src.getOpcode() == ISD::BUILD_VECTOR) {
2756 EVT SrcVT = Src.getValueType();
2757 unsigned NElts = DestVT.getVectorNumElements();
2759 if (SrcVT.getVectorNumElements() == NElts) {
2760 EVT DestEltVT = DestVT.getVectorElementType();
2762 SmallVector<SDValue, 8> CastedElts;
2764 for (unsigned I = 0, E = SrcVT.getVectorNumElements(); I != E; ++I) {
2765 SDValue Elt = Src.getOperand(I);
2766 CastedElts.push_back(DAG.getNode(ISD::BITCAST, DL, DestEltVT, Elt));
2769 return DAG.getBuildVector(DestVT, SL, CastedElts);
2774 if (DestVT.getSizeInBits() != 64 && !DestVT.isVector())
2777 // Fold bitcasts of constants.
2779 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k)
2780 // TODO: Generalize and move to DAGCombiner
2781 SDValue Src = N->getOperand(0);
2782 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) {
2783 assert(Src.getValueType() == MVT::i64);
2785 uint64_t CVal = C->getZExtValue();
2786 return DAG.getNode(ISD::BUILD_VECTOR, SL, DestVT,
2787 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
2788 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
2791 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) {
2792 const APInt &Val = C->getValueAPF().bitcastToAPInt();
2794 uint64_t CVal = Val.getZExtValue();
2795 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
2796 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
2797 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
2799 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec);
2805 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2808 return performShlCombine(N, DCI);
2811 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2814 return performSrlCombine(N, DCI);
2817 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
2820 return performSraCombine(N, DCI);
2823 return performMulCombine(N, DCI);
2825 return performMulhsCombine(N, DCI);
2827 return performMulhuCombine(N, DCI);
2828 case AMDGPUISD::MUL_I24:
2829 case AMDGPUISD::MUL_U24:
2830 case AMDGPUISD::MULHI_I24:
2831 case AMDGPUISD::MULHI_U24: {
2832 // If the first call to simplify is successfull, then N may end up being
2833 // deleted, so we shouldn't call simplifyI24 again.
2834 simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI);
2837 case AMDGPUISD::MUL_LOHI_I24:
2838 case AMDGPUISD::MUL_LOHI_U24:
2839 return performMulLoHi24Combine(N, DCI);
2841 return performSelectCombine(N, DCI);
2842 case AMDGPUISD::BFE_I32:
2843 case AMDGPUISD::BFE_U32: {
2844 assert(!N->getValueType(0).isVector() &&
2845 "Vector handling of BFE not implemented");
2846 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
2850 uint32_t WidthVal = Width->getZExtValue() & 0x1f;
2852 return DAG.getConstant(0, DL, MVT::i32);
2854 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
2858 SDValue BitsFrom = N->getOperand(0);
2859 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f;
2861 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32;
2863 if (OffsetVal == 0) {
2864 // This is already sign / zero extended, so try to fold away extra BFEs.
2865 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal);
2867 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom);
2868 if (OpSignBits >= SignBits)
2871 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal);
2873 // This is a sign_extend_inreg. Replace it to take advantage of existing
2874 // DAG Combines. If not eliminated, we will match back to BFE during
2877 // TODO: The sext_inreg of extended types ends, although we can could
2878 // handle them in a single BFE.
2879 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom,
2880 DAG.getValueType(SmallVT));
2883 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT);
2886 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) {
2888 return constantFoldBFE<int32_t>(DAG,
2889 CVal->getSExtValue(),
2895 return constantFoldBFE<uint32_t>(DAG,
2896 CVal->getZExtValue(),
2902 if ((OffsetVal + WidthVal) >= 32) {
2903 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32);
2904 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2905 BitsFrom, ShiftVal);
2908 if (BitsFrom.hasOneUse()) {
2909 APInt Demanded = APInt::getBitsSet(32,
2911 OffsetVal + WidthVal);
2913 APInt KnownZero, KnownOne;
2914 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2915 !DCI.isBeforeLegalizeOps());
2916 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2917 if (TLO.ShrinkDemandedConstant(BitsFrom, Demanded) ||
2918 TLI.SimplifyDemandedBits(BitsFrom, Demanded,
2919 KnownZero, KnownOne, TLO)) {
2920 DCI.CommitTargetLoweringOpt(TLO);
2927 return performLoadCombine(N, DCI);
2929 return performStoreCombine(N, DCI);
2934 //===----------------------------------------------------------------------===//
2936 //===----------------------------------------------------------------------===//
2938 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2939 const TargetRegisterClass *RC,
2940 unsigned Reg, EVT VT) const {
2941 MachineFunction &MF = DAG.getMachineFunction();
2942 MachineRegisterInfo &MRI = MF.getRegInfo();
2943 unsigned VirtualRegister;
2944 if (!MRI.isLiveIn(Reg)) {
2945 VirtualRegister = MRI.createVirtualRegister(RC);
2946 MRI.addLiveIn(Reg, VirtualRegister);
2948 VirtualRegister = MRI.getLiveInVirtReg(Reg);
2950 return DAG.getRegister(VirtualRegister, VT);
2953 uint32_t AMDGPUTargetLowering::getImplicitParameterOffset(
2954 const AMDGPUMachineFunction *MFI, const ImplicitParameter Param) const {
2955 unsigned Alignment = Subtarget->getAlignmentForImplicitArgPtr();
2956 uint64_t ArgOffset = alignTo(MFI->getABIArgOffset(), Alignment);
2961 return ArgOffset + 4;
2963 llvm_unreachable("unexpected implicit parameter type");
2966 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
2968 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
2969 switch ((AMDGPUISD::NodeType)Opcode) {
2970 case AMDGPUISD::FIRST_NUMBER: break;
2972 NODE_NAME_CASE(CALL);
2973 NODE_NAME_CASE(UMUL);
2974 NODE_NAME_CASE(BRANCH_COND);
2977 NODE_NAME_CASE(ENDPGM)
2978 NODE_NAME_CASE(RETURN)
2979 NODE_NAME_CASE(DWORDADDR)
2980 NODE_NAME_CASE(FRACT)
2981 NODE_NAME_CASE(SETCC)
2982 NODE_NAME_CASE(SETREG)
2983 NODE_NAME_CASE(FMA_W_CHAIN)
2984 NODE_NAME_CASE(FMUL_W_CHAIN)
2985 NODE_NAME_CASE(CLAMP)
2986 NODE_NAME_CASE(COS_HW)
2987 NODE_NAME_CASE(SIN_HW)
2988 NODE_NAME_CASE(FMAX_LEGACY)
2989 NODE_NAME_CASE(FMIN_LEGACY)
2990 NODE_NAME_CASE(FMAX3)
2991 NODE_NAME_CASE(SMAX3)
2992 NODE_NAME_CASE(UMAX3)
2993 NODE_NAME_CASE(FMIN3)
2994 NODE_NAME_CASE(SMIN3)
2995 NODE_NAME_CASE(UMIN3)
2996 NODE_NAME_CASE(FMED3)
2997 NODE_NAME_CASE(SMED3)
2998 NODE_NAME_CASE(UMED3)
2999 NODE_NAME_CASE(URECIP)
3000 NODE_NAME_CASE(DIV_SCALE)
3001 NODE_NAME_CASE(DIV_FMAS)
3002 NODE_NAME_CASE(DIV_FIXUP)
3003 NODE_NAME_CASE(TRIG_PREOP)
3006 NODE_NAME_CASE(RCP_LEGACY)
3007 NODE_NAME_CASE(RSQ_LEGACY)
3008 NODE_NAME_CASE(FMUL_LEGACY)
3009 NODE_NAME_CASE(RSQ_CLAMP)
3010 NODE_NAME_CASE(LDEXP)
3011 NODE_NAME_CASE(FP_CLASS)
3012 NODE_NAME_CASE(DOT4)
3013 NODE_NAME_CASE(CARRY)
3014 NODE_NAME_CASE(BORROW)
3015 NODE_NAME_CASE(BFE_U32)
3016 NODE_NAME_CASE(BFE_I32)
3019 NODE_NAME_CASE(FFBH_U32)
3020 NODE_NAME_CASE(FFBH_I32)
3021 NODE_NAME_CASE(MUL_U24)
3022 NODE_NAME_CASE(MUL_I24)
3023 NODE_NAME_CASE(MULHI_U24)
3024 NODE_NAME_CASE(MULHI_I24)
3025 NODE_NAME_CASE(MUL_LOHI_U24)
3026 NODE_NAME_CASE(MUL_LOHI_I24)
3027 NODE_NAME_CASE(MAD_U24)
3028 NODE_NAME_CASE(MAD_I24)
3029 NODE_NAME_CASE(TEXTURE_FETCH)
3030 NODE_NAME_CASE(EXPORT)
3031 NODE_NAME_CASE(EXPORT_DONE)
3032 NODE_NAME_CASE(R600_EXPORT)
3033 NODE_NAME_CASE(CONST_ADDRESS)
3034 NODE_NAME_CASE(REGISTER_LOAD)
3035 NODE_NAME_CASE(REGISTER_STORE)
3036 NODE_NAME_CASE(LOAD_INPUT)
3037 NODE_NAME_CASE(SAMPLE)
3038 NODE_NAME_CASE(SAMPLEB)
3039 NODE_NAME_CASE(SAMPLED)
3040 NODE_NAME_CASE(SAMPLEL)
3041 NODE_NAME_CASE(CVT_F32_UBYTE0)
3042 NODE_NAME_CASE(CVT_F32_UBYTE1)
3043 NODE_NAME_CASE(CVT_F32_UBYTE2)
3044 NODE_NAME_CASE(CVT_F32_UBYTE3)
3045 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR)
3046 NODE_NAME_CASE(CONST_DATA_PTR)
3047 NODE_NAME_CASE(PC_ADD_REL_OFFSET)
3048 NODE_NAME_CASE(KILL)
3049 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break;
3050 NODE_NAME_CASE(SENDMSG)
3051 NODE_NAME_CASE(INTERP_MOV)
3052 NODE_NAME_CASE(INTERP_P1)
3053 NODE_NAME_CASE(INTERP_P2)
3054 NODE_NAME_CASE(STORE_MSKOR)
3055 NODE_NAME_CASE(LOAD_CONSTANT)
3056 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
3057 NODE_NAME_CASE(ATOMIC_CMP_SWAP)
3058 NODE_NAME_CASE(ATOMIC_INC)
3059 NODE_NAME_CASE(ATOMIC_DEC)
3060 NODE_NAME_CASE(BUFFER_LOAD)
3061 NODE_NAME_CASE(BUFFER_LOAD_FORMAT)
3062 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break;
3067 SDValue AMDGPUTargetLowering::getSqrtEstimate(SDValue Operand,
3068 SelectionDAG &DAG, int Enabled,
3069 int &RefinementSteps,
3070 bool &UseOneConstNR,
3071 bool Reciprocal) const {
3072 EVT VT = Operand.getValueType();
3074 if (VT == MVT::f32) {
3075 RefinementSteps = 0;
3076 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand);
3079 // TODO: There is also f64 rsq instruction, but the documentation is less
3080 // clear on its precision.
3085 SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand,
3086 SelectionDAG &DAG, int Enabled,
3087 int &RefinementSteps) const {
3088 EVT VT = Operand.getValueType();
3090 if (VT == MVT::f32) {
3091 // Reciprocal, < 1 ulp error.
3093 // This reciprocal approximation converges to < 0.5 ulp error with one
3094 // newton rhapson performed with two fused multiple adds (FMAs).
3096 RefinementSteps = 0;
3097 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand);
3100 // TODO: There is also f64 rcp instruction, but the documentation is less
3101 // clear on its precision.
3106 void AMDGPUTargetLowering::computeKnownBitsForTargetNode(
3110 const SelectionDAG &DAG,
3111 unsigned Depth) const {
3113 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything.
3117 unsigned Opc = Op.getOpcode();
3122 case AMDGPUISD::CARRY:
3123 case AMDGPUISD::BORROW: {
3124 KnownZero = APInt::getHighBitsSet(32, 31);
3128 case AMDGPUISD::BFE_I32:
3129 case AMDGPUISD::BFE_U32: {
3130 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3134 unsigned BitWidth = 32;
3135 uint32_t Width = CWidth->getZExtValue() & 0x1f;
3137 if (Opc == AMDGPUISD::BFE_U32)
3138 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - Width);
3145 unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode(
3147 const SelectionDAG &DAG,
3148 unsigned Depth) const {
3149 switch (Op.getOpcode()) {
3150 case AMDGPUISD::BFE_I32: {
3151 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3155 unsigned SignBits = 32 - Width->getZExtValue() + 1;
3156 if (!isNullConstant(Op.getOperand(1)))
3159 // TODO: Could probably figure something out with non-0 offsets.
3160 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1);
3161 return std::max(SignBits, Op0SignBits);
3164 case AMDGPUISD::BFE_U32: {
3165 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3166 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1;
3169 case AMDGPUISD::CARRY:
3170 case AMDGPUISD::BORROW: