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 "AMDGPUCallLowering.h"
19 #include "AMDGPUFrameLowering.h"
20 #include "AMDGPUIntrinsicInfo.h"
21 #include "AMDGPURegisterInfo.h"
22 #include "AMDGPUSubtarget.h"
23 #include "R600MachineFunctionInfo.h"
24 #include "SIMachineFunctionInfo.h"
25 #include "llvm/CodeGen/CallingConvLower.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/SelectionDAG.h"
29 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DiagnosticInfo.h"
32 #include "llvm/Support/KnownBits.h"
33 #include "SIInstrInfo.h"
36 static bool allocateKernArg(unsigned ValNo, MVT ValVT, MVT LocVT,
37 CCValAssign::LocInfo LocInfo,
38 ISD::ArgFlagsTy ArgFlags, CCState &State) {
39 MachineFunction &MF = State.getMachineFunction();
40 AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>();
42 uint64_t Offset = MFI->allocateKernArg(LocVT.getStoreSize(),
43 ArgFlags.getOrigAlign());
44 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT, Offset, LocVT, LocInfo));
48 static bool allocateCCRegs(unsigned ValNo, MVT ValVT, MVT LocVT,
49 CCValAssign::LocInfo LocInfo,
50 ISD::ArgFlagsTy ArgFlags, CCState &State,
51 const TargetRegisterClass *RC,
53 ArrayRef<MCPhysReg> RegList = makeArrayRef(RC->begin(), NumRegs);
54 unsigned RegResult = State.AllocateReg(RegList);
55 if (RegResult == AMDGPU::NoRegister)
58 State.addLoc(CCValAssign::getReg(ValNo, ValVT, RegResult, LocVT, LocInfo));
62 static bool allocateSGPRTuple(unsigned ValNo, MVT ValVT, MVT LocVT,
63 CCValAssign::LocInfo LocInfo,
64 ISD::ArgFlagsTy ArgFlags, CCState &State) {
65 switch (LocVT.SimpleTy) {
71 return allocateCCRegs(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State,
72 &AMDGPU::SGPR_64RegClass, 20);
79 // Allocate up to VGPR31.
81 // TODO: Since there are no VGPR alignent requirements would it be better to
82 // split into individual scalar registers?
83 static bool allocateVGPRTuple(unsigned ValNo, MVT ValVT, MVT LocVT,
84 CCValAssign::LocInfo LocInfo,
85 ISD::ArgFlagsTy ArgFlags, CCState &State) {
86 switch (LocVT.SimpleTy) {
91 return allocateCCRegs(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State,
92 &AMDGPU::VReg_64RegClass, 31);
98 return allocateCCRegs(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State,
99 &AMDGPU::VReg_128RegClass, 29);
103 return allocateCCRegs(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State,
104 &AMDGPU::VReg_256RegClass, 25);
109 return allocateCCRegs(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State,
110 &AMDGPU::VReg_512RegClass, 17);
118 #include "AMDGPUGenCallingConv.inc"
120 // Find a larger type to do a load / store of a vector with.
121 EVT AMDGPUTargetLowering::getEquivalentMemType(LLVMContext &Ctx, EVT VT) {
122 unsigned StoreSize = VT.getStoreSizeInBits();
124 return EVT::getIntegerVT(Ctx, StoreSize);
126 assert(StoreSize % 32 == 0 && "Store size not a multiple of 32");
127 return EVT::getVectorVT(Ctx, MVT::i32, StoreSize / 32);
130 AMDGPUTargetLowering::AMDGPUTargetLowering(const TargetMachine &TM,
131 const AMDGPUSubtarget &STI)
132 : TargetLowering(TM), Subtarget(&STI) {
133 AMDGPUASI = AMDGPU::getAMDGPUAS(TM);
134 // Lower floating point store/load to integer store/load to reduce the number
135 // of patterns in tablegen.
136 setOperationAction(ISD::LOAD, MVT::f32, Promote);
137 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
139 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
140 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
142 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
143 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
145 setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
146 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
148 setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
149 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
151 setOperationAction(ISD::LOAD, MVT::i64, Promote);
152 AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
154 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
155 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
157 setOperationAction(ISD::LOAD, MVT::f64, Promote);
158 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::v2i32);
160 setOperationAction(ISD::LOAD, MVT::v2f64, Promote);
161 AddPromotedToType(ISD::LOAD, MVT::v2f64, MVT::v4i32);
163 // There are no 64-bit extloads. These should be done as a 32-bit extload and
164 // an extension to 64-bit.
165 for (MVT VT : MVT::integer_valuetypes()) {
166 setLoadExtAction(ISD::EXTLOAD, MVT::i64, VT, Expand);
167 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, VT, Expand);
168 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, VT, Expand);
171 for (MVT VT : MVT::integer_valuetypes()) {
175 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
176 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
177 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
178 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
180 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
181 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
182 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
183 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
185 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
186 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
187 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
188 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
191 for (MVT VT : MVT::integer_vector_valuetypes()) {
192 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i8, Expand);
193 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i8, Expand);
194 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i8, Expand);
195 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i8, Expand);
196 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i8, Expand);
197 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i8, Expand);
198 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i16, Expand);
199 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i16, Expand);
200 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i16, Expand);
201 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i16, Expand);
202 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i16, Expand);
203 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i16, Expand);
206 setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
207 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, MVT::v2f16, Expand);
208 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, MVT::v4f16, Expand);
209 setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, MVT::v8f16, Expand);
211 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
212 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
213 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Expand);
214 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f32, Expand);
216 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
217 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
218 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f16, Expand);
219 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f16, Expand);
221 setOperationAction(ISD::STORE, MVT::f32, Promote);
222 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
224 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
225 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
227 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
228 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
230 setOperationAction(ISD::STORE, MVT::v8f32, Promote);
231 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
233 setOperationAction(ISD::STORE, MVT::v16f32, Promote);
234 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
236 setOperationAction(ISD::STORE, MVT::i64, Promote);
237 AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
239 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
240 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
242 setOperationAction(ISD::STORE, MVT::f64, Promote);
243 AddPromotedToType(ISD::STORE, MVT::f64, MVT::v2i32);
245 setOperationAction(ISD::STORE, MVT::v2f64, Promote);
246 AddPromotedToType(ISD::STORE, MVT::v2f64, MVT::v4i32);
248 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
249 setTruncStoreAction(MVT::i64, MVT::i8, Expand);
250 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
251 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
253 setTruncStoreAction(MVT::v2i64, MVT::v2i1, Expand);
254 setTruncStoreAction(MVT::v2i64, MVT::v2i8, Expand);
255 setTruncStoreAction(MVT::v2i64, MVT::v2i16, Expand);
256 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
258 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
259 setTruncStoreAction(MVT::v2f32, MVT::v2f16, Expand);
260 setTruncStoreAction(MVT::v4f32, MVT::v4f16, Expand);
261 setTruncStoreAction(MVT::v8f32, MVT::v8f16, Expand);
263 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
264 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
266 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
267 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
269 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Expand);
270 setTruncStoreAction(MVT::v4f64, MVT::v4f16, Expand);
272 setTruncStoreAction(MVT::v8f64, MVT::v8f32, Expand);
273 setTruncStoreAction(MVT::v8f64, MVT::v8f16, Expand);
276 setOperationAction(ISD::Constant, MVT::i32, Legal);
277 setOperationAction(ISD::Constant, MVT::i64, Legal);
278 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
279 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
281 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
282 setOperationAction(ISD::BRIND, MVT::Other, Expand);
284 // This is totally unsupported, just custom lower to produce an error.
285 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
287 // Library functions. These default to Expand, but we have instructions
289 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
290 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
291 setOperationAction(ISD::FPOW, MVT::f32, Legal);
292 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
293 setOperationAction(ISD::FABS, MVT::f32, Legal);
294 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
295 setOperationAction(ISD::FRINT, MVT::f32, Legal);
296 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
297 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
298 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
300 setOperationAction(ISD::FROUND, MVT::f32, Custom);
301 setOperationAction(ISD::FROUND, MVT::f64, Custom);
303 setOperationAction(ISD::FNEARBYINT, MVT::f32, Custom);
304 setOperationAction(ISD::FNEARBYINT, MVT::f64, Custom);
306 setOperationAction(ISD::FREM, MVT::f32, Custom);
307 setOperationAction(ISD::FREM, MVT::f64, Custom);
309 // v_mad_f32 does not support denormals according to some sources.
310 if (!Subtarget->hasFP32Denormals())
311 setOperationAction(ISD::FMAD, MVT::f32, Legal);
313 // Expand to fneg + fadd.
314 setOperationAction(ISD::FSUB, MVT::f64, Expand);
316 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
317 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
318 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
319 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
320 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
321 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
322 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f32, Custom);
323 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
324 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f32, Custom);
325 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i32, Custom);
327 if (Subtarget->getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
328 setOperationAction(ISD::FCEIL, MVT::f64, Custom);
329 setOperationAction(ISD::FTRUNC, MVT::f64, Custom);
330 setOperationAction(ISD::FRINT, MVT::f64, Custom);
331 setOperationAction(ISD::FFLOOR, MVT::f64, Custom);
334 if (!Subtarget->hasBFI()) {
335 // fcopysign can be done in a single instruction with BFI.
336 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
337 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
340 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
341 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Custom);
342 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Custom);
344 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
345 for (MVT VT : ScalarIntVTs) {
346 // These should use [SU]DIVREM, so set them to expand
347 setOperationAction(ISD::SDIV, VT, Expand);
348 setOperationAction(ISD::UDIV, VT, Expand);
349 setOperationAction(ISD::SREM, VT, Expand);
350 setOperationAction(ISD::UREM, VT, Expand);
352 // GPU does not have divrem function for signed or unsigned.
353 setOperationAction(ISD::SDIVREM, VT, Custom);
354 setOperationAction(ISD::UDIVREM, VT, Custom);
356 // GPU does not have [S|U]MUL_LOHI functions as a single instruction.
357 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
358 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
360 setOperationAction(ISD::BSWAP, VT, Expand);
361 setOperationAction(ISD::CTTZ, VT, Expand);
362 setOperationAction(ISD::CTLZ, VT, Expand);
365 if (!Subtarget->hasBCNT(32))
366 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
368 if (!Subtarget->hasBCNT(64))
369 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
371 // The hardware supports 32-bit ROTR, but not ROTL.
372 setOperationAction(ISD::ROTL, MVT::i32, Expand);
373 setOperationAction(ISD::ROTL, MVT::i64, Expand);
374 setOperationAction(ISD::ROTR, MVT::i64, Expand);
376 setOperationAction(ISD::MUL, MVT::i64, Expand);
377 setOperationAction(ISD::MULHU, MVT::i64, Expand);
378 setOperationAction(ISD::MULHS, MVT::i64, Expand);
379 setOperationAction(ISD::UDIV, MVT::i32, Expand);
380 setOperationAction(ISD::UREM, MVT::i32, Expand);
381 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
382 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
383 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
384 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
385 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
387 setOperationAction(ISD::SMIN, MVT::i32, Legal);
388 setOperationAction(ISD::UMIN, MVT::i32, Legal);
389 setOperationAction(ISD::SMAX, MVT::i32, Legal);
390 setOperationAction(ISD::UMAX, MVT::i32, Legal);
392 if (Subtarget->hasFFBH())
393 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom);
395 if (Subtarget->hasFFBL())
396 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Legal);
398 setOperationAction(ISD::CTLZ, MVT::i64, Custom);
399 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
401 // We only really have 32-bit BFE instructions (and 16-bit on VI).
403 // On SI+ there are 64-bit BFEs, but they are scalar only and there isn't any
404 // effort to match them now. We want this to be false for i64 cases when the
405 // extraction isn't restricted to the upper or lower half. Ideally we would
406 // have some pass reduce 64-bit extracts to 32-bit if possible. Extracts that
407 // span the midpoint are probably relatively rare, so don't worry about them
409 if (Subtarget->hasBFE())
410 setHasExtractBitsInsn(true);
412 static const MVT::SimpleValueType VectorIntTypes[] = {
413 MVT::v2i32, MVT::v4i32
416 for (MVT VT : VectorIntTypes) {
417 // Expand the following operations for the current type by default.
418 setOperationAction(ISD::ADD, VT, Expand);
419 setOperationAction(ISD::AND, VT, Expand);
420 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
421 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
422 setOperationAction(ISD::MUL, VT, Expand);
423 setOperationAction(ISD::MULHU, VT, Expand);
424 setOperationAction(ISD::MULHS, VT, Expand);
425 setOperationAction(ISD::OR, VT, Expand);
426 setOperationAction(ISD::SHL, VT, Expand);
427 setOperationAction(ISD::SRA, VT, Expand);
428 setOperationAction(ISD::SRL, VT, Expand);
429 setOperationAction(ISD::ROTL, VT, Expand);
430 setOperationAction(ISD::ROTR, VT, Expand);
431 setOperationAction(ISD::SUB, VT, Expand);
432 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
433 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
434 setOperationAction(ISD::SDIV, VT, Expand);
435 setOperationAction(ISD::UDIV, VT, Expand);
436 setOperationAction(ISD::SREM, VT, Expand);
437 setOperationAction(ISD::UREM, VT, Expand);
438 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
439 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
440 setOperationAction(ISD::SDIVREM, VT, Custom);
441 setOperationAction(ISD::UDIVREM, VT, Expand);
442 setOperationAction(ISD::ADDC, VT, Expand);
443 setOperationAction(ISD::SUBC, VT, Expand);
444 setOperationAction(ISD::ADDE, VT, Expand);
445 setOperationAction(ISD::SUBE, VT, Expand);
446 setOperationAction(ISD::SELECT, VT, Expand);
447 setOperationAction(ISD::VSELECT, VT, Expand);
448 setOperationAction(ISD::SELECT_CC, VT, Expand);
449 setOperationAction(ISD::XOR, VT, Expand);
450 setOperationAction(ISD::BSWAP, VT, Expand);
451 setOperationAction(ISD::CTPOP, VT, Expand);
452 setOperationAction(ISD::CTTZ, VT, Expand);
453 setOperationAction(ISD::CTLZ, VT, Expand);
454 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
457 static const MVT::SimpleValueType FloatVectorTypes[] = {
458 MVT::v2f32, MVT::v4f32
461 for (MVT VT : FloatVectorTypes) {
462 setOperationAction(ISD::FABS, VT, Expand);
463 setOperationAction(ISD::FMINNUM, VT, Expand);
464 setOperationAction(ISD::FMAXNUM, VT, Expand);
465 setOperationAction(ISD::FADD, VT, Expand);
466 setOperationAction(ISD::FCEIL, VT, Expand);
467 setOperationAction(ISD::FCOS, VT, Expand);
468 setOperationAction(ISD::FDIV, VT, Expand);
469 setOperationAction(ISD::FEXP2, VT, Expand);
470 setOperationAction(ISD::FLOG2, VT, Expand);
471 setOperationAction(ISD::FREM, VT, Expand);
472 setOperationAction(ISD::FPOW, VT, Expand);
473 setOperationAction(ISD::FFLOOR, VT, Expand);
474 setOperationAction(ISD::FTRUNC, VT, Expand);
475 setOperationAction(ISD::FMUL, VT, Expand);
476 setOperationAction(ISD::FMA, VT, Expand);
477 setOperationAction(ISD::FRINT, VT, Expand);
478 setOperationAction(ISD::FNEARBYINT, VT, Expand);
479 setOperationAction(ISD::FSQRT, VT, Expand);
480 setOperationAction(ISD::FSIN, VT, Expand);
481 setOperationAction(ISD::FSUB, VT, Expand);
482 setOperationAction(ISD::FNEG, VT, Expand);
483 setOperationAction(ISD::VSELECT, VT, Expand);
484 setOperationAction(ISD::SELECT_CC, VT, Expand);
485 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
486 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
489 // This causes using an unrolled select operation rather than expansion with
490 // bit operations. This is in general better, but the alternative using BFI
491 // instructions may be better if the select sources are SGPRs.
492 setOperationAction(ISD::SELECT, MVT::v2f32, Promote);
493 AddPromotedToType(ISD::SELECT, MVT::v2f32, MVT::v2i32);
495 setOperationAction(ISD::SELECT, MVT::v4f32, Promote);
496 AddPromotedToType(ISD::SELECT, MVT::v4f32, MVT::v4i32);
498 // There are no libcalls of any kind.
499 for (int I = 0; I < RTLIB::UNKNOWN_LIBCALL; ++I)
500 setLibcallName(static_cast<RTLIB::Libcall>(I), nullptr);
502 setBooleanContents(ZeroOrNegativeOneBooleanContent);
503 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
505 setSchedulingPreference(Sched::RegPressure);
506 setJumpIsExpensive(true);
508 // FIXME: This is only partially true. If we have to do vector compares, any
509 // SGPR pair can be a condition register. If we have a uniform condition, we
510 // are better off doing SALU operations, where there is only one SCC. For now,
511 // we don't have a way of knowing during instruction selection if a condition
512 // will be uniform and we always use vector compares. Assume we are using
513 // vector compares until that is fixed.
514 setHasMultipleConditionRegisters(true);
516 // SI at least has hardware support for floating point exceptions, but no way
517 // of using or handling them is implemented. They are also optional in OpenCL
519 setHasFloatingPointExceptions(Subtarget->hasFPExceptions());
521 PredictableSelectIsExpensive = false;
523 // We want to find all load dependencies for long chains of stores to enable
524 // merging into very wide vectors. The problem is with vectors with > 4
525 // elements. MergeConsecutiveStores will attempt to merge these because x8/x16
526 // vectors are a legal type, even though we have to split the loads
527 // usually. When we can more precisely specify load legality per address
528 // space, we should be able to make FindBetterChain/MergeConsecutiveStores
529 // smarter so that they can figure out what to do in 2 iterations without all
530 // N > 4 stores on the same chain.
531 GatherAllAliasesMaxDepth = 16;
533 // memcpy/memmove/memset are expanded in the IR, so we shouldn't need to worry
534 // about these during lowering.
535 MaxStoresPerMemcpy = 0xffffffff;
536 MaxStoresPerMemmove = 0xffffffff;
537 MaxStoresPerMemset = 0xffffffff;
539 setTargetDAGCombine(ISD::BITCAST);
540 setTargetDAGCombine(ISD::SHL);
541 setTargetDAGCombine(ISD::SRA);
542 setTargetDAGCombine(ISD::SRL);
543 setTargetDAGCombine(ISD::MUL);
544 setTargetDAGCombine(ISD::MULHU);
545 setTargetDAGCombine(ISD::MULHS);
546 setTargetDAGCombine(ISD::SELECT);
547 setTargetDAGCombine(ISD::SELECT_CC);
548 setTargetDAGCombine(ISD::STORE);
549 setTargetDAGCombine(ISD::FADD);
550 setTargetDAGCombine(ISD::FSUB);
551 setTargetDAGCombine(ISD::FNEG);
552 setTargetDAGCombine(ISD::FABS);
555 //===----------------------------------------------------------------------===//
556 // Target Information
557 //===----------------------------------------------------------------------===//
560 static bool fnegFoldsIntoOp(unsigned Opc) {
572 case ISD::FNEARBYINT:
574 case AMDGPUISD::RCP_LEGACY:
575 case AMDGPUISD::SIN_HW:
576 case AMDGPUISD::FMUL_LEGACY:
577 case AMDGPUISD::FMIN_LEGACY:
578 case AMDGPUISD::FMAX_LEGACY:
585 /// \p returns true if the operation will definitely need to use a 64-bit
586 /// encoding, and thus will use a VOP3 encoding regardless of the source
589 static bool opMustUseVOP3Encoding(const SDNode *N, MVT VT) {
590 return N->getNumOperands() > 2 || VT == MVT::f64;
593 // Most FP instructions support source modifiers, but this could be refined
596 static bool hasSourceMods(const SDNode *N) {
597 if (isa<MemSDNode>(N))
600 switch (N->getOpcode()) {
606 case AMDGPUISD::INTERP_P1:
607 case AMDGPUISD::INTERP_P2:
608 case AMDGPUISD::DIV_SCALE:
610 // TODO: Should really be looking at the users of the bitcast. These are
611 // problematic because bitcasts are used to legalize all stores to integer
620 bool AMDGPUTargetLowering::allUsesHaveSourceMods(const SDNode *N,
621 unsigned CostThreshold) {
622 // Some users (such as 3-operand FMA/MAD) must use a VOP3 encoding, and thus
623 // it is truly free to use a source modifier in all cases. If there are
624 // multiple users but for each one will necessitate using VOP3, there will be
625 // a code size increase. Try to avoid increasing code size unless we know it
626 // will save on the instruction count.
627 unsigned NumMayIncreaseSize = 0;
628 MVT VT = N->getValueType(0).getScalarType().getSimpleVT();
630 // XXX - Should this limit number of uses to check?
631 for (const SDNode *U : N->uses()) {
632 if (!hasSourceMods(U))
635 if (!opMustUseVOP3Encoding(U, VT)) {
636 if (++NumMayIncreaseSize > CostThreshold)
644 MVT AMDGPUTargetLowering::getVectorIdxTy(const DataLayout &) const {
648 bool AMDGPUTargetLowering::isSelectSupported(SelectSupportKind SelType) const {
652 // The backend supports 32 and 64 bit floating point immediates.
653 // FIXME: Why are we reporting vectors of FP immediates as legal?
654 bool AMDGPUTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
655 EVT ScalarVT = VT.getScalarType();
656 return (ScalarVT == MVT::f32 || ScalarVT == MVT::f64 ||
657 (ScalarVT == MVT::f16 && Subtarget->has16BitInsts()));
660 // We don't want to shrink f64 / f32 constants.
661 bool AMDGPUTargetLowering::ShouldShrinkFPConstant(EVT VT) const {
662 EVT ScalarVT = VT.getScalarType();
663 return (ScalarVT != MVT::f32 && ScalarVT != MVT::f64);
666 bool AMDGPUTargetLowering::shouldReduceLoadWidth(SDNode *N,
670 unsigned NewSize = NewVT.getStoreSizeInBits();
672 // If we are reducing to a 32-bit load, this is always better.
676 EVT OldVT = N->getValueType(0);
677 unsigned OldSize = OldVT.getStoreSizeInBits();
679 // Don't produce extloads from sub 32-bit types. SI doesn't have scalar
680 // extloads, so doing one requires using a buffer_load. In cases where we
681 // still couldn't use a scalar load, using the wider load shouldn't really
684 // If the old size already had to be an extload, there's no harm in continuing
685 // to reduce the width.
686 return (OldSize < 32);
689 bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy,
692 assert(LoadTy.getSizeInBits() == CastTy.getSizeInBits());
694 if (LoadTy.getScalarType() == MVT::i32)
697 unsigned LScalarSize = LoadTy.getScalarSizeInBits();
698 unsigned CastScalarSize = CastTy.getScalarSizeInBits();
700 return (LScalarSize < CastScalarSize) ||
701 (CastScalarSize >= 32);
704 // SI+ has instructions for cttz / ctlz for 32-bit values. This is probably also
705 // profitable with the expansion for 64-bit since it's generally good to
707 // FIXME: These should really have the size as a parameter.
708 bool AMDGPUTargetLowering::isCheapToSpeculateCttz() const {
712 bool AMDGPUTargetLowering::isCheapToSpeculateCtlz() const {
716 //===---------------------------------------------------------------------===//
718 //===---------------------------------------------------------------------===//
720 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
721 assert(VT.isFloatingPoint());
723 // Packed operations do not have a fabs modifier.
724 return VT == MVT::f32 || VT == MVT::f64 ||
725 (Subtarget->has16BitInsts() && VT == MVT::f16);
728 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
729 assert(VT.isFloatingPoint());
730 return VT == MVT::f32 || VT == MVT::f64 ||
731 (Subtarget->has16BitInsts() && VT == MVT::f16) ||
732 (Subtarget->hasVOP3PInsts() && VT == MVT::v2f16);
735 bool AMDGPUTargetLowering:: storeOfVectorConstantIsCheap(EVT MemVT,
741 bool AMDGPUTargetLowering::aggressivelyPreferBuildVectorSources(EVT VecVT) const {
742 // There are few operations which truly have vector input operands. Any vector
743 // operation is going to involve operations on each component, and a
744 // build_vector will be a copy per element, so it always makes sense to use a
745 // build_vector input in place of the extracted element to avoid a copy into a
748 // We should probably only do this if all users are extracts only, but this
749 // should be the common case.
753 bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const {
754 // Truncate is just accessing a subregister.
756 unsigned SrcSize = Source.getSizeInBits();
757 unsigned DestSize = Dest.getSizeInBits();
759 return DestSize < SrcSize && DestSize % 32 == 0 ;
762 bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const {
763 // Truncate is just accessing a subregister.
765 unsigned SrcSize = Source->getScalarSizeInBits();
766 unsigned DestSize = Dest->getScalarSizeInBits();
768 if (DestSize== 16 && Subtarget->has16BitInsts())
769 return SrcSize >= 32;
771 return DestSize < SrcSize && DestSize % 32 == 0;
774 bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const {
775 unsigned SrcSize = Src->getScalarSizeInBits();
776 unsigned DestSize = Dest->getScalarSizeInBits();
778 if (SrcSize == 16 && Subtarget->has16BitInsts())
779 return DestSize >= 32;
781 return SrcSize == 32 && DestSize == 64;
784 bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const {
785 // Any register load of a 64-bit value really requires 2 32-bit moves. For all
786 // practical purposes, the extra mov 0 to load a 64-bit is free. As used,
787 // this will enable reducing 64-bit operations the 32-bit, which is always
791 return Dest == MVT::i32 ||Dest == MVT::i64 ;
793 return Src == MVT::i32 && Dest == MVT::i64;
796 bool AMDGPUTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
797 return isZExtFree(Val.getValueType(), VT2);
800 bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const {
801 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a
802 // limited number of native 64-bit operations. Shrinking an operation to fit
803 // in a single 32-bit register should always be helpful. As currently used,
804 // this is much less general than the name suggests, and is only used in
805 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is
806 // not profitable, and may actually be harmful.
807 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32;
810 //===---------------------------------------------------------------------===//
811 // TargetLowering Callbacks
812 //===---------------------------------------------------------------------===//
814 CCAssignFn *AMDGPUCallLowering::CCAssignFnForCall(CallingConv::ID CC,
817 case CallingConv::AMDGPU_KERNEL:
818 case CallingConv::SPIR_KERNEL:
819 return CC_AMDGPU_Kernel;
820 case CallingConv::AMDGPU_VS:
821 case CallingConv::AMDGPU_GS:
822 case CallingConv::AMDGPU_PS:
823 case CallingConv::AMDGPU_CS:
824 case CallingConv::AMDGPU_HS:
827 case CallingConv::Fast:
828 return CC_AMDGPU_Func;
830 report_fatal_error("Unsupported calling convention.");
834 CCAssignFn *AMDGPUCallLowering::CCAssignFnForReturn(CallingConv::ID CC,
837 case CallingConv::AMDGPU_KERNEL:
838 case CallingConv::SPIR_KERNEL:
839 return CC_AMDGPU_Kernel;
840 case CallingConv::AMDGPU_VS:
841 case CallingConv::AMDGPU_GS:
842 case CallingConv::AMDGPU_PS:
843 case CallingConv::AMDGPU_CS:
844 case CallingConv::AMDGPU_HS:
845 return RetCC_SI_Shader;
847 case CallingConv::Fast:
848 return RetCC_AMDGPU_Func;
850 report_fatal_error("Unsupported calling convention.");
854 /// The SelectionDAGBuilder will automatically promote function arguments
855 /// with illegal types. However, this does not work for the AMDGPU targets
856 /// since the function arguments are stored in memory as these illegal types.
857 /// In order to handle this properly we need to get the original types sizes
858 /// from the LLVM IR Function and fixup the ISD:InputArg values before
859 /// passing them to AnalyzeFormalArguments()
861 /// When the SelectionDAGBuilder computes the Ins, it takes care of splitting
862 /// input values across multiple registers. Each item in the Ins array
863 /// represents a single value that will be stored in regsters. Ins[x].VT is
864 /// the value type of the value that will be stored in the register, so
865 /// whatever SDNode we lower the argument to needs to be this type.
867 /// In order to correctly lower the arguments we need to know the size of each
868 /// argument. Since Ins[x].VT gives us the size of the register that will
869 /// hold the value, we need to look at Ins[x].ArgVT to see the 'real' type
870 /// for the orignal function argument so that we can deduce the correct memory
871 /// type to use for Ins[x]. In most cases the correct memory type will be
872 /// Ins[x].ArgVT. However, this will not always be the case. If, for example,
873 /// we have a kernel argument of type v8i8, this argument will be split into
874 /// 8 parts and each part will be represented by its own item in the Ins array.
875 /// For each part the Ins[x].ArgVT will be the v8i8, which is the full type of
876 /// the argument before it was split. From this, we deduce that the memory type
877 /// for each individual part is i8. We pass the memory type as LocVT to the
878 /// calling convention analysis function and the register type (Ins[x].VT) as
880 void AMDGPUTargetLowering::analyzeFormalArgumentsCompute(CCState &State,
881 const SmallVectorImpl<ISD::InputArg> &Ins) const {
882 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
883 const ISD::InputArg &In = Ins[i];
886 unsigned NumRegs = getNumRegisters(State.getContext(), In.ArgVT);
888 if (!Subtarget->isAmdHsaOS() &&
889 (In.ArgVT == MVT::i16 || In.ArgVT == MVT::i8 || In.ArgVT == MVT::f16)) {
890 // The ABI says the caller will extend these values to 32-bits.
891 MemVT = In.ArgVT.isInteger() ? MVT::i32 : MVT::f32;
892 } else if (NumRegs == 1) {
893 // This argument is not split, so the IR type is the memory type.
894 assert(!In.Flags.isSplit());
895 if (In.ArgVT.isExtended()) {
896 // We have an extended type, like i24, so we should just use the register type
901 } else if (In.ArgVT.isVector() && In.VT.isVector() &&
902 In.ArgVT.getScalarType() == In.VT.getScalarType()) {
903 assert(In.ArgVT.getVectorNumElements() > In.VT.getVectorNumElements());
904 // We have a vector value which has been split into a vector with
905 // the same scalar type, but fewer elements. This should handle
906 // all the floating-point vector types.
908 } else if (In.ArgVT.isVector() &&
909 In.ArgVT.getVectorNumElements() == NumRegs) {
910 // This arg has been split so that each element is stored in a separate
912 MemVT = In.ArgVT.getScalarType();
913 } else if (In.ArgVT.isExtended()) {
914 // We have an extended type, like i65.
917 unsigned MemoryBits = In.ArgVT.getStoreSizeInBits() / NumRegs;
918 assert(In.ArgVT.getStoreSizeInBits() % NumRegs == 0);
919 if (In.VT.isInteger()) {
920 MemVT = EVT::getIntegerVT(State.getContext(), MemoryBits);
921 } else if (In.VT.isVector()) {
922 assert(!In.VT.getScalarType().isFloatingPoint());
923 unsigned NumElements = In.VT.getVectorNumElements();
924 assert(MemoryBits % NumElements == 0);
925 // This vector type has been split into another vector type with
926 // a different elements size.
927 EVT ScalarVT = EVT::getIntegerVT(State.getContext(),
928 MemoryBits / NumElements);
929 MemVT = EVT::getVectorVT(State.getContext(), ScalarVT, NumElements);
931 llvm_unreachable("cannot deduce memory type.");
935 // Convert one element vectors to scalar.
936 if (MemVT.isVector() && MemVT.getVectorNumElements() == 1)
937 MemVT = MemVT.getScalarType();
939 if (MemVT.isExtended()) {
940 // This should really only happen if we have vec3 arguments
941 assert(MemVT.isVector() && MemVT.getVectorNumElements() == 3);
942 MemVT = MemVT.getPow2VectorType(State.getContext());
945 assert(MemVT.isSimple());
946 allocateKernArg(i, In.VT, MemVT.getSimpleVT(), CCValAssign::Full, In.Flags,
951 SDValue AMDGPUTargetLowering::LowerReturn(
952 SDValue Chain, CallingConv::ID CallConv,
954 const SmallVectorImpl<ISD::OutputArg> &Outs,
955 const SmallVectorImpl<SDValue> &OutVals,
956 const SDLoc &DL, SelectionDAG &DAG) const {
957 // FIXME: Fails for r600 tests
958 //assert(!isVarArg && Outs.empty() && OutVals.empty() &&
959 // "wave terminate should not have return values");
960 return DAG.getNode(AMDGPUISD::ENDPGM, DL, MVT::Other, Chain);
963 //===---------------------------------------------------------------------===//
964 // Target specific lowering
965 //===---------------------------------------------------------------------===//
967 /// Selects the correct CCAssignFn for a given CallingConvention value.
968 CCAssignFn *AMDGPUTargetLowering::CCAssignFnForCall(CallingConv::ID CC,
970 return AMDGPUCallLowering::CCAssignFnForCall(CC, IsVarArg);
973 CCAssignFn *AMDGPUTargetLowering::CCAssignFnForReturn(CallingConv::ID CC,
975 return AMDGPUCallLowering::CCAssignFnForReturn(CC, IsVarArg);
978 SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI,
979 SmallVectorImpl<SDValue> &InVals) const {
980 SDValue Callee = CLI.Callee;
981 SelectionDAG &DAG = CLI.DAG;
983 const Function &Fn = *DAG.getMachineFunction().getFunction();
985 StringRef FuncName("<unknown>");
987 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee))
988 FuncName = G->getSymbol();
989 else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
990 FuncName = G->getGlobal()->getName();
992 DiagnosticInfoUnsupported NoCalls(
993 Fn, "unsupported call to function " + FuncName, CLI.DL.getDebugLoc());
994 DAG.getContext()->diagnose(NoCalls);
996 if (!CLI.IsTailCall) {
997 for (unsigned I = 0, E = CLI.Ins.size(); I != E; ++I)
998 InVals.push_back(DAG.getUNDEF(CLI.Ins[I].VT));
1001 return DAG.getEntryNode();
1004 SDValue AMDGPUTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1005 SelectionDAG &DAG) const {
1006 const Function &Fn = *DAG.getMachineFunction().getFunction();
1008 DiagnosticInfoUnsupported NoDynamicAlloca(Fn, "unsupported dynamic alloca",
1009 SDLoc(Op).getDebugLoc());
1010 DAG.getContext()->diagnose(NoDynamicAlloca);
1011 auto Ops = {DAG.getConstant(0, SDLoc(), Op.getValueType()), Op.getOperand(0)};
1012 return DAG.getMergeValues(Ops, SDLoc());
1015 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op,
1016 SelectionDAG &DAG) const {
1017 switch (Op.getOpcode()) {
1019 Op->print(errs(), &DAG);
1020 llvm_unreachable("Custom lowering code for this"
1021 "instruction is not implemented yet!");
1023 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
1024 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
1025 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
1026 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
1027 case ISD::SDIVREM: return LowerSDIVREM(Op, DAG);
1028 case ISD::FREM: return LowerFREM(Op, DAG);
1029 case ISD::FCEIL: return LowerFCEIL(Op, DAG);
1030 case ISD::FTRUNC: return LowerFTRUNC(Op, DAG);
1031 case ISD::FRINT: return LowerFRINT(Op, DAG);
1032 case ISD::FNEARBYINT: return LowerFNEARBYINT(Op, DAG);
1033 case ISD::FROUND: return LowerFROUND(Op, DAG);
1034 case ISD::FFLOOR: return LowerFFLOOR(Op, DAG);
1035 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
1036 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
1037 case ISD::FP_TO_FP16: return LowerFP_TO_FP16(Op, DAG);
1038 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
1039 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
1041 case ISD::CTLZ_ZERO_UNDEF:
1042 return LowerCTLZ(Op, DAG);
1043 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
1048 void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N,
1049 SmallVectorImpl<SDValue> &Results,
1050 SelectionDAG &DAG) const {
1051 switch (N->getOpcode()) {
1052 case ISD::SIGN_EXTEND_INREG:
1053 // Different parts of legalization seem to interpret which type of
1054 // sign_extend_inreg is the one to check for custom lowering. The extended
1055 // from type is what really matters, but some places check for custom
1056 // lowering of the result type. This results in trying to use
1057 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do
1058 // nothing here and let the illegal result integer be handled normally.
1065 static bool hasDefinedInitializer(const GlobalValue *GV) {
1066 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1067 if (!GVar || !GVar->hasInitializer())
1070 return !isa<UndefValue>(GVar->getInitializer());
1073 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
1075 SelectionDAG &DAG) const {
1077 const DataLayout &DL = DAG.getDataLayout();
1078 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
1079 const GlobalValue *GV = G->getGlobal();
1081 if (G->getAddressSpace() == AMDGPUASI.LOCAL_ADDRESS) {
1082 // XXX: What does the value of G->getOffset() mean?
1083 assert(G->getOffset() == 0 &&
1084 "Do not know what to do with an non-zero offset");
1086 // TODO: We could emit code to handle the initialization somewhere.
1087 if (!hasDefinedInitializer(GV)) {
1088 unsigned Offset = MFI->allocateLDSGlobal(DL, *GV);
1089 return DAG.getConstant(Offset, SDLoc(Op), Op.getValueType());
1093 const Function &Fn = *DAG.getMachineFunction().getFunction();
1094 DiagnosticInfoUnsupported BadInit(
1095 Fn, "unsupported initializer for address space", SDLoc(Op).getDebugLoc());
1096 DAG.getContext()->diagnose(BadInit);
1100 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
1101 SelectionDAG &DAG) const {
1102 SmallVector<SDValue, 8> Args;
1104 for (const SDUse &U : Op->ops())
1105 DAG.ExtractVectorElements(U.get(), Args);
1107 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
1110 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
1111 SelectionDAG &DAG) const {
1113 SmallVector<SDValue, 8> Args;
1114 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1115 EVT VT = Op.getValueType();
1116 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start,
1117 VT.getVectorNumElements());
1119 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
1122 /// \brief Generate Min/Max node
1123 SDValue AMDGPUTargetLowering::combineFMinMaxLegacy(const SDLoc &DL, EVT VT,
1124 SDValue LHS, SDValue RHS,
1125 SDValue True, SDValue False,
1127 DAGCombinerInfo &DCI) const {
1128 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True))
1131 SelectionDAG &DAG = DCI.DAG;
1132 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1141 case ISD::SETFALSE2:
1150 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1151 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1157 // Ordered. Assume ordered for undefined.
1159 // Only do this after legalization to avoid interfering with other combines
1160 // which might occur.
1161 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1162 !DCI.isCalledByLegalizer())
1165 // We need to permute the operands to get the correct NaN behavior. The
1166 // selected operand is the second one based on the failing compare with NaN,
1167 // so permute it based on the compare type the hardware uses.
1169 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
1170 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
1175 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
1176 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
1182 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1183 !DCI.isCalledByLegalizer())
1187 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1188 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1190 case ISD::SETCC_INVALID:
1191 llvm_unreachable("Invalid setcc condcode!");
1196 std::pair<SDValue, SDValue>
1197 AMDGPUTargetLowering::split64BitValue(SDValue Op, SelectionDAG &DAG) const {
1200 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1202 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1203 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1205 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1206 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1208 return std::make_pair(Lo, Hi);
1211 SDValue AMDGPUTargetLowering::getLoHalf64(SDValue Op, SelectionDAG &DAG) const {
1214 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1215 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1216 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1219 SDValue AMDGPUTargetLowering::getHiHalf64(SDValue Op, SelectionDAG &DAG) const {
1222 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1223 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1224 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1227 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue Op,
1228 SelectionDAG &DAG) const {
1229 LoadSDNode *Load = cast<LoadSDNode>(Op);
1230 EVT VT = Op.getValueType();
1233 // If this is a 2 element vector, we really want to scalarize and not create
1234 // weird 1 element vectors.
1235 if (VT.getVectorNumElements() == 2)
1236 return scalarizeVectorLoad(Load, DAG);
1238 SDValue BasePtr = Load->getBasePtr();
1239 EVT PtrVT = BasePtr.getValueType();
1240 EVT MemVT = Load->getMemoryVT();
1243 const MachinePointerInfo &SrcValue = Load->getMemOperand()->getPointerInfo();
1246 EVT LoMemVT, HiMemVT;
1249 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1250 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1251 std::tie(Lo, Hi) = DAG.SplitVector(Op, SL, LoVT, HiVT);
1253 unsigned Size = LoMemVT.getStoreSize();
1254 unsigned BaseAlign = Load->getAlignment();
1255 unsigned HiAlign = MinAlign(BaseAlign, Size);
1257 SDValue LoLoad = DAG.getExtLoad(Load->getExtensionType(), SL, LoVT,
1258 Load->getChain(), BasePtr, SrcValue, LoMemVT,
1259 BaseAlign, Load->getMemOperand()->getFlags());
1260 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
1261 DAG.getConstant(Size, SL, PtrVT));
1263 DAG.getExtLoad(Load->getExtensionType(), SL, HiVT, Load->getChain(),
1264 HiPtr, SrcValue.getWithOffset(LoMemVT.getStoreSize()),
1265 HiMemVT, HiAlign, Load->getMemOperand()->getFlags());
1268 DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, LoLoad, HiLoad),
1269 DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
1270 LoLoad.getValue(1), HiLoad.getValue(1))
1273 return DAG.getMergeValues(Ops, SL);
1276 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
1277 SelectionDAG &DAG) const {
1278 StoreSDNode *Store = cast<StoreSDNode>(Op);
1279 SDValue Val = Store->getValue();
1280 EVT VT = Val.getValueType();
1282 // If this is a 2 element vector, we really want to scalarize and not create
1283 // weird 1 element vectors.
1284 if (VT.getVectorNumElements() == 2)
1285 return scalarizeVectorStore(Store, DAG);
1287 EVT MemVT = Store->getMemoryVT();
1288 SDValue Chain = Store->getChain();
1289 SDValue BasePtr = Store->getBasePtr();
1293 EVT LoMemVT, HiMemVT;
1296 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1297 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1298 std::tie(Lo, Hi) = DAG.SplitVector(Val, SL, LoVT, HiVT);
1300 EVT PtrVT = BasePtr.getValueType();
1301 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
1302 DAG.getConstant(LoMemVT.getStoreSize(), SL,
1305 const MachinePointerInfo &SrcValue = Store->getMemOperand()->getPointerInfo();
1306 unsigned BaseAlign = Store->getAlignment();
1307 unsigned Size = LoMemVT.getStoreSize();
1308 unsigned HiAlign = MinAlign(BaseAlign, Size);
1311 DAG.getTruncStore(Chain, SL, Lo, BasePtr, SrcValue, LoMemVT, BaseAlign,
1312 Store->getMemOperand()->getFlags());
1314 DAG.getTruncStore(Chain, SL, Hi, HiPtr, SrcValue.getWithOffset(Size),
1315 HiMemVT, HiAlign, Store->getMemOperand()->getFlags());
1317 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoStore, HiStore);
1320 // This is a shortcut for integer division because we have fast i32<->f32
1321 // conversions, and fast f32 reciprocal instructions. The fractional part of a
1322 // float is enough to accurately represent up to a 24-bit signed integer.
1323 SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG,
1326 EVT VT = Op.getValueType();
1327 SDValue LHS = Op.getOperand(0);
1328 SDValue RHS = Op.getOperand(1);
1329 MVT IntVT = MVT::i32;
1330 MVT FltVT = MVT::f32;
1332 unsigned LHSSignBits = DAG.ComputeNumSignBits(LHS);
1333 if (LHSSignBits < 9)
1336 unsigned RHSSignBits = DAG.ComputeNumSignBits(RHS);
1337 if (RHSSignBits < 9)
1340 unsigned BitSize = VT.getSizeInBits();
1341 unsigned SignBits = std::min(LHSSignBits, RHSSignBits);
1342 unsigned DivBits = BitSize - SignBits;
1346 ISD::NodeType ToFp = Sign ? ISD::SINT_TO_FP : ISD::UINT_TO_FP;
1347 ISD::NodeType ToInt = Sign ? ISD::FP_TO_SINT : ISD::FP_TO_UINT;
1349 SDValue jq = DAG.getConstant(1, DL, IntVT);
1352 // char|short jq = ia ^ ib;
1353 jq = DAG.getNode(ISD::XOR, DL, VT, LHS, RHS);
1355 // jq = jq >> (bitsize - 2)
1356 jq = DAG.getNode(ISD::SRA, DL, VT, jq,
1357 DAG.getConstant(BitSize - 2, DL, VT));
1360 jq = DAG.getNode(ISD::OR, DL, VT, jq, DAG.getConstant(1, DL, VT));
1363 // int ia = (int)LHS;
1366 // int ib, (int)RHS;
1369 // float fa = (float)ia;
1370 SDValue fa = DAG.getNode(ToFp, DL, FltVT, ia);
1372 // float fb = (float)ib;
1373 SDValue fb = DAG.getNode(ToFp, DL, FltVT, ib);
1375 SDValue fq = DAG.getNode(ISD::FMUL, DL, FltVT,
1376 fa, DAG.getNode(AMDGPUISD::RCP, DL, FltVT, fb));
1379 fq = DAG.getNode(ISD::FTRUNC, DL, FltVT, fq);
1381 // float fqneg = -fq;
1382 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FltVT, fq);
1384 // float fr = mad(fqneg, fb, fa);
1385 unsigned OpCode = Subtarget->hasFP32Denormals() ?
1386 (unsigned)AMDGPUISD::FMAD_FTZ :
1387 (unsigned)ISD::FMAD;
1388 SDValue fr = DAG.getNode(OpCode, DL, FltVT, fqneg, fb, fa);
1390 // int iq = (int)fq;
1391 SDValue iq = DAG.getNode(ToInt, DL, IntVT, fq);
1394 fr = DAG.getNode(ISD::FABS, DL, FltVT, fr);
1397 fb = DAG.getNode(ISD::FABS, DL, FltVT, fb);
1399 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
1401 // int cv = fr >= fb;
1402 SDValue cv = DAG.getSetCC(DL, SetCCVT, fr, fb, ISD::SETOGE);
1404 // jq = (cv ? jq : 0);
1405 jq = DAG.getNode(ISD::SELECT, DL, VT, cv, jq, DAG.getConstant(0, DL, VT));
1408 SDValue Div = DAG.getNode(ISD::ADD, DL, VT, iq, jq);
1410 // Rem needs compensation, it's easier to recompute it
1411 SDValue Rem = DAG.getNode(ISD::MUL, DL, VT, Div, RHS);
1412 Rem = DAG.getNode(ISD::SUB, DL, VT, LHS, Rem);
1414 // Truncate to number of bits this divide really is.
1417 = DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), DivBits));
1418 Div = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Div, InRegSize);
1419 Rem = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Rem, InRegSize);
1421 SDValue TruncMask = DAG.getConstant((UINT64_C(1) << DivBits) - 1, DL, VT);
1422 Div = DAG.getNode(ISD::AND, DL, VT, Div, TruncMask);
1423 Rem = DAG.getNode(ISD::AND, DL, VT, Rem, TruncMask);
1426 return DAG.getMergeValues({ Div, Rem }, DL);
1429 void AMDGPUTargetLowering::LowerUDIVREM64(SDValue Op,
1431 SmallVectorImpl<SDValue> &Results) const {
1432 assert(Op.getValueType() == MVT::i64);
1435 EVT VT = Op.getValueType();
1436 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1438 SDValue one = DAG.getConstant(1, DL, HalfVT);
1439 SDValue zero = DAG.getConstant(0, DL, HalfVT);
1442 SDValue LHS = Op.getOperand(0);
1443 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero);
1444 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one);
1446 SDValue RHS = Op.getOperand(1);
1447 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero);
1448 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one);
1450 if (VT == MVT::i64 &&
1451 DAG.MaskedValueIsZero(RHS, APInt::getHighBitsSet(64, 32)) &&
1452 DAG.MaskedValueIsZero(LHS, APInt::getHighBitsSet(64, 32))) {
1454 SDValue Res = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1457 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(0), zero});
1458 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(1), zero});
1460 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV));
1461 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM));
1465 // Get Speculative values
1466 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo);
1467 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo);
1469 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ);
1470 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {REM_Lo, zero});
1471 REM = DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM);
1473 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ);
1474 SDValue DIV_Lo = zero;
1476 const unsigned halfBitWidth = HalfVT.getSizeInBits();
1478 for (unsigned i = 0; i < halfBitWidth; ++i) {
1479 const unsigned bitPos = halfBitWidth - i - 1;
1480 SDValue POS = DAG.getConstant(bitPos, DL, HalfVT);
1481 // Get value of high bit
1482 SDValue HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS);
1483 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one);
1484 HBit = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, HBit);
1487 REM = DAG.getNode(ISD::SHL, DL, VT, REM, DAG.getConstant(1, DL, VT));
1489 REM = DAG.getNode(ISD::OR, DL, VT, REM, HBit);
1491 SDValue BIT = DAG.getConstant(1ULL << bitPos, DL, HalfVT);
1492 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETUGE);
1494 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT);
1497 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS);
1498 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETUGE);
1501 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {DIV_Lo, DIV_Hi});
1502 DIV = DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV);
1503 Results.push_back(DIV);
1504 Results.push_back(REM);
1507 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
1508 SelectionDAG &DAG) const {
1510 EVT VT = Op.getValueType();
1512 if (VT == MVT::i64) {
1513 SmallVector<SDValue, 2> Results;
1514 LowerUDIVREM64(Op, DAG, Results);
1515 return DAG.getMergeValues(Results, DL);
1518 if (VT == MVT::i32) {
1519 if (SDValue Res = LowerDIVREM24(Op, DAG, false))
1523 SDValue Num = Op.getOperand(0);
1524 SDValue Den = Op.getOperand(1);
1526 // RCP = URECIP(Den) = 2^32 / Den + e
1527 // e is rounding error.
1528 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
1530 // RCP_LO = mul(RCP, Den) */
1531 SDValue RCP_LO = DAG.getNode(ISD::MUL, DL, VT, RCP, Den);
1533 // RCP_HI = mulhu (RCP, Den) */
1534 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
1536 // NEG_RCP_LO = -RCP_LO
1537 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
1540 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
1541 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
1544 // Calculate the rounding error from the URECIP instruction
1545 // E = mulhu(ABS_RCP_LO, RCP)
1546 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
1548 // RCP_A_E = RCP + E
1549 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
1551 // RCP_S_E = RCP - E
1552 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
1554 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
1555 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
1558 // Quotient = mulhu(Tmp0, Num)
1559 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
1561 // Num_S_Remainder = Quotient * Den
1562 SDValue Num_S_Remainder = DAG.getNode(ISD::MUL, DL, VT, Quotient, Den);
1564 // Remainder = Num - Num_S_Remainder
1565 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
1567 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
1568 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
1569 DAG.getConstant(-1, DL, VT),
1570 DAG.getConstant(0, DL, VT),
1572 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
1573 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
1575 DAG.getConstant(-1, DL, VT),
1576 DAG.getConstant(0, DL, VT),
1578 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
1579 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
1582 // Calculate Division result:
1584 // Quotient_A_One = Quotient + 1
1585 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
1586 DAG.getConstant(1, DL, VT));
1588 // Quotient_S_One = Quotient - 1
1589 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
1590 DAG.getConstant(1, DL, VT));
1592 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
1593 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
1594 Quotient, Quotient_A_One, ISD::SETEQ);
1596 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
1597 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
1598 Quotient_S_One, Div, ISD::SETEQ);
1600 // Calculate Rem result:
1602 // Remainder_S_Den = Remainder - Den
1603 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
1605 // Remainder_A_Den = Remainder + Den
1606 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
1608 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
1609 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
1610 Remainder, Remainder_S_Den, ISD::SETEQ);
1612 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
1613 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
1614 Remainder_A_Den, Rem, ISD::SETEQ);
1619 return DAG.getMergeValues(Ops, DL);
1622 SDValue AMDGPUTargetLowering::LowerSDIVREM(SDValue Op,
1623 SelectionDAG &DAG) const {
1625 EVT VT = Op.getValueType();
1627 SDValue LHS = Op.getOperand(0);
1628 SDValue RHS = Op.getOperand(1);
1630 SDValue Zero = DAG.getConstant(0, DL, VT);
1631 SDValue NegOne = DAG.getConstant(-1, DL, VT);
1633 if (VT == MVT::i32) {
1634 if (SDValue Res = LowerDIVREM24(Op, DAG, true))
1638 if (VT == MVT::i64 &&
1639 DAG.ComputeNumSignBits(LHS) > 32 &&
1640 DAG.ComputeNumSignBits(RHS) > 32) {
1641 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1644 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero);
1645 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero);
1646 SDValue DIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1649 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(0)),
1650 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(1))
1652 return DAG.getMergeValues(Res, DL);
1655 SDValue LHSign = DAG.getSelectCC(DL, LHS, Zero, NegOne, Zero, ISD::SETLT);
1656 SDValue RHSign = DAG.getSelectCC(DL, RHS, Zero, NegOne, Zero, ISD::SETLT);
1657 SDValue DSign = DAG.getNode(ISD::XOR, DL, VT, LHSign, RHSign);
1658 SDValue RSign = LHSign; // Remainder sign is the same as LHS
1660 LHS = DAG.getNode(ISD::ADD, DL, VT, LHS, LHSign);
1661 RHS = DAG.getNode(ISD::ADD, DL, VT, RHS, RHSign);
1663 LHS = DAG.getNode(ISD::XOR, DL, VT, LHS, LHSign);
1664 RHS = DAG.getNode(ISD::XOR, DL, VT, RHS, RHSign);
1666 SDValue Div = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT), LHS, RHS);
1667 SDValue Rem = Div.getValue(1);
1669 Div = DAG.getNode(ISD::XOR, DL, VT, Div, DSign);
1670 Rem = DAG.getNode(ISD::XOR, DL, VT, Rem, RSign);
1672 Div = DAG.getNode(ISD::SUB, DL, VT, Div, DSign);
1673 Rem = DAG.getNode(ISD::SUB, DL, VT, Rem, RSign);
1679 return DAG.getMergeValues(Res, DL);
1682 // (frem x, y) -> (fsub x, (fmul (ftrunc (fdiv x, y)), y))
1683 SDValue AMDGPUTargetLowering::LowerFREM(SDValue Op, SelectionDAG &DAG) const {
1685 EVT VT = Op.getValueType();
1686 SDValue X = Op.getOperand(0);
1687 SDValue Y = Op.getOperand(1);
1689 // TODO: Should this propagate fast-math-flags?
1691 SDValue Div = DAG.getNode(ISD::FDIV, SL, VT, X, Y);
1692 SDValue Floor = DAG.getNode(ISD::FTRUNC, SL, VT, Div);
1693 SDValue Mul = DAG.getNode(ISD::FMUL, SL, VT, Floor, Y);
1695 return DAG.getNode(ISD::FSUB, SL, VT, X, Mul);
1698 SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const {
1700 SDValue Src = Op.getOperand(0);
1702 // result = trunc(src)
1703 // if (src > 0.0 && src != result)
1706 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1708 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1709 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
1712 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
1714 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT);
1715 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1716 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1718 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero);
1719 // TODO: Should this propagate fast-math-flags?
1720 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1723 static SDValue extractF64Exponent(SDValue Hi, const SDLoc &SL,
1724 SelectionDAG &DAG) {
1725 const unsigned FractBits = 52;
1726 const unsigned ExpBits = 11;
1728 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32,
1730 DAG.getConstant(FractBits - 32, SL, MVT::i32),
1731 DAG.getConstant(ExpBits, SL, MVT::i32));
1732 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart,
1733 DAG.getConstant(1023, SL, MVT::i32));
1738 SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const {
1740 SDValue Src = Op.getOperand(0);
1742 assert(Op.getValueType() == MVT::f64);
1744 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1745 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1747 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1749 // Extract the upper half, since this is where we will find the sign and
1751 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One);
1753 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
1755 const unsigned FractBits = 52;
1757 // Extract the sign bit.
1758 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, SL, MVT::i32);
1759 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask);
1761 // Extend back to to 64-bits.
1762 SDValue SignBit64 = DAG.getBuildVector(MVT::v2i32, SL, {Zero, SignBit});
1763 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64);
1765 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src);
1766 const SDValue FractMask
1767 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, SL, MVT::i64);
1769 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp);
1770 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64);
1771 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not);
1774 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
1776 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, SL, MVT::i32);
1778 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1779 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1781 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0);
1782 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1);
1784 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2);
1787 SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const {
1789 SDValue Src = Op.getOperand(0);
1791 assert(Op.getValueType() == MVT::f64);
1793 APFloat C1Val(APFloat::IEEEdouble(), "0x1.0p+52");
1794 SDValue C1 = DAG.getConstantFP(C1Val, SL, MVT::f64);
1795 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src);
1797 // TODO: Should this propagate fast-math-flags?
1799 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign);
1800 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign);
1802 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src);
1804 APFloat C2Val(APFloat::IEEEdouble(), "0x1.fffffffffffffp+51");
1805 SDValue C2 = DAG.getConstantFP(C2Val, SL, MVT::f64);
1808 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
1809 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT);
1811 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2);
1814 SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const {
1815 // FNEARBYINT and FRINT are the same, except in their handling of FP
1816 // exceptions. Those aren't really meaningful for us, and OpenCL only has
1817 // rint, so just treat them as equivalent.
1818 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0));
1821 // XXX - May require not supporting f32 denormals?
1823 // Don't handle v2f16. The extra instructions to scalarize and repack around the
1824 // compare and vselect end up producing worse code than scalarizing the whole
1826 SDValue AMDGPUTargetLowering::LowerFROUND32_16(SDValue Op, SelectionDAG &DAG) const {
1828 SDValue X = Op.getOperand(0);
1829 EVT VT = Op.getValueType();
1831 SDValue T = DAG.getNode(ISD::FTRUNC, SL, VT, X);
1833 // TODO: Should this propagate fast-math-flags?
1835 SDValue Diff = DAG.getNode(ISD::FSUB, SL, VT, X, T);
1837 SDValue AbsDiff = DAG.getNode(ISD::FABS, SL, VT, Diff);
1839 const SDValue Zero = DAG.getConstantFP(0.0, SL, VT);
1840 const SDValue One = DAG.getConstantFP(1.0, SL, VT);
1841 const SDValue Half = DAG.getConstantFP(0.5, SL, VT);
1843 SDValue SignOne = DAG.getNode(ISD::FCOPYSIGN, SL, VT, One, X);
1846 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
1848 SDValue Cmp = DAG.getSetCC(SL, SetCCVT, AbsDiff, Half, ISD::SETOGE);
1850 SDValue Sel = DAG.getNode(ISD::SELECT, SL, VT, Cmp, SignOne, Zero);
1852 return DAG.getNode(ISD::FADD, SL, VT, T, Sel);
1855 SDValue AMDGPUTargetLowering::LowerFROUND64(SDValue Op, SelectionDAG &DAG) const {
1857 SDValue X = Op.getOperand(0);
1859 SDValue L = DAG.getNode(ISD::BITCAST, SL, MVT::i64, X);
1861 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1862 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1863 const SDValue NegOne = DAG.getConstant(-1, SL, MVT::i32);
1864 const SDValue FiftyOne = DAG.getConstant(51, SL, MVT::i32);
1866 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
1868 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1870 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, One);
1872 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
1874 const SDValue Mask = DAG.getConstant(INT64_C(0x000fffffffffffff), SL,
1877 SDValue M = DAG.getNode(ISD::SRA, SL, MVT::i64, Mask, Exp);
1878 SDValue D = DAG.getNode(ISD::SRA, SL, MVT::i64,
1879 DAG.getConstant(INT64_C(0x0008000000000000), SL,
1883 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, L, M);
1884 SDValue Tmp1 = DAG.getSetCC(SL, SetCCVT,
1885 DAG.getConstant(0, SL, MVT::i64), Tmp0,
1888 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, Tmp1,
1889 D, DAG.getConstant(0, SL, MVT::i64));
1890 SDValue K = DAG.getNode(ISD::ADD, SL, MVT::i64, L, Tmp2);
1892 K = DAG.getNode(ISD::AND, SL, MVT::i64, K, DAG.getNOT(SL, M, MVT::i64));
1893 K = DAG.getNode(ISD::BITCAST, SL, MVT::f64, K);
1895 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1896 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1897 SDValue ExpEqNegOne = DAG.getSetCC(SL, SetCCVT, NegOne, Exp, ISD::SETEQ);
1899 SDValue Mag = DAG.getNode(ISD::SELECT, SL, MVT::f64,
1901 DAG.getConstantFP(1.0, SL, MVT::f64),
1902 DAG.getConstantFP(0.0, SL, MVT::f64));
1904 SDValue S = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, Mag, X);
1906 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpLt0, S, K);
1907 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpGt51, X, K);
1912 SDValue AMDGPUTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const {
1913 EVT VT = Op.getValueType();
1915 if (VT == MVT::f32 || VT == MVT::f16)
1916 return LowerFROUND32_16(Op, DAG);
1919 return LowerFROUND64(Op, DAG);
1921 llvm_unreachable("unhandled type");
1924 SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const {
1926 SDValue Src = Op.getOperand(0);
1928 // result = trunc(src);
1929 // if (src < 0.0 && src != result)
1932 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1934 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1935 const SDValue NegOne = DAG.getConstantFP(-1.0, SL, MVT::f64);
1938 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
1940 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT);
1941 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1942 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1944 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero);
1945 // TODO: Should this propagate fast-math-flags?
1946 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1949 SDValue AMDGPUTargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
1951 SDValue Src = Op.getOperand(0);
1952 bool ZeroUndef = Op.getOpcode() == ISD::CTLZ_ZERO_UNDEF;
1954 if (ZeroUndef && Src.getValueType() == MVT::i32)
1955 return DAG.getNode(AMDGPUISD::FFBH_U32, SL, MVT::i32, Src);
1957 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1959 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1960 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1962 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1963 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1965 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1966 *DAG.getContext(), MVT::i32);
1968 SDValue Hi0 = DAG.getSetCC(SL, SetCCVT, Hi, Zero, ISD::SETEQ);
1970 SDValue CtlzLo = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Lo);
1971 SDValue CtlzHi = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Hi);
1973 const SDValue Bits32 = DAG.getConstant(32, SL, MVT::i32);
1974 SDValue Add = DAG.getNode(ISD::ADD, SL, MVT::i32, CtlzLo, Bits32);
1976 // ctlz(x) = hi_32(x) == 0 ? ctlz(lo_32(x)) + 32 : ctlz(hi_32(x))
1977 SDValue NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0, Add, CtlzHi);
1980 // Test if the full 64-bit input is zero.
1982 // FIXME: DAG combines turn what should be an s_and_b64 into a v_or_b32,
1983 // which we probably don't want.
1984 SDValue Lo0 = DAG.getSetCC(SL, SetCCVT, Lo, Zero, ISD::SETEQ);
1985 SDValue SrcIsZero = DAG.getNode(ISD::AND, SL, SetCCVT, Lo0, Hi0);
1987 // TODO: If i64 setcc is half rate, it can result in 1 fewer instruction
1988 // with the same cycles, otherwise it is slower.
1989 // SDValue SrcIsZero = DAG.getSetCC(SL, SetCCVT, Src,
1990 // DAG.getConstant(0, SL, MVT::i64), ISD::SETEQ);
1992 const SDValue Bits32 = DAG.getConstant(64, SL, MVT::i32);
1994 // The instruction returns -1 for 0 input, but the defined intrinsic
1995 // behavior is to return the number of bits.
1996 NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32,
1997 SrcIsZero, Bits32, NewCtlz);
2000 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, NewCtlz);
2003 SDValue AMDGPUTargetLowering::LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG,
2004 bool Signed) const {
2008 // uint lz = clz(u);
2009 // uint e = (u != 0) ? 127U + 63U - lz : 0;
2010 // u = (u << lz) & 0x7fffffffffffffffUL;
2011 // ulong t = u & 0xffffffffffUL;
2012 // uint v = (e << 23) | (uint)(u >> 40);
2013 // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U);
2014 // return as_float(v + r);
2019 // long s = l >> 63;
2020 // float r = cul2f((l + s) ^ s);
2021 // return s ? -r : r;
2025 SDValue Src = Op.getOperand(0);
2030 const SDValue SignBit = DAG.getConstant(63, SL, MVT::i64);
2031 S = DAG.getNode(ISD::SRA, SL, MVT::i64, L, SignBit);
2033 SDValue LPlusS = DAG.getNode(ISD::ADD, SL, MVT::i64, L, S);
2034 L = DAG.getNode(ISD::XOR, SL, MVT::i64, LPlusS, S);
2037 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
2038 *DAG.getContext(), MVT::f32);
2041 SDValue ZeroI32 = DAG.getConstant(0, SL, MVT::i32);
2042 SDValue ZeroI64 = DAG.getConstant(0, SL, MVT::i64);
2043 SDValue LZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i64, L);
2044 LZ = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LZ);
2046 SDValue K = DAG.getConstant(127U + 63U, SL, MVT::i32);
2047 SDValue E = DAG.getSelect(SL, MVT::i32,
2048 DAG.getSetCC(SL, SetCCVT, L, ZeroI64, ISD::SETNE),
2049 DAG.getNode(ISD::SUB, SL, MVT::i32, K, LZ),
2052 SDValue U = DAG.getNode(ISD::AND, SL, MVT::i64,
2053 DAG.getNode(ISD::SHL, SL, MVT::i64, L, LZ),
2054 DAG.getConstant((-1ULL) >> 1, SL, MVT::i64));
2056 SDValue T = DAG.getNode(ISD::AND, SL, MVT::i64, U,
2057 DAG.getConstant(0xffffffffffULL, SL, MVT::i64));
2059 SDValue UShl = DAG.getNode(ISD::SRL, SL, MVT::i64,
2060 U, DAG.getConstant(40, SL, MVT::i64));
2062 SDValue V = DAG.getNode(ISD::OR, SL, MVT::i32,
2063 DAG.getNode(ISD::SHL, SL, MVT::i32, E, DAG.getConstant(23, SL, MVT::i32)),
2064 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, UShl));
2066 SDValue C = DAG.getConstant(0x8000000000ULL, SL, MVT::i64);
2067 SDValue RCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETUGT);
2068 SDValue TCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETEQ);
2070 SDValue One = DAG.getConstant(1, SL, MVT::i32);
2072 SDValue VTrunc1 = DAG.getNode(ISD::AND, SL, MVT::i32, V, One);
2074 SDValue R = DAG.getSelect(SL, MVT::i32,
2077 DAG.getSelect(SL, MVT::i32, TCmp, VTrunc1, ZeroI32));
2078 R = DAG.getNode(ISD::ADD, SL, MVT::i32, V, R);
2079 R = DAG.getNode(ISD::BITCAST, SL, MVT::f32, R);
2084 SDValue RNeg = DAG.getNode(ISD::FNEG, SL, MVT::f32, R);
2085 return DAG.getSelect(SL, MVT::f32, DAG.getSExtOrTrunc(S, SL, SetCCVT), RNeg, R);
2088 SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG,
2089 bool Signed) const {
2091 SDValue Src = Op.getOperand(0);
2093 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
2095 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
2096 DAG.getConstant(0, SL, MVT::i32));
2097 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
2098 DAG.getConstant(1, SL, MVT::i32));
2100 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
2103 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo);
2105 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi,
2106 DAG.getConstant(32, SL, MVT::i32));
2107 // TODO: Should this propagate fast-math-flags?
2108 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo);
2111 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
2112 SelectionDAG &DAG) const {
2113 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
2114 "operation should be legal");
2116 // TODO: Factor out code common with LowerSINT_TO_FP.
2118 EVT DestVT = Op.getValueType();
2119 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
2121 SDValue Src = Op.getOperand(0);
2123 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
2124 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
2126 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
2131 if (DestVT == MVT::f32)
2132 return LowerINT_TO_FP32(Op, DAG, false);
2134 assert(DestVT == MVT::f64);
2135 return LowerINT_TO_FP64(Op, DAG, false);
2138 SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op,
2139 SelectionDAG &DAG) const {
2140 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
2141 "operation should be legal");
2143 // TODO: Factor out code common with LowerUINT_TO_FP.
2145 EVT DestVT = Op.getValueType();
2146 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
2148 SDValue Src = Op.getOperand(0);
2150 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
2151 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
2153 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
2158 if (DestVT == MVT::f32)
2159 return LowerINT_TO_FP32(Op, DAG, true);
2161 assert(DestVT == MVT::f64);
2162 return LowerINT_TO_FP64(Op, DAG, true);
2165 SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG,
2166 bool Signed) const {
2169 SDValue Src = Op.getOperand(0);
2171 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
2173 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL,
2175 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL,
2177 // TODO: Should this propagate fast-math-flags?
2178 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0);
2180 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul);
2183 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc);
2185 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL,
2186 MVT::i32, FloorMul);
2187 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma);
2189 SDValue Result = DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi});
2191 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result);
2194 SDValue AMDGPUTargetLowering::LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const {
2196 SDValue N0 = Op.getOperand(0);
2198 // Convert to target node to get known bits
2199 if (N0.getValueType() == MVT::f32)
2200 return DAG.getNode(AMDGPUISD::FP_TO_FP16, DL, Op.getValueType(), N0);
2202 if (getTargetMachine().Options.UnsafeFPMath) {
2203 // There is a generic expand for FP_TO_FP16 with unsafe fast math.
2207 assert(N0.getSimpleValueType() == MVT::f64);
2209 // f64 -> f16 conversion using round-to-nearest-even rounding mode.
2210 const unsigned ExpMask = 0x7ff;
2211 const unsigned ExpBiasf64 = 1023;
2212 const unsigned ExpBiasf16 = 15;
2213 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2214 SDValue One = DAG.getConstant(1, DL, MVT::i32);
2215 SDValue U = DAG.getNode(ISD::BITCAST, DL, MVT::i64, N0);
2216 SDValue UH = DAG.getNode(ISD::SRL, DL, MVT::i64, U,
2217 DAG.getConstant(32, DL, MVT::i64));
2218 UH = DAG.getZExtOrTrunc(UH, DL, MVT::i32);
2219 U = DAG.getZExtOrTrunc(U, DL, MVT::i32);
2220 SDValue E = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2221 DAG.getConstant(20, DL, MVT::i64));
2222 E = DAG.getNode(ISD::AND, DL, MVT::i32, E,
2223 DAG.getConstant(ExpMask, DL, MVT::i32));
2224 // Subtract the fp64 exponent bias (1023) to get the real exponent and
2225 // add the f16 bias (15) to get the biased exponent for the f16 format.
2226 E = DAG.getNode(ISD::ADD, DL, MVT::i32, E,
2227 DAG.getConstant(-ExpBiasf64 + ExpBiasf16, DL, MVT::i32));
2229 SDValue M = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2230 DAG.getConstant(8, DL, MVT::i32));
2231 M = DAG.getNode(ISD::AND, DL, MVT::i32, M,
2232 DAG.getConstant(0xffe, DL, MVT::i32));
2234 SDValue MaskedSig = DAG.getNode(ISD::AND, DL, MVT::i32, UH,
2235 DAG.getConstant(0x1ff, DL, MVT::i32));
2236 MaskedSig = DAG.getNode(ISD::OR, DL, MVT::i32, MaskedSig, U);
2238 SDValue Lo40Set = DAG.getSelectCC(DL, MaskedSig, Zero, Zero, One, ISD::SETEQ);
2239 M = DAG.getNode(ISD::OR, DL, MVT::i32, M, Lo40Set);
2241 // (M != 0 ? 0x0200 : 0) | 0x7c00;
2242 SDValue I = DAG.getNode(ISD::OR, DL, MVT::i32,
2243 DAG.getSelectCC(DL, M, Zero, DAG.getConstant(0x0200, DL, MVT::i32),
2244 Zero, ISD::SETNE), DAG.getConstant(0x7c00, DL, MVT::i32));
2246 // N = M | (E << 12);
2247 SDValue N = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2248 DAG.getNode(ISD::SHL, DL, MVT::i32, E,
2249 DAG.getConstant(12, DL, MVT::i32)));
2251 // B = clamp(1-E, 0, 13);
2252 SDValue OneSubExp = DAG.getNode(ISD::SUB, DL, MVT::i32,
2254 SDValue B = DAG.getNode(ISD::SMAX, DL, MVT::i32, OneSubExp, Zero);
2255 B = DAG.getNode(ISD::SMIN, DL, MVT::i32, B,
2256 DAG.getConstant(13, DL, MVT::i32));
2258 SDValue SigSetHigh = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2259 DAG.getConstant(0x1000, DL, MVT::i32));
2261 SDValue D = DAG.getNode(ISD::SRL, DL, MVT::i32, SigSetHigh, B);
2262 SDValue D0 = DAG.getNode(ISD::SHL, DL, MVT::i32, D, B);
2263 SDValue D1 = DAG.getSelectCC(DL, D0, SigSetHigh, One, Zero, ISD::SETNE);
2264 D = DAG.getNode(ISD::OR, DL, MVT::i32, D, D1);
2266 SDValue V = DAG.getSelectCC(DL, E, One, D, N, ISD::SETLT);
2267 SDValue VLow3 = DAG.getNode(ISD::AND, DL, MVT::i32, V,
2268 DAG.getConstant(0x7, DL, MVT::i32));
2269 V = DAG.getNode(ISD::SRL, DL, MVT::i32, V,
2270 DAG.getConstant(2, DL, MVT::i32));
2271 SDValue V0 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(3, DL, MVT::i32),
2272 One, Zero, ISD::SETEQ);
2273 SDValue V1 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(5, DL, MVT::i32),
2274 One, Zero, ISD::SETGT);
2275 V1 = DAG.getNode(ISD::OR, DL, MVT::i32, V0, V1);
2276 V = DAG.getNode(ISD::ADD, DL, MVT::i32, V, V1);
2278 V = DAG.getSelectCC(DL, E, DAG.getConstant(30, DL, MVT::i32),
2279 DAG.getConstant(0x7c00, DL, MVT::i32), V, ISD::SETGT);
2280 V = DAG.getSelectCC(DL, E, DAG.getConstant(1039, DL, MVT::i32),
2283 // Extract the sign bit.
2284 SDValue Sign = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2285 DAG.getConstant(16, DL, MVT::i32));
2286 Sign = DAG.getNode(ISD::AND, DL, MVT::i32, Sign,
2287 DAG.getConstant(0x8000, DL, MVT::i32));
2289 V = DAG.getNode(ISD::OR, DL, MVT::i32, Sign, V);
2290 return DAG.getZExtOrTrunc(V, DL, Op.getValueType());
2293 SDValue AMDGPUTargetLowering::LowerFP_TO_SINT(SDValue Op,
2294 SelectionDAG &DAG) const {
2295 SDValue Src = Op.getOperand(0);
2297 // TODO: Factor out code common with LowerFP_TO_UINT.
2299 EVT SrcVT = Src.getValueType();
2300 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2303 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2305 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2310 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2311 return LowerFP64_TO_INT(Op, DAG, true);
2316 SDValue AMDGPUTargetLowering::LowerFP_TO_UINT(SDValue Op,
2317 SelectionDAG &DAG) const {
2318 SDValue Src = Op.getOperand(0);
2320 // TODO: Factor out code common with LowerFP_TO_SINT.
2322 EVT SrcVT = Src.getValueType();
2323 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2326 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2328 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2333 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2334 return LowerFP64_TO_INT(Op, DAG, false);
2339 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
2340 SelectionDAG &DAG) const {
2341 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2342 MVT VT = Op.getSimpleValueType();
2343 MVT ScalarVT = VT.getScalarType();
2345 assert(VT.isVector());
2347 SDValue Src = Op.getOperand(0);
2350 // TODO: Don't scalarize on Evergreen?
2351 unsigned NElts = VT.getVectorNumElements();
2352 SmallVector<SDValue, 8> Args;
2353 DAG.ExtractVectorElements(Src, Args, 0, NElts);
2355 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType());
2356 for (unsigned I = 0; I < NElts; ++I)
2357 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp);
2359 return DAG.getBuildVector(VT, DL, Args);
2362 //===----------------------------------------------------------------------===//
2363 // Custom DAG optimizations
2364 //===----------------------------------------------------------------------===//
2366 static bool isU24(SDValue Op, SelectionDAG &DAG) {
2368 EVT VT = Op.getValueType();
2369 DAG.computeKnownBits(Op, Known);
2371 return (VT.getSizeInBits() - Known.countMinLeadingZeros()) <= 24;
2374 static bool isI24(SDValue Op, SelectionDAG &DAG) {
2375 EVT VT = Op.getValueType();
2377 // In order for this to be a signed 24-bit value, bit 23, must
2379 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
2380 // as unsigned 24-bit values.
2381 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24;
2384 static bool simplifyI24(SDNode *Node24, unsigned OpIdx,
2385 TargetLowering::DAGCombinerInfo &DCI) {
2387 SelectionDAG &DAG = DCI.DAG;
2388 SDValue Op = Node24->getOperand(OpIdx);
2389 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2390 EVT VT = Op.getValueType();
2392 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24);
2393 APInt KnownZero, KnownOne;
2394 TargetLowering::TargetLoweringOpt TLO(DAG, true, true);
2395 if (TLI.SimplifyDemandedBits(Node24, OpIdx, Demanded, DCI, TLO))
2401 template <typename IntTy>
2402 static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, uint32_t Offset,
2403 uint32_t Width, const SDLoc &DL) {
2404 if (Width + Offset < 32) {
2405 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width);
2406 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width);
2407 return DAG.getConstant(Result, DL, MVT::i32);
2410 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32);
2413 static bool hasVolatileUser(SDNode *Val) {
2414 for (SDNode *U : Val->uses()) {
2415 if (MemSDNode *M = dyn_cast<MemSDNode>(U)) {
2416 if (M->isVolatile())
2424 bool AMDGPUTargetLowering::shouldCombineMemoryType(EVT VT) const {
2425 // i32 vectors are the canonical memory type.
2426 if (VT.getScalarType() == MVT::i32 || isTypeLegal(VT))
2429 if (!VT.isByteSized())
2432 unsigned Size = VT.getStoreSize();
2434 if ((Size == 1 || Size == 2 || Size == 4) && !VT.isVector())
2437 if (Size == 3 || (Size > 4 && (Size % 4 != 0)))
2443 // Replace load of an illegal type with a store of a bitcast to a friendlier
2445 SDValue AMDGPUTargetLowering::performLoadCombine(SDNode *N,
2446 DAGCombinerInfo &DCI) const {
2447 if (!DCI.isBeforeLegalize())
2450 LoadSDNode *LN = cast<LoadSDNode>(N);
2451 if (LN->isVolatile() || !ISD::isNormalLoad(LN) || hasVolatileUser(LN))
2455 SelectionDAG &DAG = DCI.DAG;
2456 EVT VT = LN->getMemoryVT();
2458 unsigned Size = VT.getStoreSize();
2459 unsigned Align = LN->getAlignment();
2460 if (Align < Size && isTypeLegal(VT)) {
2462 unsigned AS = LN->getAddressSpace();
2464 // Expand unaligned loads earlier than legalization. Due to visitation order
2465 // problems during legalization, the emitted instructions to pack and unpack
2466 // the bytes again are not eliminated in the case of an unaligned copy.
2467 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
2469 return scalarizeVectorLoad(LN, DAG);
2472 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(LN, DAG);
2473 return DAG.getMergeValues(Ops, SDLoc(N));
2480 if (!shouldCombineMemoryType(VT))
2483 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
2486 = DAG.getLoad(NewVT, SL, LN->getChain(),
2487 LN->getBasePtr(), LN->getMemOperand());
2489 SDValue BC = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad);
2490 DCI.CombineTo(N, BC, NewLoad.getValue(1));
2491 return SDValue(N, 0);
2494 // Replace store of an illegal type with a store of a bitcast to a friendlier
2496 SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N,
2497 DAGCombinerInfo &DCI) const {
2498 if (!DCI.isBeforeLegalize())
2501 StoreSDNode *SN = cast<StoreSDNode>(N);
2502 if (SN->isVolatile() || !ISD::isNormalStore(SN))
2505 EVT VT = SN->getMemoryVT();
2506 unsigned Size = VT.getStoreSize();
2509 SelectionDAG &DAG = DCI.DAG;
2510 unsigned Align = SN->getAlignment();
2511 if (Align < Size && isTypeLegal(VT)) {
2513 unsigned AS = SN->getAddressSpace();
2515 // Expand unaligned stores earlier than legalization. Due to visitation
2516 // order problems during legalization, the emitted instructions to pack and
2517 // unpack the bytes again are not eliminated in the case of an unaligned
2519 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
2521 return scalarizeVectorStore(SN, DAG);
2523 return expandUnalignedStore(SN, DAG);
2530 if (!shouldCombineMemoryType(VT))
2533 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
2534 SDValue Val = SN->getValue();
2536 //DCI.AddToWorklist(Val.getNode());
2538 bool OtherUses = !Val.hasOneUse();
2539 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NewVT, Val);
2541 SDValue CastBack = DAG.getNode(ISD::BITCAST, SL, VT, CastVal);
2542 DAG.ReplaceAllUsesOfValueWith(Val, CastBack);
2545 return DAG.getStore(SN->getChain(), SL, CastVal,
2546 SN->getBasePtr(), SN->getMemOperand());
2549 SDValue AMDGPUTargetLowering::performClampCombine(SDNode *N,
2550 DAGCombinerInfo &DCI) const {
2551 ConstantFPSDNode *CSrc = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
2555 const APFloat &F = CSrc->getValueAPF();
2556 APFloat Zero = APFloat::getZero(F.getSemantics());
2557 APFloat::cmpResult Cmp0 = F.compare(Zero);
2558 if (Cmp0 == APFloat::cmpLessThan ||
2559 (Cmp0 == APFloat::cmpUnordered && Subtarget->enableDX10Clamp())) {
2560 return DCI.DAG.getConstantFP(Zero, SDLoc(N), N->getValueType(0));
2563 APFloat One(F.getSemantics(), "1.0");
2564 APFloat::cmpResult Cmp1 = F.compare(One);
2565 if (Cmp1 == APFloat::cmpGreaterThan)
2566 return DCI.DAG.getConstantFP(One, SDLoc(N), N->getValueType(0));
2568 return SDValue(CSrc, 0);
2571 /// Split the 64-bit value \p LHS into two 32-bit components, and perform the
2572 /// binary operation \p Opc to it with the corresponding constant operands.
2573 SDValue AMDGPUTargetLowering::splitBinaryBitConstantOpImpl(
2574 DAGCombinerInfo &DCI, const SDLoc &SL,
2575 unsigned Opc, SDValue LHS,
2576 uint32_t ValLo, uint32_t ValHi) const {
2577 SelectionDAG &DAG = DCI.DAG;
2579 std::tie(Lo, Hi) = split64BitValue(LHS, DAG);
2581 SDValue LoRHS = DAG.getConstant(ValLo, SL, MVT::i32);
2582 SDValue HiRHS = DAG.getConstant(ValHi, SL, MVT::i32);
2584 SDValue LoAnd = DAG.getNode(Opc, SL, MVT::i32, Lo, LoRHS);
2585 SDValue HiAnd = DAG.getNode(Opc, SL, MVT::i32, Hi, HiRHS);
2587 // Re-visit the ands. It's possible we eliminated one of them and it could
2588 // simplify the vector.
2589 DCI.AddToWorklist(Lo.getNode());
2590 DCI.AddToWorklist(Hi.getNode());
2592 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd});
2593 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2596 SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N,
2597 DAGCombinerInfo &DCI) const {
2598 EVT VT = N->getValueType(0);
2602 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2606 SDValue LHS = N->getOperand(0);
2607 unsigned RHSVal = RHS->getZExtValue();
2612 SelectionDAG &DAG = DCI.DAG;
2614 switch (LHS->getOpcode()) {
2617 case ISD::ZERO_EXTEND:
2618 case ISD::SIGN_EXTEND:
2619 case ISD::ANY_EXTEND: {
2620 // shl (ext x) => zext (shl x), if shift does not overflow int
2622 SDValue X = LHS->getOperand(0);
2623 DAG.computeKnownBits(X, Known);
2624 unsigned LZ = Known.countMinLeadingZeros();
2627 EVT XVT = X.getValueType();
2628 SDValue Shl = DAG.getNode(ISD::SHL, SL, XVT, X, SDValue(RHS, 0));
2629 return DAG.getZExtOrTrunc(Shl, SL, VT);
2633 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32))
2635 // On some subtargets, 64-bit shift is a quarter rate instruction. In the
2636 // common case, splitting this into a move and a 32-bit shift is faster and
2637 // the same code size.
2641 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32);
2643 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS);
2644 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt);
2646 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
2648 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift});
2649 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2652 SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N,
2653 DAGCombinerInfo &DCI) const {
2654 if (N->getValueType(0) != MVT::i64)
2657 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2661 SelectionDAG &DAG = DCI.DAG;
2663 unsigned RHSVal = RHS->getZExtValue();
2665 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31)
2667 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2668 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2669 DAG.getConstant(31, SL, MVT::i32));
2671 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift});
2672 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2675 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31)
2677 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2678 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2679 DAG.getConstant(31, SL, MVT::i32));
2680 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift});
2681 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2687 SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N,
2688 DAGCombinerInfo &DCI) const {
2689 if (N->getValueType(0) != MVT::i64)
2692 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2696 unsigned ShiftAmt = RHS->getZExtValue();
2700 // srl i64:x, C for C >= 32
2702 // build_pair (srl hi_32(x), C - 32), 0
2704 SelectionDAG &DAG = DCI.DAG;
2707 SDValue One = DAG.getConstant(1, SL, MVT::i32);
2708 SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
2710 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, N->getOperand(0));
2711 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32,
2714 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32);
2715 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst);
2717 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero});
2719 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair);
2722 // We need to specifically handle i64 mul here to avoid unnecessary conversion
2723 // instructions. If we only match on the legalized i64 mul expansion,
2724 // SimplifyDemandedBits will be unable to remove them because there will be
2725 // multiple uses due to the separate mul + mulh[su].
2726 static SDValue getMul24(SelectionDAG &DAG, const SDLoc &SL,
2727 SDValue N0, SDValue N1, unsigned Size, bool Signed) {
2729 unsigned MulOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2730 return DAG.getNode(MulOpc, SL, MVT::i32, N0, N1);
2733 // Because we want to eliminate extension instructions before the
2734 // operation, we need to create a single user here (i.e. not the separate
2735 // mul_lo + mul_hi) so that SimplifyDemandedBits will deal with it.
2737 unsigned MulOpc = Signed ? AMDGPUISD::MUL_LOHI_I24 : AMDGPUISD::MUL_LOHI_U24;
2739 SDValue Mul = DAG.getNode(MulOpc, SL,
2740 DAG.getVTList(MVT::i32, MVT::i32), N0, N1);
2742 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64,
2743 Mul.getValue(0), Mul.getValue(1));
2746 SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N,
2747 DAGCombinerInfo &DCI) const {
2748 EVT VT = N->getValueType(0);
2750 unsigned Size = VT.getSizeInBits();
2751 if (VT.isVector() || Size > 64)
2754 // There are i16 integer mul/mad.
2755 if (Subtarget->has16BitInsts() && VT.getScalarType().bitsLE(MVT::i16))
2758 SelectionDAG &DAG = DCI.DAG;
2761 SDValue N0 = N->getOperand(0);
2762 SDValue N1 = N->getOperand(1);
2765 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) {
2766 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2767 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
2768 Mul = getMul24(DAG, DL, N0, N1, Size, false);
2769 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) {
2770 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2771 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
2772 Mul = getMul24(DAG, DL, N0, N1, Size, true);
2777 // We need to use sext even for MUL_U24, because MUL_U24 is used
2778 // for signed multiply of 8 and 16-bit types.
2779 return DAG.getSExtOrTrunc(Mul, DL, VT);
2782 SDValue AMDGPUTargetLowering::performMulhsCombine(SDNode *N,
2783 DAGCombinerInfo &DCI) const {
2784 EVT VT = N->getValueType(0);
2786 if (!Subtarget->hasMulI24() || VT.isVector())
2789 SelectionDAG &DAG = DCI.DAG;
2792 SDValue N0 = N->getOperand(0);
2793 SDValue N1 = N->getOperand(1);
2795 if (!isI24(N0, DAG) || !isI24(N1, DAG))
2798 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2799 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
2801 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_I24, DL, MVT::i32, N0, N1);
2802 DCI.AddToWorklist(Mulhi.getNode());
2803 return DAG.getSExtOrTrunc(Mulhi, DL, VT);
2806 SDValue AMDGPUTargetLowering::performMulhuCombine(SDNode *N,
2807 DAGCombinerInfo &DCI) const {
2808 EVT VT = N->getValueType(0);
2810 if (!Subtarget->hasMulU24() || VT.isVector() || VT.getSizeInBits() > 32)
2813 SelectionDAG &DAG = DCI.DAG;
2816 SDValue N0 = N->getOperand(0);
2817 SDValue N1 = N->getOperand(1);
2819 if (!isU24(N0, DAG) || !isU24(N1, DAG))
2822 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2823 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
2825 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_U24, DL, MVT::i32, N0, N1);
2826 DCI.AddToWorklist(Mulhi.getNode());
2827 return DAG.getZExtOrTrunc(Mulhi, DL, VT);
2830 SDValue AMDGPUTargetLowering::performMulLoHi24Combine(
2831 SDNode *N, DAGCombinerInfo &DCI) const {
2832 SelectionDAG &DAG = DCI.DAG;
2834 // Simplify demanded bits before splitting into multiple users.
2835 if (simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI))
2838 SDValue N0 = N->getOperand(0);
2839 SDValue N1 = N->getOperand(1);
2841 bool Signed = (N->getOpcode() == AMDGPUISD::MUL_LOHI_I24);
2843 unsigned MulLoOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2844 unsigned MulHiOpc = Signed ? AMDGPUISD::MULHI_I24 : AMDGPUISD::MULHI_U24;
2848 SDValue MulLo = DAG.getNode(MulLoOpc, SL, MVT::i32, N0, N1);
2849 SDValue MulHi = DAG.getNode(MulHiOpc, SL, MVT::i32, N0, N1);
2850 return DAG.getMergeValues({ MulLo, MulHi }, SL);
2853 static bool isNegativeOne(SDValue Val) {
2854 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val))
2855 return C->isAllOnesValue();
2859 static bool isCtlzOpc(unsigned Opc) {
2860 return Opc == ISD::CTLZ || Opc == ISD::CTLZ_ZERO_UNDEF;
2863 SDValue AMDGPUTargetLowering::getFFBH_U32(SelectionDAG &DAG,
2865 const SDLoc &DL) const {
2866 EVT VT = Op.getValueType();
2867 EVT LegalVT = getTypeToTransformTo(*DAG.getContext(), VT);
2868 if (LegalVT != MVT::i32 && (Subtarget->has16BitInsts() &&
2869 LegalVT != MVT::i16))
2873 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Op);
2875 SDValue FFBH = DAG.getNode(AMDGPUISD::FFBH_U32, DL, MVT::i32, Op);
2877 FFBH = DAG.getNode(ISD::TRUNCATE, DL, VT, FFBH);
2882 // The native instructions return -1 on 0 input. Optimize out a select that
2883 // produces -1 on 0.
2885 // TODO: If zero is not undef, we could also do this if the output is compared
2886 // against the bitwidth.
2888 // TODO: Should probably combine against FFBH_U32 instead of ctlz directly.
2889 SDValue AMDGPUTargetLowering::performCtlzCombine(const SDLoc &SL, SDValue Cond,
2890 SDValue LHS, SDValue RHS,
2891 DAGCombinerInfo &DCI) const {
2892 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2893 if (!CmpRhs || !CmpRhs->isNullValue())
2896 SelectionDAG &DAG = DCI.DAG;
2897 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
2898 SDValue CmpLHS = Cond.getOperand(0);
2900 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x
2901 if (CCOpcode == ISD::SETEQ &&
2902 isCtlzOpc(RHS.getOpcode()) &&
2903 RHS.getOperand(0) == CmpLHS &&
2904 isNegativeOne(LHS)) {
2905 return getFFBH_U32(DAG, CmpLHS, SL);
2908 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x
2909 if (CCOpcode == ISD::SETNE &&
2910 isCtlzOpc(LHS.getOpcode()) &&
2911 LHS.getOperand(0) == CmpLHS &&
2912 isNegativeOne(RHS)) {
2913 return getFFBH_U32(DAG, CmpLHS, SL);
2919 static SDValue distributeOpThroughSelect(TargetLowering::DAGCombinerInfo &DCI,
2925 SelectionDAG &DAG = DCI.DAG;
2926 EVT VT = N1.getValueType();
2928 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, Cond,
2929 N1.getOperand(0), N2.getOperand(0));
2930 DCI.AddToWorklist(NewSelect.getNode());
2931 return DAG.getNode(Op, SL, VT, NewSelect);
2934 // Pull a free FP operation out of a select so it may fold into uses.
2936 // select c, (fneg x), (fneg y) -> fneg (select c, x, y)
2937 // select c, (fneg x), k -> fneg (select c, x, (fneg k))
2939 // select c, (fabs x), (fabs y) -> fabs (select c, x, y)
2940 // select c, (fabs x), +k -> fabs (select c, x, k)
2941 static SDValue foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI,
2943 SelectionDAG &DAG = DCI.DAG;
2944 SDValue Cond = N.getOperand(0);
2945 SDValue LHS = N.getOperand(1);
2946 SDValue RHS = N.getOperand(2);
2948 EVT VT = N.getValueType();
2949 if ((LHS.getOpcode() == ISD::FABS && RHS.getOpcode() == ISD::FABS) ||
2950 (LHS.getOpcode() == ISD::FNEG && RHS.getOpcode() == ISD::FNEG)) {
2951 return distributeOpThroughSelect(DCI, LHS.getOpcode(),
2952 SDLoc(N), Cond, LHS, RHS);
2956 if (RHS.getOpcode() == ISD::FABS || RHS.getOpcode() == ISD::FNEG) {
2957 std::swap(LHS, RHS);
2961 // TODO: Support vector constants.
2962 ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
2963 if ((LHS.getOpcode() == ISD::FNEG || LHS.getOpcode() == ISD::FABS) && CRHS) {
2965 // If one side is an fneg/fabs and the other is a constant, we can push the
2966 // fneg/fabs down. If it's an fabs, the constant needs to be non-negative.
2967 SDValue NewLHS = LHS.getOperand(0);
2968 SDValue NewRHS = RHS;
2970 // Careful: if the neg can be folded up, don't try to pull it back down.
2971 bool ShouldFoldNeg = true;
2973 if (NewLHS.hasOneUse()) {
2974 unsigned Opc = NewLHS.getOpcode();
2975 if (LHS.getOpcode() == ISD::FNEG && fnegFoldsIntoOp(Opc))
2976 ShouldFoldNeg = false;
2977 if (LHS.getOpcode() == ISD::FABS && Opc == ISD::FMUL)
2978 ShouldFoldNeg = false;
2981 if (ShouldFoldNeg) {
2982 if (LHS.getOpcode() == ISD::FNEG)
2983 NewRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
2984 else if (CRHS->isNegative())
2988 std::swap(NewLHS, NewRHS);
2990 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT,
2991 Cond, NewLHS, NewRHS);
2992 DCI.AddToWorklist(NewSelect.getNode());
2993 return DAG.getNode(LHS.getOpcode(), SL, VT, NewSelect);
3001 SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N,
3002 DAGCombinerInfo &DCI) const {
3003 if (SDValue Folded = foldFreeOpFromSelect(DCI, SDValue(N, 0)))
3006 SDValue Cond = N->getOperand(0);
3007 if (Cond.getOpcode() != ISD::SETCC)
3010 EVT VT = N->getValueType(0);
3011 SDValue LHS = Cond.getOperand(0);
3012 SDValue RHS = Cond.getOperand(1);
3013 SDValue CC = Cond.getOperand(2);
3015 SDValue True = N->getOperand(1);
3016 SDValue False = N->getOperand(2);
3018 if (Cond.hasOneUse()) { // TODO: Look for multiple select uses.
3019 SelectionDAG &DAG = DCI.DAG;
3020 if ((DAG.isConstantValueOfAnyType(True) ||
3021 DAG.isConstantValueOfAnyType(True)) &&
3022 (!DAG.isConstantValueOfAnyType(False) &&
3023 !DAG.isConstantValueOfAnyType(False))) {
3024 // Swap cmp + select pair to move constant to false input.
3025 // This will allow using VOPC cndmasks more often.
3026 // select (setcc x, y), k, x -> select (setcc y, x) x, x
3029 ISD::CondCode NewCC = getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3030 LHS.getValueType().isInteger());
3032 SDValue NewCond = DAG.getSetCC(SL, Cond.getValueType(), LHS, RHS, NewCC);
3033 return DAG.getNode(ISD::SELECT, SL, VT, NewCond, False, True);
3036 if (VT == MVT::f32 && Subtarget->hasFminFmaxLegacy()) {
3038 = combineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI);
3039 // Revisit this node so we can catch min3/max3/med3 patterns.
3040 //DCI.AddToWorklist(MinMax.getNode());
3045 // There's no reason to not do this if the condition has other uses.
3046 return performCtlzCombine(SDLoc(N), Cond, True, False, DCI);
3049 static bool isConstantFPZero(SDValue N) {
3050 if (const ConstantFPSDNode *C = isConstOrConstSplatFP(N))
3051 return C->isZero() && !C->isNegative();
3055 static unsigned inverseMinMax(unsigned Opc) {
3058 return ISD::FMINNUM;
3060 return ISD::FMAXNUM;
3061 case AMDGPUISD::FMAX_LEGACY:
3062 return AMDGPUISD::FMIN_LEGACY;
3063 case AMDGPUISD::FMIN_LEGACY:
3064 return AMDGPUISD::FMAX_LEGACY;
3066 llvm_unreachable("invalid min/max opcode");
3070 SDValue AMDGPUTargetLowering::performFNegCombine(SDNode *N,
3071 DAGCombinerInfo &DCI) const {
3072 SelectionDAG &DAG = DCI.DAG;
3073 SDValue N0 = N->getOperand(0);
3074 EVT VT = N->getValueType(0);
3076 unsigned Opc = N0.getOpcode();
3078 // If the input has multiple uses and we can either fold the negate down, or
3079 // the other uses cannot, give up. This both prevents unprofitable
3080 // transformations and infinite loops: we won't repeatedly try to fold around
3081 // a negate that has no 'good' form.
3082 if (N0.hasOneUse()) {
3083 // This may be able to fold into the source, but at a code size cost. Don't
3084 // fold if the fold into the user is free.
3085 if (allUsesHaveSourceMods(N, 0))
3088 if (fnegFoldsIntoOp(Opc) &&
3089 (allUsesHaveSourceMods(N) || !allUsesHaveSourceMods(N0.getNode())))
3096 if (!mayIgnoreSignedZero(N0))
3099 // (fneg (fadd x, y)) -> (fadd (fneg x), (fneg y))
3100 SDValue LHS = N0.getOperand(0);
3101 SDValue RHS = N0.getOperand(1);
3103 if (LHS.getOpcode() != ISD::FNEG)
3104 LHS = DAG.getNode(ISD::FNEG, SL, VT, LHS);
3106 LHS = LHS.getOperand(0);
3108 if (RHS.getOpcode() != ISD::FNEG)
3109 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3111 RHS = RHS.getOperand(0);
3113 SDValue Res = DAG.getNode(ISD::FADD, SL, VT, LHS, RHS, N0->getFlags());
3114 if (!N0.hasOneUse())
3115 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3119 case AMDGPUISD::FMUL_LEGACY: {
3120 // (fneg (fmul x, y)) -> (fmul x, (fneg y))
3121 // (fneg (fmul_legacy x, y)) -> (fmul_legacy x, (fneg y))
3122 SDValue LHS = N0.getOperand(0);
3123 SDValue RHS = N0.getOperand(1);
3125 if (LHS.getOpcode() == ISD::FNEG)
3126 LHS = LHS.getOperand(0);
3127 else if (RHS.getOpcode() == ISD::FNEG)
3128 RHS = RHS.getOperand(0);
3130 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3132 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, RHS, N0->getFlags());
3133 if (!N0.hasOneUse())
3134 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3139 if (!mayIgnoreSignedZero(N0))
3142 // (fneg (fma x, y, z)) -> (fma x, (fneg y), (fneg z))
3143 SDValue LHS = N0.getOperand(0);
3144 SDValue MHS = N0.getOperand(1);
3145 SDValue RHS = N0.getOperand(2);
3147 if (LHS.getOpcode() == ISD::FNEG)
3148 LHS = LHS.getOperand(0);
3149 else if (MHS.getOpcode() == ISD::FNEG)
3150 MHS = MHS.getOperand(0);
3152 MHS = DAG.getNode(ISD::FNEG, SL, VT, MHS);
3154 if (RHS.getOpcode() != ISD::FNEG)
3155 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3157 RHS = RHS.getOperand(0);
3159 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, MHS, RHS);
3160 if (!N0.hasOneUse())
3161 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3166 case AMDGPUISD::FMAX_LEGACY:
3167 case AMDGPUISD::FMIN_LEGACY: {
3168 // fneg (fmaxnum x, y) -> fminnum (fneg x), (fneg y)
3169 // fneg (fminnum x, y) -> fmaxnum (fneg x), (fneg y)
3170 // fneg (fmax_legacy x, y) -> fmin_legacy (fneg x), (fneg y)
3171 // fneg (fmin_legacy x, y) -> fmax_legacy (fneg x), (fneg y)
3173 SDValue LHS = N0.getOperand(0);
3174 SDValue RHS = N0.getOperand(1);
3176 // 0 doesn't have a negated inline immediate.
3177 // TODO: Shouldn't fold 1/2pi either, and should be generalized to other
3179 if (isConstantFPZero(RHS))
3182 SDValue NegLHS = DAG.getNode(ISD::FNEG, SL, VT, LHS);
3183 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3184 unsigned Opposite = inverseMinMax(Opc);
3186 SDValue Res = DAG.getNode(Opposite, SL, VT, NegLHS, NegRHS, N0->getFlags());
3187 if (!N0.hasOneUse())
3188 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3191 case ISD::FP_EXTEND:
3194 case ISD::FNEARBYINT: // XXX - Should fround be handled?
3196 case AMDGPUISD::RCP:
3197 case AMDGPUISD::RCP_LEGACY:
3198 case AMDGPUISD::SIN_HW: {
3199 SDValue CvtSrc = N0.getOperand(0);
3200 if (CvtSrc.getOpcode() == ISD::FNEG) {
3201 // (fneg (fp_extend (fneg x))) -> (fp_extend x)
3202 // (fneg (rcp (fneg x))) -> (rcp x)
3203 return DAG.getNode(Opc, SL, VT, CvtSrc.getOperand(0));
3206 if (!N0.hasOneUse())
3209 // (fneg (fp_extend x)) -> (fp_extend (fneg x))
3210 // (fneg (rcp x)) -> (rcp (fneg x))
3211 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
3212 return DAG.getNode(Opc, SL, VT, Neg, N0->getFlags());
3214 case ISD::FP_ROUND: {
3215 SDValue CvtSrc = N0.getOperand(0);
3217 if (CvtSrc.getOpcode() == ISD::FNEG) {
3218 // (fneg (fp_round (fneg x))) -> (fp_round x)
3219 return DAG.getNode(ISD::FP_ROUND, SL, VT,
3220 CvtSrc.getOperand(0), N0.getOperand(1));
3223 if (!N0.hasOneUse())
3226 // (fneg (fp_round x)) -> (fp_round (fneg x))
3227 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
3228 return DAG.getNode(ISD::FP_ROUND, SL, VT, Neg, N0.getOperand(1));
3230 case ISD::FP16_TO_FP: {
3231 // v_cvt_f32_f16 supports source modifiers on pre-VI targets without legal
3232 // f16, but legalization of f16 fneg ends up pulling it out of the source.
3233 // Put the fneg back as a legal source operation that can be matched later.
3236 SDValue Src = N0.getOperand(0);
3237 EVT SrcVT = Src.getValueType();
3239 // fneg (fp16_to_fp x) -> fp16_to_fp (xor x, 0x8000)
3240 SDValue IntFNeg = DAG.getNode(ISD::XOR, SL, SrcVT, Src,
3241 DAG.getConstant(0x8000, SL, SrcVT));
3242 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFNeg);
3249 SDValue AMDGPUTargetLowering::performFAbsCombine(SDNode *N,
3250 DAGCombinerInfo &DCI) const {
3251 SelectionDAG &DAG = DCI.DAG;
3252 SDValue N0 = N->getOperand(0);
3254 if (!N0.hasOneUse())
3257 switch (N0.getOpcode()) {
3258 case ISD::FP16_TO_FP: {
3259 assert(!Subtarget->has16BitInsts() && "should only see if f16 is illegal");
3261 SDValue Src = N0.getOperand(0);
3262 EVT SrcVT = Src.getValueType();
3264 // fabs (fp16_to_fp x) -> fp16_to_fp (and x, 0x7fff)
3265 SDValue IntFAbs = DAG.getNode(ISD::AND, SL, SrcVT, Src,
3266 DAG.getConstant(0x7fff, SL, SrcVT));
3267 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFAbs);
3274 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N,
3275 DAGCombinerInfo &DCI) const {
3276 SelectionDAG &DAG = DCI.DAG;
3279 switch(N->getOpcode()) {
3282 case ISD::BITCAST: {
3283 EVT DestVT = N->getValueType(0);
3285 // Push casts through vector builds. This helps avoid emitting a large
3286 // number of copies when materializing floating point vector constants.
3288 // vNt1 bitcast (vNt0 (build_vector t0:x, t0:y)) =>
3289 // vnt1 = build_vector (t1 (bitcast t0:x)), (t1 (bitcast t0:y))
3290 if (DestVT.isVector()) {
3291 SDValue Src = N->getOperand(0);
3292 if (Src.getOpcode() == ISD::BUILD_VECTOR) {
3293 EVT SrcVT = Src.getValueType();
3294 unsigned NElts = DestVT.getVectorNumElements();
3296 if (SrcVT.getVectorNumElements() == NElts) {
3297 EVT DestEltVT = DestVT.getVectorElementType();
3299 SmallVector<SDValue, 8> CastedElts;
3301 for (unsigned I = 0, E = SrcVT.getVectorNumElements(); I != E; ++I) {
3302 SDValue Elt = Src.getOperand(I);
3303 CastedElts.push_back(DAG.getNode(ISD::BITCAST, DL, DestEltVT, Elt));
3306 return DAG.getBuildVector(DestVT, SL, CastedElts);
3311 if (DestVT.getSizeInBits() != 64 && !DestVT.isVector())
3314 // Fold bitcasts of constants.
3316 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k)
3317 // TODO: Generalize and move to DAGCombiner
3318 SDValue Src = N->getOperand(0);
3319 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) {
3320 assert(Src.getValueType() == MVT::i64);
3322 uint64_t CVal = C->getZExtValue();
3323 return DAG.getNode(ISD::BUILD_VECTOR, SL, DestVT,
3324 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3325 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3328 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) {
3329 const APInt &Val = C->getValueAPF().bitcastToAPInt();
3331 uint64_t CVal = Val.getZExtValue();
3332 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3333 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3334 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3336 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec);
3342 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3345 return performShlCombine(N, DCI);
3348 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3351 return performSrlCombine(N, DCI);
3354 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3357 return performSraCombine(N, DCI);
3360 return performMulCombine(N, DCI);
3362 return performMulhsCombine(N, DCI);
3364 return performMulhuCombine(N, DCI);
3365 case AMDGPUISD::MUL_I24:
3366 case AMDGPUISD::MUL_U24:
3367 case AMDGPUISD::MULHI_I24:
3368 case AMDGPUISD::MULHI_U24: {
3369 // If the first call to simplify is successfull, then N may end up being
3370 // deleted, so we shouldn't call simplifyI24 again.
3371 simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI);
3374 case AMDGPUISD::MUL_LOHI_I24:
3375 case AMDGPUISD::MUL_LOHI_U24:
3376 return performMulLoHi24Combine(N, DCI);
3378 return performSelectCombine(N, DCI);
3380 return performFNegCombine(N, DCI);
3382 return performFAbsCombine(N, DCI);
3383 case AMDGPUISD::BFE_I32:
3384 case AMDGPUISD::BFE_U32: {
3385 assert(!N->getValueType(0).isVector() &&
3386 "Vector handling of BFE not implemented");
3387 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
3391 uint32_t WidthVal = Width->getZExtValue() & 0x1f;
3393 return DAG.getConstant(0, DL, MVT::i32);
3395 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
3399 SDValue BitsFrom = N->getOperand(0);
3400 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f;
3402 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32;
3404 if (OffsetVal == 0) {
3405 // This is already sign / zero extended, so try to fold away extra BFEs.
3406 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal);
3408 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom);
3409 if (OpSignBits >= SignBits)
3412 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal);
3414 // This is a sign_extend_inreg. Replace it to take advantage of existing
3415 // DAG Combines. If not eliminated, we will match back to BFE during
3418 // TODO: The sext_inreg of extended types ends, although we can could
3419 // handle them in a single BFE.
3420 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom,
3421 DAG.getValueType(SmallVT));
3424 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT);
3427 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) {
3429 return constantFoldBFE<int32_t>(DAG,
3430 CVal->getSExtValue(),
3436 return constantFoldBFE<uint32_t>(DAG,
3437 CVal->getZExtValue(),
3443 if ((OffsetVal + WidthVal) >= 32) {
3444 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32);
3445 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32,
3446 BitsFrom, ShiftVal);
3449 if (BitsFrom.hasOneUse()) {
3450 APInt Demanded = APInt::getBitsSet(32,
3452 OffsetVal + WidthVal);
3455 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3456 !DCI.isBeforeLegalizeOps());
3457 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3458 if (TLI.ShrinkDemandedConstant(BitsFrom, Demanded, TLO) ||
3459 TLI.SimplifyDemandedBits(BitsFrom, Demanded, Known, TLO)) {
3460 DCI.CommitTargetLoweringOpt(TLO);
3467 return performLoadCombine(N, DCI);
3469 return performStoreCombine(N, DCI);
3470 case AMDGPUISD::CLAMP:
3471 return performClampCombine(N, DCI);
3472 case AMDGPUISD::RCP: {
3473 if (const auto *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
3474 // XXX - Should this flush denormals?
3475 const APFloat &Val = CFP->getValueAPF();
3476 APFloat One(Val.getSemantics(), "1.0");
3477 return DAG.getConstantFP(One / Val, SDLoc(N), N->getValueType(0));
3486 //===----------------------------------------------------------------------===//
3488 //===----------------------------------------------------------------------===//
3490 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3491 const TargetRegisterClass *RC,
3492 unsigned Reg, EVT VT) const {
3493 MachineFunction &MF = DAG.getMachineFunction();
3494 MachineRegisterInfo &MRI = MF.getRegInfo();
3495 unsigned VirtualRegister;
3496 if (!MRI.isLiveIn(Reg)) {
3497 VirtualRegister = MRI.createVirtualRegister(RC);
3498 MRI.addLiveIn(Reg, VirtualRegister);
3500 VirtualRegister = MRI.getLiveInVirtReg(Reg);
3502 return DAG.getRegister(VirtualRegister, VT);
3505 uint32_t AMDGPUTargetLowering::getImplicitParameterOffset(
3506 const AMDGPUMachineFunction *MFI, const ImplicitParameter Param) const {
3507 unsigned Alignment = Subtarget->getAlignmentForImplicitArgPtr();
3508 uint64_t ArgOffset = alignTo(MFI->getABIArgOffset(), Alignment);
3513 return ArgOffset + 4;
3515 llvm_unreachable("unexpected implicit parameter type");
3518 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
3520 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
3521 switch ((AMDGPUISD::NodeType)Opcode) {
3522 case AMDGPUISD::FIRST_NUMBER: break;
3524 NODE_NAME_CASE(UMUL);
3525 NODE_NAME_CASE(BRANCH_COND);
3529 NODE_NAME_CASE(ELSE)
3530 NODE_NAME_CASE(LOOP)
3531 NODE_NAME_CASE(CALL)
3532 NODE_NAME_CASE(TRAP)
3533 NODE_NAME_CASE(RET_FLAG)
3534 NODE_NAME_CASE(RETURN_TO_EPILOG)
3535 NODE_NAME_CASE(ENDPGM)
3536 NODE_NAME_CASE(DWORDADDR)
3537 NODE_NAME_CASE(FRACT)
3538 NODE_NAME_CASE(SETCC)
3539 NODE_NAME_CASE(SETREG)
3540 NODE_NAME_CASE(FMA_W_CHAIN)
3541 NODE_NAME_CASE(FMUL_W_CHAIN)
3542 NODE_NAME_CASE(CLAMP)
3543 NODE_NAME_CASE(COS_HW)
3544 NODE_NAME_CASE(SIN_HW)
3545 NODE_NAME_CASE(FMAX_LEGACY)
3546 NODE_NAME_CASE(FMIN_LEGACY)
3547 NODE_NAME_CASE(FMAX3)
3548 NODE_NAME_CASE(SMAX3)
3549 NODE_NAME_CASE(UMAX3)
3550 NODE_NAME_CASE(FMIN3)
3551 NODE_NAME_CASE(SMIN3)
3552 NODE_NAME_CASE(UMIN3)
3553 NODE_NAME_CASE(FMED3)
3554 NODE_NAME_CASE(SMED3)
3555 NODE_NAME_CASE(UMED3)
3556 NODE_NAME_CASE(URECIP)
3557 NODE_NAME_CASE(DIV_SCALE)
3558 NODE_NAME_CASE(DIV_FMAS)
3559 NODE_NAME_CASE(DIV_FIXUP)
3560 NODE_NAME_CASE(FMAD_FTZ)
3561 NODE_NAME_CASE(TRIG_PREOP)
3564 NODE_NAME_CASE(RCP_LEGACY)
3565 NODE_NAME_CASE(RSQ_LEGACY)
3566 NODE_NAME_CASE(FMUL_LEGACY)
3567 NODE_NAME_CASE(RSQ_CLAMP)
3568 NODE_NAME_CASE(LDEXP)
3569 NODE_NAME_CASE(FP_CLASS)
3570 NODE_NAME_CASE(DOT4)
3571 NODE_NAME_CASE(CARRY)
3572 NODE_NAME_CASE(BORROW)
3573 NODE_NAME_CASE(BFE_U32)
3574 NODE_NAME_CASE(BFE_I32)
3577 NODE_NAME_CASE(FFBH_U32)
3578 NODE_NAME_CASE(FFBH_I32)
3579 NODE_NAME_CASE(MUL_U24)
3580 NODE_NAME_CASE(MUL_I24)
3581 NODE_NAME_CASE(MULHI_U24)
3582 NODE_NAME_CASE(MULHI_I24)
3583 NODE_NAME_CASE(MUL_LOHI_U24)
3584 NODE_NAME_CASE(MUL_LOHI_I24)
3585 NODE_NAME_CASE(MAD_U24)
3586 NODE_NAME_CASE(MAD_I24)
3587 NODE_NAME_CASE(TEXTURE_FETCH)
3588 NODE_NAME_CASE(EXPORT)
3589 NODE_NAME_CASE(EXPORT_DONE)
3590 NODE_NAME_CASE(R600_EXPORT)
3591 NODE_NAME_CASE(CONST_ADDRESS)
3592 NODE_NAME_CASE(REGISTER_LOAD)
3593 NODE_NAME_CASE(REGISTER_STORE)
3594 NODE_NAME_CASE(SAMPLE)
3595 NODE_NAME_CASE(SAMPLEB)
3596 NODE_NAME_CASE(SAMPLED)
3597 NODE_NAME_CASE(SAMPLEL)
3598 NODE_NAME_CASE(CVT_F32_UBYTE0)
3599 NODE_NAME_CASE(CVT_F32_UBYTE1)
3600 NODE_NAME_CASE(CVT_F32_UBYTE2)
3601 NODE_NAME_CASE(CVT_F32_UBYTE3)
3602 NODE_NAME_CASE(CVT_PKRTZ_F16_F32)
3603 NODE_NAME_CASE(FP_TO_FP16)
3604 NODE_NAME_CASE(FP16_ZEXT)
3605 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR)
3606 NODE_NAME_CASE(CONST_DATA_PTR)
3607 NODE_NAME_CASE(PC_ADD_REL_OFFSET)
3608 NODE_NAME_CASE(KILL)
3609 NODE_NAME_CASE(DUMMY_CHAIN)
3610 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break;
3611 NODE_NAME_CASE(INIT_EXEC)
3612 NODE_NAME_CASE(INIT_EXEC_FROM_INPUT)
3613 NODE_NAME_CASE(SENDMSG)
3614 NODE_NAME_CASE(SENDMSGHALT)
3615 NODE_NAME_CASE(INTERP_MOV)
3616 NODE_NAME_CASE(INTERP_P1)
3617 NODE_NAME_CASE(INTERP_P2)
3618 NODE_NAME_CASE(STORE_MSKOR)
3619 NODE_NAME_CASE(LOAD_CONSTANT)
3620 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
3621 NODE_NAME_CASE(ATOMIC_CMP_SWAP)
3622 NODE_NAME_CASE(ATOMIC_INC)
3623 NODE_NAME_CASE(ATOMIC_DEC)
3624 NODE_NAME_CASE(BUFFER_LOAD)
3625 NODE_NAME_CASE(BUFFER_LOAD_FORMAT)
3626 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break;
3631 SDValue AMDGPUTargetLowering::getSqrtEstimate(SDValue Operand,
3632 SelectionDAG &DAG, int Enabled,
3633 int &RefinementSteps,
3634 bool &UseOneConstNR,
3635 bool Reciprocal) const {
3636 EVT VT = Operand.getValueType();
3638 if (VT == MVT::f32) {
3639 RefinementSteps = 0;
3640 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand);
3643 // TODO: There is also f64 rsq instruction, but the documentation is less
3644 // clear on its precision.
3649 SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand,
3650 SelectionDAG &DAG, int Enabled,
3651 int &RefinementSteps) const {
3652 EVT VT = Operand.getValueType();
3654 if (VT == MVT::f32) {
3655 // Reciprocal, < 1 ulp error.
3657 // This reciprocal approximation converges to < 0.5 ulp error with one
3658 // newton rhapson performed with two fused multiple adds (FMAs).
3660 RefinementSteps = 0;
3661 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand);
3664 // TODO: There is also f64 rcp instruction, but the documentation is less
3665 // clear on its precision.
3670 void AMDGPUTargetLowering::computeKnownBitsForTargetNode(
3671 const SDValue Op, KnownBits &Known,
3672 const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth) const {
3674 Known.resetAll(); // Don't know anything.
3677 unsigned Opc = Op.getOpcode();
3682 case AMDGPUISD::CARRY:
3683 case AMDGPUISD::BORROW: {
3684 Known.Zero = APInt::getHighBitsSet(32, 31);
3688 case AMDGPUISD::BFE_I32:
3689 case AMDGPUISD::BFE_U32: {
3690 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3694 uint32_t Width = CWidth->getZExtValue() & 0x1f;
3696 if (Opc == AMDGPUISD::BFE_U32)
3697 Known.Zero = APInt::getHighBitsSet(32, 32 - Width);
3701 case AMDGPUISD::FP_TO_FP16:
3702 case AMDGPUISD::FP16_ZEXT: {
3703 unsigned BitWidth = Known.getBitWidth();
3705 // High bits are zero.
3706 Known.Zero = APInt::getHighBitsSet(BitWidth, BitWidth - 16);
3712 unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode(
3713 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG,
3714 unsigned Depth) const {
3715 switch (Op.getOpcode()) {
3716 case AMDGPUISD::BFE_I32: {
3717 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3721 unsigned SignBits = 32 - Width->getZExtValue() + 1;
3722 if (!isNullConstant(Op.getOperand(1)))
3725 // TODO: Could probably figure something out with non-0 offsets.
3726 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1);
3727 return std::max(SignBits, Op0SignBits);
3730 case AMDGPUISD::BFE_U32: {
3731 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3732 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1;
3735 case AMDGPUISD::CARRY:
3736 case AMDGPUISD::BORROW:
3738 case AMDGPUISD::FP_TO_FP16:
3739 case AMDGPUISD::FP16_ZEXT: