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 "SIInstrInfo.h"
25 #include "SIMachineFunctionInfo.h"
26 #include "llvm/CodeGen/CallingConvLower.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/CodeGen/SelectionDAG.h"
30 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
31 #include "llvm/IR/DataLayout.h"
32 #include "llvm/IR/DiagnosticInfo.h"
33 #include "llvm/Support/KnownBits.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 bool AMDGPUTargetLowering::isOrEquivalentToAdd(SelectionDAG &DAG, SDValue Op)
132 assert(Op.getOpcode() == ISD::OR);
134 SDValue N0 = Op->getOperand(0);
135 SDValue N1 = Op->getOperand(1);
136 EVT VT = N0.getValueType();
138 if (VT.isInteger() && !VT.isVector()) {
139 KnownBits LHSKnown, RHSKnown;
140 DAG.computeKnownBits(N0, LHSKnown);
142 if (LHSKnown.Zero.getBoolValue()) {
143 DAG.computeKnownBits(N1, RHSKnown);
145 if (!(~RHSKnown.Zero & ~LHSKnown.Zero))
153 AMDGPUTargetLowering::AMDGPUTargetLowering(const TargetMachine &TM,
154 const AMDGPUSubtarget &STI)
155 : TargetLowering(TM), Subtarget(&STI) {
156 AMDGPUASI = AMDGPU::getAMDGPUAS(TM);
157 // Lower floating point store/load to integer store/load to reduce the number
158 // of patterns in tablegen.
159 setOperationAction(ISD::LOAD, MVT::f32, Promote);
160 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
162 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
163 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
165 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
166 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
168 setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
169 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
171 setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
172 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
174 setOperationAction(ISD::LOAD, MVT::i64, Promote);
175 AddPromotedToType(ISD::LOAD, MVT::i64, MVT::v2i32);
177 setOperationAction(ISD::LOAD, MVT::v2i64, Promote);
178 AddPromotedToType(ISD::LOAD, MVT::v2i64, MVT::v4i32);
180 setOperationAction(ISD::LOAD, MVT::f64, Promote);
181 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::v2i32);
183 setOperationAction(ISD::LOAD, MVT::v2f64, Promote);
184 AddPromotedToType(ISD::LOAD, MVT::v2f64, MVT::v4i32);
186 // There are no 64-bit extloads. These should be done as a 32-bit extload and
187 // an extension to 64-bit.
188 for (MVT VT : MVT::integer_valuetypes()) {
189 setLoadExtAction(ISD::EXTLOAD, MVT::i64, VT, Expand);
190 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, VT, Expand);
191 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, VT, Expand);
194 for (MVT VT : MVT::integer_valuetypes()) {
198 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
199 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Legal);
200 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Legal);
201 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i32, Expand);
203 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
204 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Legal);
205 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Legal);
206 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i32, Expand);
208 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
209 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Legal);
210 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Legal);
211 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i32, Expand);
214 for (MVT VT : MVT::integer_vector_valuetypes()) {
215 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i8, Expand);
216 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i8, Expand);
217 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i8, Expand);
218 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i8, Expand);
219 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i8, Expand);
220 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i8, Expand);
221 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i16, Expand);
222 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i16, Expand);
223 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i16, Expand);
224 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i16, Expand);
225 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i16, Expand);
226 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i16, Expand);
229 setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
230 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, MVT::v2f16, Expand);
231 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, MVT::v4f16, Expand);
232 setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, MVT::v8f16, Expand);
234 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand);
235 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f32, Expand);
236 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f32, Expand);
237 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f32, Expand);
239 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
240 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
241 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f16, Expand);
242 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f16, Expand);
244 setOperationAction(ISD::STORE, MVT::f32, Promote);
245 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
247 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
248 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
250 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
251 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
253 setOperationAction(ISD::STORE, MVT::v8f32, Promote);
254 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
256 setOperationAction(ISD::STORE, MVT::v16f32, Promote);
257 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
259 setOperationAction(ISD::STORE, MVT::i64, Promote);
260 AddPromotedToType(ISD::STORE, MVT::i64, MVT::v2i32);
262 setOperationAction(ISD::STORE, MVT::v2i64, Promote);
263 AddPromotedToType(ISD::STORE, MVT::v2i64, MVT::v4i32);
265 setOperationAction(ISD::STORE, MVT::f64, Promote);
266 AddPromotedToType(ISD::STORE, MVT::f64, MVT::v2i32);
268 setOperationAction(ISD::STORE, MVT::v2f64, Promote);
269 AddPromotedToType(ISD::STORE, MVT::v2f64, MVT::v4i32);
271 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
272 setTruncStoreAction(MVT::i64, MVT::i8, Expand);
273 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
274 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
276 setTruncStoreAction(MVT::v2i64, MVT::v2i1, Expand);
277 setTruncStoreAction(MVT::v2i64, MVT::v2i8, Expand);
278 setTruncStoreAction(MVT::v2i64, MVT::v2i16, Expand);
279 setTruncStoreAction(MVT::v2i64, MVT::v2i32, Expand);
281 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
282 setTruncStoreAction(MVT::v2f32, MVT::v2f16, Expand);
283 setTruncStoreAction(MVT::v4f32, MVT::v4f16, Expand);
284 setTruncStoreAction(MVT::v8f32, MVT::v8f16, Expand);
286 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
287 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
289 setTruncStoreAction(MVT::v2f64, MVT::v2f32, Expand);
290 setTruncStoreAction(MVT::v2f64, MVT::v2f16, Expand);
292 setTruncStoreAction(MVT::v4f64, MVT::v4f32, Expand);
293 setTruncStoreAction(MVT::v4f64, MVT::v4f16, Expand);
295 setTruncStoreAction(MVT::v8f64, MVT::v8f32, Expand);
296 setTruncStoreAction(MVT::v8f64, MVT::v8f16, Expand);
299 setOperationAction(ISD::Constant, MVT::i32, Legal);
300 setOperationAction(ISD::Constant, MVT::i64, Legal);
301 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
302 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
304 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
305 setOperationAction(ISD::BRIND, MVT::Other, Expand);
307 // This is totally unsupported, just custom lower to produce an error.
308 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
310 // Library functions. These default to Expand, but we have instructions
312 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
313 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
314 setOperationAction(ISD::FPOW, MVT::f32, Legal);
315 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
316 setOperationAction(ISD::FABS, MVT::f32, Legal);
317 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
318 setOperationAction(ISD::FRINT, MVT::f32, Legal);
319 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
320 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
321 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
323 setOperationAction(ISD::FROUND, MVT::f32, Custom);
324 setOperationAction(ISD::FROUND, MVT::f64, Custom);
326 setOperationAction(ISD::FNEARBYINT, MVT::f32, Custom);
327 setOperationAction(ISD::FNEARBYINT, MVT::f64, Custom);
329 setOperationAction(ISD::FREM, MVT::f32, Custom);
330 setOperationAction(ISD::FREM, MVT::f64, Custom);
332 // v_mad_f32 does not support denormals according to some sources.
333 if (!Subtarget->hasFP32Denormals())
334 setOperationAction(ISD::FMAD, MVT::f32, Legal);
336 // Expand to fneg + fadd.
337 setOperationAction(ISD::FSUB, MVT::f64, Expand);
339 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
340 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
341 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
342 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
343 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
344 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
345 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f32, Custom);
346 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
347 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f32, Custom);
348 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i32, Custom);
350 if (Subtarget->getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
351 setOperationAction(ISD::FCEIL, MVT::f64, Custom);
352 setOperationAction(ISD::FTRUNC, MVT::f64, Custom);
353 setOperationAction(ISD::FRINT, MVT::f64, Custom);
354 setOperationAction(ISD::FFLOOR, MVT::f64, Custom);
357 if (!Subtarget->hasBFI()) {
358 // fcopysign can be done in a single instruction with BFI.
359 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
360 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
363 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
364 setOperationAction(ISD::FP_TO_FP16, MVT::f64, Custom);
365 setOperationAction(ISD::FP_TO_FP16, MVT::f32, Custom);
367 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
368 for (MVT VT : ScalarIntVTs) {
369 // These should use [SU]DIVREM, so set them to expand
370 setOperationAction(ISD::SDIV, VT, Expand);
371 setOperationAction(ISD::UDIV, VT, Expand);
372 setOperationAction(ISD::SREM, VT, Expand);
373 setOperationAction(ISD::UREM, VT, Expand);
375 // GPU does not have divrem function for signed or unsigned.
376 setOperationAction(ISD::SDIVREM, VT, Custom);
377 setOperationAction(ISD::UDIVREM, VT, Custom);
379 // GPU does not have [S|U]MUL_LOHI functions as a single instruction.
380 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
381 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
383 setOperationAction(ISD::BSWAP, VT, Expand);
384 setOperationAction(ISD::CTTZ, VT, Expand);
385 setOperationAction(ISD::CTLZ, VT, Expand);
388 if (!Subtarget->hasBCNT(32))
389 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
391 if (!Subtarget->hasBCNT(64))
392 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
394 // The hardware supports 32-bit ROTR, but not ROTL.
395 setOperationAction(ISD::ROTL, MVT::i32, Expand);
396 setOperationAction(ISD::ROTL, MVT::i64, Expand);
397 setOperationAction(ISD::ROTR, MVT::i64, Expand);
399 setOperationAction(ISD::MUL, MVT::i64, Expand);
400 setOperationAction(ISD::MULHU, MVT::i64, Expand);
401 setOperationAction(ISD::MULHS, MVT::i64, Expand);
402 setOperationAction(ISD::UDIV, MVT::i32, Expand);
403 setOperationAction(ISD::UREM, MVT::i32, Expand);
404 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
405 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
406 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
407 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
408 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
410 setOperationAction(ISD::SMIN, MVT::i32, Legal);
411 setOperationAction(ISD::UMIN, MVT::i32, Legal);
412 setOperationAction(ISD::SMAX, MVT::i32, Legal);
413 setOperationAction(ISD::UMAX, MVT::i32, Legal);
415 if (Subtarget->hasFFBH())
416 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Custom);
418 if (Subtarget->hasFFBL())
419 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Legal);
421 setOperationAction(ISD::CTLZ, MVT::i64, Custom);
422 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
424 // We only really have 32-bit BFE instructions (and 16-bit on VI).
426 // On SI+ there are 64-bit BFEs, but they are scalar only and there isn't any
427 // effort to match them now. We want this to be false for i64 cases when the
428 // extraction isn't restricted to the upper or lower half. Ideally we would
429 // have some pass reduce 64-bit extracts to 32-bit if possible. Extracts that
430 // span the midpoint are probably relatively rare, so don't worry about them
432 if (Subtarget->hasBFE())
433 setHasExtractBitsInsn(true);
435 static const MVT::SimpleValueType VectorIntTypes[] = {
436 MVT::v2i32, MVT::v4i32
439 for (MVT VT : VectorIntTypes) {
440 // Expand the following operations for the current type by default.
441 setOperationAction(ISD::ADD, VT, Expand);
442 setOperationAction(ISD::AND, VT, Expand);
443 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
444 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
445 setOperationAction(ISD::MUL, VT, Expand);
446 setOperationAction(ISD::MULHU, VT, Expand);
447 setOperationAction(ISD::MULHS, VT, Expand);
448 setOperationAction(ISD::OR, VT, Expand);
449 setOperationAction(ISD::SHL, VT, Expand);
450 setOperationAction(ISD::SRA, VT, Expand);
451 setOperationAction(ISD::SRL, VT, Expand);
452 setOperationAction(ISD::ROTL, VT, Expand);
453 setOperationAction(ISD::ROTR, VT, Expand);
454 setOperationAction(ISD::SUB, VT, Expand);
455 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
456 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
457 setOperationAction(ISD::SDIV, VT, Expand);
458 setOperationAction(ISD::UDIV, VT, Expand);
459 setOperationAction(ISD::SREM, VT, Expand);
460 setOperationAction(ISD::UREM, VT, Expand);
461 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
462 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
463 setOperationAction(ISD::SDIVREM, VT, Custom);
464 setOperationAction(ISD::UDIVREM, VT, Expand);
465 setOperationAction(ISD::ADDC, VT, Expand);
466 setOperationAction(ISD::SUBC, VT, Expand);
467 setOperationAction(ISD::ADDE, VT, Expand);
468 setOperationAction(ISD::SUBE, VT, Expand);
469 setOperationAction(ISD::SELECT, VT, Expand);
470 setOperationAction(ISD::VSELECT, VT, Expand);
471 setOperationAction(ISD::SELECT_CC, VT, Expand);
472 setOperationAction(ISD::XOR, VT, Expand);
473 setOperationAction(ISD::BSWAP, VT, Expand);
474 setOperationAction(ISD::CTPOP, VT, Expand);
475 setOperationAction(ISD::CTTZ, VT, Expand);
476 setOperationAction(ISD::CTLZ, VT, Expand);
477 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
480 static const MVT::SimpleValueType FloatVectorTypes[] = {
481 MVT::v2f32, MVT::v4f32
484 for (MVT VT : FloatVectorTypes) {
485 setOperationAction(ISD::FABS, VT, Expand);
486 setOperationAction(ISD::FMINNUM, VT, Expand);
487 setOperationAction(ISD::FMAXNUM, VT, Expand);
488 setOperationAction(ISD::FADD, VT, Expand);
489 setOperationAction(ISD::FCEIL, VT, Expand);
490 setOperationAction(ISD::FCOS, VT, Expand);
491 setOperationAction(ISD::FDIV, VT, Expand);
492 setOperationAction(ISD::FEXP2, VT, Expand);
493 setOperationAction(ISD::FLOG2, VT, Expand);
494 setOperationAction(ISD::FREM, VT, Expand);
495 setOperationAction(ISD::FPOW, VT, Expand);
496 setOperationAction(ISD::FFLOOR, VT, Expand);
497 setOperationAction(ISD::FTRUNC, VT, Expand);
498 setOperationAction(ISD::FMUL, VT, Expand);
499 setOperationAction(ISD::FMA, VT, Expand);
500 setOperationAction(ISD::FRINT, VT, Expand);
501 setOperationAction(ISD::FNEARBYINT, VT, Expand);
502 setOperationAction(ISD::FSQRT, VT, Expand);
503 setOperationAction(ISD::FSIN, VT, Expand);
504 setOperationAction(ISD::FSUB, VT, Expand);
505 setOperationAction(ISD::FNEG, VT, Expand);
506 setOperationAction(ISD::VSELECT, VT, Expand);
507 setOperationAction(ISD::SELECT_CC, VT, Expand);
508 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
509 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
512 // This causes using an unrolled select operation rather than expansion with
513 // bit operations. This is in general better, but the alternative using BFI
514 // instructions may be better if the select sources are SGPRs.
515 setOperationAction(ISD::SELECT, MVT::v2f32, Promote);
516 AddPromotedToType(ISD::SELECT, MVT::v2f32, MVT::v2i32);
518 setOperationAction(ISD::SELECT, MVT::v4f32, Promote);
519 AddPromotedToType(ISD::SELECT, MVT::v4f32, MVT::v4i32);
521 // There are no libcalls of any kind.
522 for (int I = 0; I < RTLIB::UNKNOWN_LIBCALL; ++I)
523 setLibcallName(static_cast<RTLIB::Libcall>(I), nullptr);
525 setBooleanContents(ZeroOrNegativeOneBooleanContent);
526 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
528 setSchedulingPreference(Sched::RegPressure);
529 setJumpIsExpensive(true);
531 // FIXME: This is only partially true. If we have to do vector compares, any
532 // SGPR pair can be a condition register. If we have a uniform condition, we
533 // are better off doing SALU operations, where there is only one SCC. For now,
534 // we don't have a way of knowing during instruction selection if a condition
535 // will be uniform and we always use vector compares. Assume we are using
536 // vector compares until that is fixed.
537 setHasMultipleConditionRegisters(true);
539 // SI at least has hardware support for floating point exceptions, but no way
540 // of using or handling them is implemented. They are also optional in OpenCL
542 setHasFloatingPointExceptions(Subtarget->hasFPExceptions());
544 PredictableSelectIsExpensive = false;
546 // We want to find all load dependencies for long chains of stores to enable
547 // merging into very wide vectors. The problem is with vectors with > 4
548 // elements. MergeConsecutiveStores will attempt to merge these because x8/x16
549 // vectors are a legal type, even though we have to split the loads
550 // usually. When we can more precisely specify load legality per address
551 // space, we should be able to make FindBetterChain/MergeConsecutiveStores
552 // smarter so that they can figure out what to do in 2 iterations without all
553 // N > 4 stores on the same chain.
554 GatherAllAliasesMaxDepth = 16;
556 // memcpy/memmove/memset are expanded in the IR, so we shouldn't need to worry
557 // about these during lowering.
558 MaxStoresPerMemcpy = 0xffffffff;
559 MaxStoresPerMemmove = 0xffffffff;
560 MaxStoresPerMemset = 0xffffffff;
562 setTargetDAGCombine(ISD::BITCAST);
563 setTargetDAGCombine(ISD::SHL);
564 setTargetDAGCombine(ISD::SRA);
565 setTargetDAGCombine(ISD::SRL);
566 setTargetDAGCombine(ISD::MUL);
567 setTargetDAGCombine(ISD::MULHU);
568 setTargetDAGCombine(ISD::MULHS);
569 setTargetDAGCombine(ISD::SELECT);
570 setTargetDAGCombine(ISD::SELECT_CC);
571 setTargetDAGCombine(ISD::STORE);
572 setTargetDAGCombine(ISD::FADD);
573 setTargetDAGCombine(ISD::FSUB);
574 setTargetDAGCombine(ISD::FNEG);
575 setTargetDAGCombine(ISD::FABS);
578 //===----------------------------------------------------------------------===//
579 // Target Information
580 //===----------------------------------------------------------------------===//
583 static bool fnegFoldsIntoOp(unsigned Opc) {
595 case ISD::FNEARBYINT:
597 case AMDGPUISD::RCP_LEGACY:
598 case AMDGPUISD::SIN_HW:
599 case AMDGPUISD::FMUL_LEGACY:
600 case AMDGPUISD::FMIN_LEGACY:
601 case AMDGPUISD::FMAX_LEGACY:
608 /// \p returns true if the operation will definitely need to use a 64-bit
609 /// encoding, and thus will use a VOP3 encoding regardless of the source
612 static bool opMustUseVOP3Encoding(const SDNode *N, MVT VT) {
613 return N->getNumOperands() > 2 || VT == MVT::f64;
616 // Most FP instructions support source modifiers, but this could be refined
619 static bool hasSourceMods(const SDNode *N) {
620 if (isa<MemSDNode>(N))
623 switch (N->getOpcode()) {
629 case AMDGPUISD::INTERP_P1:
630 case AMDGPUISD::INTERP_P2:
631 case AMDGPUISD::DIV_SCALE:
633 // TODO: Should really be looking at the users of the bitcast. These are
634 // problematic because bitcasts are used to legalize all stores to integer
643 bool AMDGPUTargetLowering::allUsesHaveSourceMods(const SDNode *N,
644 unsigned CostThreshold) {
645 // Some users (such as 3-operand FMA/MAD) must use a VOP3 encoding, and thus
646 // it is truly free to use a source modifier in all cases. If there are
647 // multiple users but for each one will necessitate using VOP3, there will be
648 // a code size increase. Try to avoid increasing code size unless we know it
649 // will save on the instruction count.
650 unsigned NumMayIncreaseSize = 0;
651 MVT VT = N->getValueType(0).getScalarType().getSimpleVT();
653 // XXX - Should this limit number of uses to check?
654 for (const SDNode *U : N->uses()) {
655 if (!hasSourceMods(U))
658 if (!opMustUseVOP3Encoding(U, VT)) {
659 if (++NumMayIncreaseSize > CostThreshold)
667 MVT AMDGPUTargetLowering::getVectorIdxTy(const DataLayout &) const {
671 bool AMDGPUTargetLowering::isSelectSupported(SelectSupportKind SelType) const {
675 // The backend supports 32 and 64 bit floating point immediates.
676 // FIXME: Why are we reporting vectors of FP immediates as legal?
677 bool AMDGPUTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
678 EVT ScalarVT = VT.getScalarType();
679 return (ScalarVT == MVT::f32 || ScalarVT == MVT::f64 ||
680 (ScalarVT == MVT::f16 && Subtarget->has16BitInsts()));
683 // We don't want to shrink f64 / f32 constants.
684 bool AMDGPUTargetLowering::ShouldShrinkFPConstant(EVT VT) const {
685 EVT ScalarVT = VT.getScalarType();
686 return (ScalarVT != MVT::f32 && ScalarVT != MVT::f64);
689 bool AMDGPUTargetLowering::shouldReduceLoadWidth(SDNode *N,
693 unsigned NewSize = NewVT.getStoreSizeInBits();
695 // If we are reducing to a 32-bit load, this is always better.
699 EVT OldVT = N->getValueType(0);
700 unsigned OldSize = OldVT.getStoreSizeInBits();
702 // Don't produce extloads from sub 32-bit types. SI doesn't have scalar
703 // extloads, so doing one requires using a buffer_load. In cases where we
704 // still couldn't use a scalar load, using the wider load shouldn't really
707 // If the old size already had to be an extload, there's no harm in continuing
708 // to reduce the width.
709 return (OldSize < 32);
712 bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy,
715 assert(LoadTy.getSizeInBits() == CastTy.getSizeInBits());
717 if (LoadTy.getScalarType() == MVT::i32)
720 unsigned LScalarSize = LoadTy.getScalarSizeInBits();
721 unsigned CastScalarSize = CastTy.getScalarSizeInBits();
723 return (LScalarSize < CastScalarSize) ||
724 (CastScalarSize >= 32);
727 // SI+ has instructions for cttz / ctlz for 32-bit values. This is probably also
728 // profitable with the expansion for 64-bit since it's generally good to
730 // FIXME: These should really have the size as a parameter.
731 bool AMDGPUTargetLowering::isCheapToSpeculateCttz() const {
735 bool AMDGPUTargetLowering::isCheapToSpeculateCtlz() const {
739 //===---------------------------------------------------------------------===//
741 //===---------------------------------------------------------------------===//
743 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
744 assert(VT.isFloatingPoint());
746 // Packed operations do not have a fabs modifier.
747 return VT == MVT::f32 || VT == MVT::f64 ||
748 (Subtarget->has16BitInsts() && VT == MVT::f16);
751 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
752 assert(VT.isFloatingPoint());
753 return VT == MVT::f32 || VT == MVT::f64 ||
754 (Subtarget->has16BitInsts() && VT == MVT::f16) ||
755 (Subtarget->hasVOP3PInsts() && VT == MVT::v2f16);
758 bool AMDGPUTargetLowering:: storeOfVectorConstantIsCheap(EVT MemVT,
764 bool AMDGPUTargetLowering::aggressivelyPreferBuildVectorSources(EVT VecVT) const {
765 // There are few operations which truly have vector input operands. Any vector
766 // operation is going to involve operations on each component, and a
767 // build_vector will be a copy per element, so it always makes sense to use a
768 // build_vector input in place of the extracted element to avoid a copy into a
771 // We should probably only do this if all users are extracts only, but this
772 // should be the common case.
776 bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const {
777 // Truncate is just accessing a subregister.
779 unsigned SrcSize = Source.getSizeInBits();
780 unsigned DestSize = Dest.getSizeInBits();
782 return DestSize < SrcSize && DestSize % 32 == 0 ;
785 bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const {
786 // Truncate is just accessing a subregister.
788 unsigned SrcSize = Source->getScalarSizeInBits();
789 unsigned DestSize = Dest->getScalarSizeInBits();
791 if (DestSize== 16 && Subtarget->has16BitInsts())
792 return SrcSize >= 32;
794 return DestSize < SrcSize && DestSize % 32 == 0;
797 bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const {
798 unsigned SrcSize = Src->getScalarSizeInBits();
799 unsigned DestSize = Dest->getScalarSizeInBits();
801 if (SrcSize == 16 && Subtarget->has16BitInsts())
802 return DestSize >= 32;
804 return SrcSize == 32 && DestSize == 64;
807 bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const {
808 // Any register load of a 64-bit value really requires 2 32-bit moves. For all
809 // practical purposes, the extra mov 0 to load a 64-bit is free. As used,
810 // this will enable reducing 64-bit operations the 32-bit, which is always
814 return Dest == MVT::i32 ||Dest == MVT::i64 ;
816 return Src == MVT::i32 && Dest == MVT::i64;
819 bool AMDGPUTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
820 return isZExtFree(Val.getValueType(), VT2);
823 bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const {
824 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a
825 // limited number of native 64-bit operations. Shrinking an operation to fit
826 // in a single 32-bit register should always be helpful. As currently used,
827 // this is much less general than the name suggests, and is only used in
828 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is
829 // not profitable, and may actually be harmful.
830 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32;
833 //===---------------------------------------------------------------------===//
834 // TargetLowering Callbacks
835 //===---------------------------------------------------------------------===//
837 CCAssignFn *AMDGPUCallLowering::CCAssignFnForCall(CallingConv::ID CC,
840 case CallingConv::AMDGPU_KERNEL:
841 case CallingConv::SPIR_KERNEL:
842 return CC_AMDGPU_Kernel;
843 case CallingConv::AMDGPU_VS:
844 case CallingConv::AMDGPU_GS:
845 case CallingConv::AMDGPU_PS:
846 case CallingConv::AMDGPU_CS:
847 case CallingConv::AMDGPU_HS:
850 case CallingConv::Fast:
851 return CC_AMDGPU_Func;
853 report_fatal_error("Unsupported calling convention.");
857 CCAssignFn *AMDGPUCallLowering::CCAssignFnForReturn(CallingConv::ID CC,
860 case CallingConv::AMDGPU_KERNEL:
861 case CallingConv::SPIR_KERNEL:
862 return CC_AMDGPU_Kernel;
863 case CallingConv::AMDGPU_VS:
864 case CallingConv::AMDGPU_GS:
865 case CallingConv::AMDGPU_PS:
866 case CallingConv::AMDGPU_CS:
867 case CallingConv::AMDGPU_HS:
868 return RetCC_SI_Shader;
870 case CallingConv::Fast:
871 return RetCC_AMDGPU_Func;
873 report_fatal_error("Unsupported calling convention.");
877 /// The SelectionDAGBuilder will automatically promote function arguments
878 /// with illegal types. However, this does not work for the AMDGPU targets
879 /// since the function arguments are stored in memory as these illegal types.
880 /// In order to handle this properly we need to get the original types sizes
881 /// from the LLVM IR Function and fixup the ISD:InputArg values before
882 /// passing them to AnalyzeFormalArguments()
884 /// When the SelectionDAGBuilder computes the Ins, it takes care of splitting
885 /// input values across multiple registers. Each item in the Ins array
886 /// represents a single value that will be stored in regsters. Ins[x].VT is
887 /// the value type of the value that will be stored in the register, so
888 /// whatever SDNode we lower the argument to needs to be this type.
890 /// In order to correctly lower the arguments we need to know the size of each
891 /// argument. Since Ins[x].VT gives us the size of the register that will
892 /// hold the value, we need to look at Ins[x].ArgVT to see the 'real' type
893 /// for the orignal function argument so that we can deduce the correct memory
894 /// type to use for Ins[x]. In most cases the correct memory type will be
895 /// Ins[x].ArgVT. However, this will not always be the case. If, for example,
896 /// we have a kernel argument of type v8i8, this argument will be split into
897 /// 8 parts and each part will be represented by its own item in the Ins array.
898 /// For each part the Ins[x].ArgVT will be the v8i8, which is the full type of
899 /// the argument before it was split. From this, we deduce that the memory type
900 /// for each individual part is i8. We pass the memory type as LocVT to the
901 /// calling convention analysis function and the register type (Ins[x].VT) as
903 void AMDGPUTargetLowering::analyzeFormalArgumentsCompute(CCState &State,
904 const SmallVectorImpl<ISD::InputArg> &Ins) const {
905 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
906 const ISD::InputArg &In = Ins[i];
909 unsigned NumRegs = getNumRegisters(State.getContext(), In.ArgVT);
911 if (!Subtarget->isAmdHsaOS() &&
912 (In.ArgVT == MVT::i16 || In.ArgVT == MVT::i8 || In.ArgVT == MVT::f16)) {
913 // The ABI says the caller will extend these values to 32-bits.
914 MemVT = In.ArgVT.isInteger() ? MVT::i32 : MVT::f32;
915 } else if (NumRegs == 1) {
916 // This argument is not split, so the IR type is the memory type.
917 assert(!In.Flags.isSplit());
918 if (In.ArgVT.isExtended()) {
919 // We have an extended type, like i24, so we should just use the register type
924 } else if (In.ArgVT.isVector() && In.VT.isVector() &&
925 In.ArgVT.getScalarType() == In.VT.getScalarType()) {
926 assert(In.ArgVT.getVectorNumElements() > In.VT.getVectorNumElements());
927 // We have a vector value which has been split into a vector with
928 // the same scalar type, but fewer elements. This should handle
929 // all the floating-point vector types.
931 } else if (In.ArgVT.isVector() &&
932 In.ArgVT.getVectorNumElements() == NumRegs) {
933 // This arg has been split so that each element is stored in a separate
935 MemVT = In.ArgVT.getScalarType();
936 } else if (In.ArgVT.isExtended()) {
937 // We have an extended type, like i65.
940 unsigned MemoryBits = In.ArgVT.getStoreSizeInBits() / NumRegs;
941 assert(In.ArgVT.getStoreSizeInBits() % NumRegs == 0);
942 if (In.VT.isInteger()) {
943 MemVT = EVT::getIntegerVT(State.getContext(), MemoryBits);
944 } else if (In.VT.isVector()) {
945 assert(!In.VT.getScalarType().isFloatingPoint());
946 unsigned NumElements = In.VT.getVectorNumElements();
947 assert(MemoryBits % NumElements == 0);
948 // This vector type has been split into another vector type with
949 // a different elements size.
950 EVT ScalarVT = EVT::getIntegerVT(State.getContext(),
951 MemoryBits / NumElements);
952 MemVT = EVT::getVectorVT(State.getContext(), ScalarVT, NumElements);
954 llvm_unreachable("cannot deduce memory type.");
958 // Convert one element vectors to scalar.
959 if (MemVT.isVector() && MemVT.getVectorNumElements() == 1)
960 MemVT = MemVT.getScalarType();
962 if (MemVT.isExtended()) {
963 // This should really only happen if we have vec3 arguments
964 assert(MemVT.isVector() && MemVT.getVectorNumElements() == 3);
965 MemVT = MemVT.getPow2VectorType(State.getContext());
968 assert(MemVT.isSimple());
969 allocateKernArg(i, In.VT, MemVT.getSimpleVT(), CCValAssign::Full, In.Flags,
974 SDValue AMDGPUTargetLowering::LowerReturn(
975 SDValue Chain, CallingConv::ID CallConv,
977 const SmallVectorImpl<ISD::OutputArg> &Outs,
978 const SmallVectorImpl<SDValue> &OutVals,
979 const SDLoc &DL, SelectionDAG &DAG) const {
980 // FIXME: Fails for r600 tests
981 //assert(!isVarArg && Outs.empty() && OutVals.empty() &&
982 // "wave terminate should not have return values");
983 return DAG.getNode(AMDGPUISD::ENDPGM, DL, MVT::Other, Chain);
986 //===---------------------------------------------------------------------===//
987 // Target specific lowering
988 //===---------------------------------------------------------------------===//
990 /// Selects the correct CCAssignFn for a given CallingConvention value.
991 CCAssignFn *AMDGPUTargetLowering::CCAssignFnForCall(CallingConv::ID CC,
993 return AMDGPUCallLowering::CCAssignFnForCall(CC, IsVarArg);
996 CCAssignFn *AMDGPUTargetLowering::CCAssignFnForReturn(CallingConv::ID CC,
998 return AMDGPUCallLowering::CCAssignFnForReturn(CC, IsVarArg);
1001 SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI,
1002 SmallVectorImpl<SDValue> &InVals) const {
1003 SDValue Callee = CLI.Callee;
1004 SelectionDAG &DAG = CLI.DAG;
1006 const Function &Fn = *DAG.getMachineFunction().getFunction();
1008 StringRef FuncName("<unknown>");
1010 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee))
1011 FuncName = G->getSymbol();
1012 else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1013 FuncName = G->getGlobal()->getName();
1015 DiagnosticInfoUnsupported NoCalls(
1016 Fn, "unsupported call to function " + FuncName, CLI.DL.getDebugLoc());
1017 DAG.getContext()->diagnose(NoCalls);
1019 if (!CLI.IsTailCall) {
1020 for (unsigned I = 0, E = CLI.Ins.size(); I != E; ++I)
1021 InVals.push_back(DAG.getUNDEF(CLI.Ins[I].VT));
1024 return DAG.getEntryNode();
1027 SDValue AMDGPUTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
1028 SelectionDAG &DAG) const {
1029 const Function &Fn = *DAG.getMachineFunction().getFunction();
1031 DiagnosticInfoUnsupported NoDynamicAlloca(Fn, "unsupported dynamic alloca",
1032 SDLoc(Op).getDebugLoc());
1033 DAG.getContext()->diagnose(NoDynamicAlloca);
1034 auto Ops = {DAG.getConstant(0, SDLoc(), Op.getValueType()), Op.getOperand(0)};
1035 return DAG.getMergeValues(Ops, SDLoc());
1038 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op,
1039 SelectionDAG &DAG) const {
1040 switch (Op.getOpcode()) {
1042 Op->print(errs(), &DAG);
1043 llvm_unreachable("Custom lowering code for this"
1044 "instruction is not implemented yet!");
1046 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
1047 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
1048 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
1049 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
1050 case ISD::SDIVREM: return LowerSDIVREM(Op, DAG);
1051 case ISD::FREM: return LowerFREM(Op, DAG);
1052 case ISD::FCEIL: return LowerFCEIL(Op, DAG);
1053 case ISD::FTRUNC: return LowerFTRUNC(Op, DAG);
1054 case ISD::FRINT: return LowerFRINT(Op, DAG);
1055 case ISD::FNEARBYINT: return LowerFNEARBYINT(Op, DAG);
1056 case ISD::FROUND: return LowerFROUND(Op, DAG);
1057 case ISD::FFLOOR: return LowerFFLOOR(Op, DAG);
1058 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
1059 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
1060 case ISD::FP_TO_FP16: return LowerFP_TO_FP16(Op, DAG);
1061 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
1062 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
1064 case ISD::CTLZ_ZERO_UNDEF:
1065 return LowerCTLZ(Op, DAG);
1066 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
1071 void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N,
1072 SmallVectorImpl<SDValue> &Results,
1073 SelectionDAG &DAG) const {
1074 switch (N->getOpcode()) {
1075 case ISD::SIGN_EXTEND_INREG:
1076 // Different parts of legalization seem to interpret which type of
1077 // sign_extend_inreg is the one to check for custom lowering. The extended
1078 // from type is what really matters, but some places check for custom
1079 // lowering of the result type. This results in trying to use
1080 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do
1081 // nothing here and let the illegal result integer be handled normally.
1088 static bool hasDefinedInitializer(const GlobalValue *GV) {
1089 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
1090 if (!GVar || !GVar->hasInitializer())
1093 return !isa<UndefValue>(GVar->getInitializer());
1096 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
1098 SelectionDAG &DAG) const {
1100 const DataLayout &DL = DAG.getDataLayout();
1101 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
1102 const GlobalValue *GV = G->getGlobal();
1104 if (G->getAddressSpace() == AMDGPUASI.LOCAL_ADDRESS) {
1105 // XXX: What does the value of G->getOffset() mean?
1106 assert(G->getOffset() == 0 &&
1107 "Do not know what to do with an non-zero offset");
1109 // TODO: We could emit code to handle the initialization somewhere.
1110 if (!hasDefinedInitializer(GV)) {
1111 unsigned Offset = MFI->allocateLDSGlobal(DL, *GV);
1112 return DAG.getConstant(Offset, SDLoc(Op), Op.getValueType());
1116 const Function &Fn = *DAG.getMachineFunction().getFunction();
1117 DiagnosticInfoUnsupported BadInit(
1118 Fn, "unsupported initializer for address space", SDLoc(Op).getDebugLoc());
1119 DAG.getContext()->diagnose(BadInit);
1123 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
1124 SelectionDAG &DAG) const {
1125 SmallVector<SDValue, 8> Args;
1127 for (const SDUse &U : Op->ops())
1128 DAG.ExtractVectorElements(U.get(), Args);
1130 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
1133 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
1134 SelectionDAG &DAG) const {
1136 SmallVector<SDValue, 8> Args;
1137 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1138 EVT VT = Op.getValueType();
1139 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start,
1140 VT.getVectorNumElements());
1142 return DAG.getBuildVector(Op.getValueType(), SDLoc(Op), Args);
1145 /// \brief Generate Min/Max node
1146 SDValue AMDGPUTargetLowering::combineFMinMaxLegacy(const SDLoc &DL, EVT VT,
1147 SDValue LHS, SDValue RHS,
1148 SDValue True, SDValue False,
1150 DAGCombinerInfo &DCI) const {
1151 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True))
1154 SelectionDAG &DAG = DCI.DAG;
1155 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1164 case ISD::SETFALSE2:
1173 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1174 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1180 // Ordered. Assume ordered for undefined.
1182 // Only do this after legalization to avoid interfering with other combines
1183 // which might occur.
1184 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1185 !DCI.isCalledByLegalizer())
1188 // We need to permute the operands to get the correct NaN behavior. The
1189 // selected operand is the second one based on the failing compare with NaN,
1190 // so permute it based on the compare type the hardware uses.
1192 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
1193 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
1198 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
1199 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
1205 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1206 !DCI.isCalledByLegalizer())
1210 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1211 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1213 case ISD::SETCC_INVALID:
1214 llvm_unreachable("Invalid setcc condcode!");
1219 std::pair<SDValue, SDValue>
1220 AMDGPUTargetLowering::split64BitValue(SDValue Op, SelectionDAG &DAG) const {
1223 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1225 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1226 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1228 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1229 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1231 return std::make_pair(Lo, Hi);
1234 SDValue AMDGPUTargetLowering::getLoHalf64(SDValue Op, SelectionDAG &DAG) const {
1237 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1238 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1239 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1242 SDValue AMDGPUTargetLowering::getHiHalf64(SDValue Op, SelectionDAG &DAG) const {
1245 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Op);
1246 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1247 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1250 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue Op,
1251 SelectionDAG &DAG) const {
1252 LoadSDNode *Load = cast<LoadSDNode>(Op);
1253 EVT VT = Op.getValueType();
1256 // If this is a 2 element vector, we really want to scalarize and not create
1257 // weird 1 element vectors.
1258 if (VT.getVectorNumElements() == 2)
1259 return scalarizeVectorLoad(Load, DAG);
1261 SDValue BasePtr = Load->getBasePtr();
1262 EVT PtrVT = BasePtr.getValueType();
1263 EVT MemVT = Load->getMemoryVT();
1266 const MachinePointerInfo &SrcValue = Load->getMemOperand()->getPointerInfo();
1269 EVT LoMemVT, HiMemVT;
1272 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1273 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1274 std::tie(Lo, Hi) = DAG.SplitVector(Op, SL, LoVT, HiVT);
1276 unsigned Size = LoMemVT.getStoreSize();
1277 unsigned BaseAlign = Load->getAlignment();
1278 unsigned HiAlign = MinAlign(BaseAlign, Size);
1280 SDValue LoLoad = DAG.getExtLoad(Load->getExtensionType(), SL, LoVT,
1281 Load->getChain(), BasePtr, SrcValue, LoMemVT,
1282 BaseAlign, Load->getMemOperand()->getFlags());
1283 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
1284 DAG.getConstant(Size, SL, PtrVT));
1286 DAG.getExtLoad(Load->getExtensionType(), SL, HiVT, Load->getChain(),
1287 HiPtr, SrcValue.getWithOffset(LoMemVT.getStoreSize()),
1288 HiMemVT, HiAlign, Load->getMemOperand()->getFlags());
1291 DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, LoLoad, HiLoad),
1292 DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
1293 LoLoad.getValue(1), HiLoad.getValue(1))
1296 return DAG.getMergeValues(Ops, SL);
1299 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
1300 SelectionDAG &DAG) const {
1301 StoreSDNode *Store = cast<StoreSDNode>(Op);
1302 SDValue Val = Store->getValue();
1303 EVT VT = Val.getValueType();
1305 // If this is a 2 element vector, we really want to scalarize and not create
1306 // weird 1 element vectors.
1307 if (VT.getVectorNumElements() == 2)
1308 return scalarizeVectorStore(Store, DAG);
1310 EVT MemVT = Store->getMemoryVT();
1311 SDValue Chain = Store->getChain();
1312 SDValue BasePtr = Store->getBasePtr();
1316 EVT LoMemVT, HiMemVT;
1319 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1320 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1321 std::tie(Lo, Hi) = DAG.SplitVector(Val, SL, LoVT, HiVT);
1323 EVT PtrVT = BasePtr.getValueType();
1324 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
1325 DAG.getConstant(LoMemVT.getStoreSize(), SL,
1328 const MachinePointerInfo &SrcValue = Store->getMemOperand()->getPointerInfo();
1329 unsigned BaseAlign = Store->getAlignment();
1330 unsigned Size = LoMemVT.getStoreSize();
1331 unsigned HiAlign = MinAlign(BaseAlign, Size);
1334 DAG.getTruncStore(Chain, SL, Lo, BasePtr, SrcValue, LoMemVT, BaseAlign,
1335 Store->getMemOperand()->getFlags());
1337 DAG.getTruncStore(Chain, SL, Hi, HiPtr, SrcValue.getWithOffset(Size),
1338 HiMemVT, HiAlign, Store->getMemOperand()->getFlags());
1340 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoStore, HiStore);
1343 // This is a shortcut for integer division because we have fast i32<->f32
1344 // conversions, and fast f32 reciprocal instructions. The fractional part of a
1345 // float is enough to accurately represent up to a 24-bit signed integer.
1346 SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG,
1349 EVT VT = Op.getValueType();
1350 SDValue LHS = Op.getOperand(0);
1351 SDValue RHS = Op.getOperand(1);
1352 MVT IntVT = MVT::i32;
1353 MVT FltVT = MVT::f32;
1355 unsigned LHSSignBits = DAG.ComputeNumSignBits(LHS);
1356 if (LHSSignBits < 9)
1359 unsigned RHSSignBits = DAG.ComputeNumSignBits(RHS);
1360 if (RHSSignBits < 9)
1363 unsigned BitSize = VT.getSizeInBits();
1364 unsigned SignBits = std::min(LHSSignBits, RHSSignBits);
1365 unsigned DivBits = BitSize - SignBits;
1369 ISD::NodeType ToFp = Sign ? ISD::SINT_TO_FP : ISD::UINT_TO_FP;
1370 ISD::NodeType ToInt = Sign ? ISD::FP_TO_SINT : ISD::FP_TO_UINT;
1372 SDValue jq = DAG.getConstant(1, DL, IntVT);
1375 // char|short jq = ia ^ ib;
1376 jq = DAG.getNode(ISD::XOR, DL, VT, LHS, RHS);
1378 // jq = jq >> (bitsize - 2)
1379 jq = DAG.getNode(ISD::SRA, DL, VT, jq,
1380 DAG.getConstant(BitSize - 2, DL, VT));
1383 jq = DAG.getNode(ISD::OR, DL, VT, jq, DAG.getConstant(1, DL, VT));
1386 // int ia = (int)LHS;
1389 // int ib, (int)RHS;
1392 // float fa = (float)ia;
1393 SDValue fa = DAG.getNode(ToFp, DL, FltVT, ia);
1395 // float fb = (float)ib;
1396 SDValue fb = DAG.getNode(ToFp, DL, FltVT, ib);
1398 SDValue fq = DAG.getNode(ISD::FMUL, DL, FltVT,
1399 fa, DAG.getNode(AMDGPUISD::RCP, DL, FltVT, fb));
1402 fq = DAG.getNode(ISD::FTRUNC, DL, FltVT, fq);
1404 // float fqneg = -fq;
1405 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FltVT, fq);
1407 // float fr = mad(fqneg, fb, fa);
1408 unsigned OpCode = Subtarget->hasFP32Denormals() ?
1409 (unsigned)AMDGPUISD::FMAD_FTZ :
1410 (unsigned)ISD::FMAD;
1411 SDValue fr = DAG.getNode(OpCode, DL, FltVT, fqneg, fb, fa);
1413 // int iq = (int)fq;
1414 SDValue iq = DAG.getNode(ToInt, DL, IntVT, fq);
1417 fr = DAG.getNode(ISD::FABS, DL, FltVT, fr);
1420 fb = DAG.getNode(ISD::FABS, DL, FltVT, fb);
1422 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
1424 // int cv = fr >= fb;
1425 SDValue cv = DAG.getSetCC(DL, SetCCVT, fr, fb, ISD::SETOGE);
1427 // jq = (cv ? jq : 0);
1428 jq = DAG.getNode(ISD::SELECT, DL, VT, cv, jq, DAG.getConstant(0, DL, VT));
1431 SDValue Div = DAG.getNode(ISD::ADD, DL, VT, iq, jq);
1433 // Rem needs compensation, it's easier to recompute it
1434 SDValue Rem = DAG.getNode(ISD::MUL, DL, VT, Div, RHS);
1435 Rem = DAG.getNode(ISD::SUB, DL, VT, LHS, Rem);
1437 // Truncate to number of bits this divide really is.
1440 = DAG.getValueType(EVT::getIntegerVT(*DAG.getContext(), DivBits));
1441 Div = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Div, InRegSize);
1442 Rem = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, Rem, InRegSize);
1444 SDValue TruncMask = DAG.getConstant((UINT64_C(1) << DivBits) - 1, DL, VT);
1445 Div = DAG.getNode(ISD::AND, DL, VT, Div, TruncMask);
1446 Rem = DAG.getNode(ISD::AND, DL, VT, Rem, TruncMask);
1449 return DAG.getMergeValues({ Div, Rem }, DL);
1452 void AMDGPUTargetLowering::LowerUDIVREM64(SDValue Op,
1454 SmallVectorImpl<SDValue> &Results) const {
1455 assert(Op.getValueType() == MVT::i64);
1458 EVT VT = Op.getValueType();
1459 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1461 SDValue one = DAG.getConstant(1, DL, HalfVT);
1462 SDValue zero = DAG.getConstant(0, DL, HalfVT);
1465 SDValue LHS = Op.getOperand(0);
1466 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero);
1467 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one);
1469 SDValue RHS = Op.getOperand(1);
1470 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero);
1471 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one);
1473 if (VT == MVT::i64 &&
1474 DAG.MaskedValueIsZero(RHS, APInt::getHighBitsSet(64, 32)) &&
1475 DAG.MaskedValueIsZero(LHS, APInt::getHighBitsSet(64, 32))) {
1477 SDValue Res = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1480 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(0), zero});
1481 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {Res.getValue(1), zero});
1483 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV));
1484 Results.push_back(DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM));
1488 // Get Speculative values
1489 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo);
1490 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo);
1492 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ);
1493 SDValue REM = DAG.getBuildVector(MVT::v2i32, DL, {REM_Lo, zero});
1494 REM = DAG.getNode(ISD::BITCAST, DL, MVT::i64, REM);
1496 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ);
1497 SDValue DIV_Lo = zero;
1499 const unsigned halfBitWidth = HalfVT.getSizeInBits();
1501 for (unsigned i = 0; i < halfBitWidth; ++i) {
1502 const unsigned bitPos = halfBitWidth - i - 1;
1503 SDValue POS = DAG.getConstant(bitPos, DL, HalfVT);
1504 // Get value of high bit
1505 SDValue HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS);
1506 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one);
1507 HBit = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, HBit);
1510 REM = DAG.getNode(ISD::SHL, DL, VT, REM, DAG.getConstant(1, DL, VT));
1512 REM = DAG.getNode(ISD::OR, DL, VT, REM, HBit);
1514 SDValue BIT = DAG.getConstant(1ULL << bitPos, DL, HalfVT);
1515 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETUGE);
1517 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT);
1520 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS);
1521 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETUGE);
1524 SDValue DIV = DAG.getBuildVector(MVT::v2i32, DL, {DIV_Lo, DIV_Hi});
1525 DIV = DAG.getNode(ISD::BITCAST, DL, MVT::i64, DIV);
1526 Results.push_back(DIV);
1527 Results.push_back(REM);
1530 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
1531 SelectionDAG &DAG) const {
1533 EVT VT = Op.getValueType();
1535 if (VT == MVT::i64) {
1536 SmallVector<SDValue, 2> Results;
1537 LowerUDIVREM64(Op, DAG, Results);
1538 return DAG.getMergeValues(Results, DL);
1541 if (VT == MVT::i32) {
1542 if (SDValue Res = LowerDIVREM24(Op, DAG, false))
1546 SDValue Num = Op.getOperand(0);
1547 SDValue Den = Op.getOperand(1);
1549 // RCP = URECIP(Den) = 2^32 / Den + e
1550 // e is rounding error.
1551 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
1553 // RCP_LO = mul(RCP, Den) */
1554 SDValue RCP_LO = DAG.getNode(ISD::MUL, DL, VT, RCP, Den);
1556 // RCP_HI = mulhu (RCP, Den) */
1557 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
1559 // NEG_RCP_LO = -RCP_LO
1560 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
1563 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
1564 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
1567 // Calculate the rounding error from the URECIP instruction
1568 // E = mulhu(ABS_RCP_LO, RCP)
1569 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
1571 // RCP_A_E = RCP + E
1572 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
1574 // RCP_S_E = RCP - E
1575 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
1577 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
1578 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
1581 // Quotient = mulhu(Tmp0, Num)
1582 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
1584 // Num_S_Remainder = Quotient * Den
1585 SDValue Num_S_Remainder = DAG.getNode(ISD::MUL, DL, VT, Quotient, Den);
1587 // Remainder = Num - Num_S_Remainder
1588 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
1590 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
1591 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
1592 DAG.getConstant(-1, DL, VT),
1593 DAG.getConstant(0, DL, VT),
1595 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
1596 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
1598 DAG.getConstant(-1, DL, VT),
1599 DAG.getConstant(0, DL, VT),
1601 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
1602 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
1605 // Calculate Division result:
1607 // Quotient_A_One = Quotient + 1
1608 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
1609 DAG.getConstant(1, DL, VT));
1611 // Quotient_S_One = Quotient - 1
1612 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
1613 DAG.getConstant(1, DL, VT));
1615 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
1616 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
1617 Quotient, Quotient_A_One, ISD::SETEQ);
1619 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
1620 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
1621 Quotient_S_One, Div, ISD::SETEQ);
1623 // Calculate Rem result:
1625 // Remainder_S_Den = Remainder - Den
1626 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
1628 // Remainder_A_Den = Remainder + Den
1629 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
1631 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
1632 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
1633 Remainder, Remainder_S_Den, ISD::SETEQ);
1635 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
1636 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
1637 Remainder_A_Den, Rem, ISD::SETEQ);
1642 return DAG.getMergeValues(Ops, DL);
1645 SDValue AMDGPUTargetLowering::LowerSDIVREM(SDValue Op,
1646 SelectionDAG &DAG) const {
1648 EVT VT = Op.getValueType();
1650 SDValue LHS = Op.getOperand(0);
1651 SDValue RHS = Op.getOperand(1);
1653 SDValue Zero = DAG.getConstant(0, DL, VT);
1654 SDValue NegOne = DAG.getConstant(-1, DL, VT);
1656 if (VT == MVT::i32) {
1657 if (SDValue Res = LowerDIVREM24(Op, DAG, true))
1661 if (VT == MVT::i64 &&
1662 DAG.ComputeNumSignBits(LHS) > 32 &&
1663 DAG.ComputeNumSignBits(RHS) > 32) {
1664 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1667 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero);
1668 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero);
1669 SDValue DIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1672 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(0)),
1673 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(1))
1675 return DAG.getMergeValues(Res, DL);
1678 SDValue LHSign = DAG.getSelectCC(DL, LHS, Zero, NegOne, Zero, ISD::SETLT);
1679 SDValue RHSign = DAG.getSelectCC(DL, RHS, Zero, NegOne, Zero, ISD::SETLT);
1680 SDValue DSign = DAG.getNode(ISD::XOR, DL, VT, LHSign, RHSign);
1681 SDValue RSign = LHSign; // Remainder sign is the same as LHS
1683 LHS = DAG.getNode(ISD::ADD, DL, VT, LHS, LHSign);
1684 RHS = DAG.getNode(ISD::ADD, DL, VT, RHS, RHSign);
1686 LHS = DAG.getNode(ISD::XOR, DL, VT, LHS, LHSign);
1687 RHS = DAG.getNode(ISD::XOR, DL, VT, RHS, RHSign);
1689 SDValue Div = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT), LHS, RHS);
1690 SDValue Rem = Div.getValue(1);
1692 Div = DAG.getNode(ISD::XOR, DL, VT, Div, DSign);
1693 Rem = DAG.getNode(ISD::XOR, DL, VT, Rem, RSign);
1695 Div = DAG.getNode(ISD::SUB, DL, VT, Div, DSign);
1696 Rem = DAG.getNode(ISD::SUB, DL, VT, Rem, RSign);
1702 return DAG.getMergeValues(Res, DL);
1705 // (frem x, y) -> (fsub x, (fmul (ftrunc (fdiv x, y)), y))
1706 SDValue AMDGPUTargetLowering::LowerFREM(SDValue Op, SelectionDAG &DAG) const {
1708 EVT VT = Op.getValueType();
1709 SDValue X = Op.getOperand(0);
1710 SDValue Y = Op.getOperand(1);
1712 // TODO: Should this propagate fast-math-flags?
1714 SDValue Div = DAG.getNode(ISD::FDIV, SL, VT, X, Y);
1715 SDValue Floor = DAG.getNode(ISD::FTRUNC, SL, VT, Div);
1716 SDValue Mul = DAG.getNode(ISD::FMUL, SL, VT, Floor, Y);
1718 return DAG.getNode(ISD::FSUB, SL, VT, X, Mul);
1721 SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const {
1723 SDValue Src = Op.getOperand(0);
1725 // result = trunc(src)
1726 // if (src > 0.0 && src != result)
1729 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1731 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1732 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
1735 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
1737 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT);
1738 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1739 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1741 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero);
1742 // TODO: Should this propagate fast-math-flags?
1743 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1746 static SDValue extractF64Exponent(SDValue Hi, const SDLoc &SL,
1747 SelectionDAG &DAG) {
1748 const unsigned FractBits = 52;
1749 const unsigned ExpBits = 11;
1751 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32,
1753 DAG.getConstant(FractBits - 32, SL, MVT::i32),
1754 DAG.getConstant(ExpBits, SL, MVT::i32));
1755 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart,
1756 DAG.getConstant(1023, SL, MVT::i32));
1761 SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const {
1763 SDValue Src = Op.getOperand(0);
1765 assert(Op.getValueType() == MVT::f64);
1767 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1768 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1770 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1772 // Extract the upper half, since this is where we will find the sign and
1774 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One);
1776 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
1778 const unsigned FractBits = 52;
1780 // Extract the sign bit.
1781 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, SL, MVT::i32);
1782 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask);
1784 // Extend back to to 64-bits.
1785 SDValue SignBit64 = DAG.getBuildVector(MVT::v2i32, SL, {Zero, SignBit});
1786 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64);
1788 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src);
1789 const SDValue FractMask
1790 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, SL, MVT::i64);
1792 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp);
1793 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64);
1794 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not);
1797 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
1799 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, SL, MVT::i32);
1801 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1802 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1804 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0);
1805 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1);
1807 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2);
1810 SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const {
1812 SDValue Src = Op.getOperand(0);
1814 assert(Op.getValueType() == MVT::f64);
1816 APFloat C1Val(APFloat::IEEEdouble(), "0x1.0p+52");
1817 SDValue C1 = DAG.getConstantFP(C1Val, SL, MVT::f64);
1818 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src);
1820 // TODO: Should this propagate fast-math-flags?
1822 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign);
1823 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign);
1825 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src);
1827 APFloat C2Val(APFloat::IEEEdouble(), "0x1.fffffffffffffp+51");
1828 SDValue C2 = DAG.getConstantFP(C2Val, SL, MVT::f64);
1831 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
1832 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT);
1834 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2);
1837 SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const {
1838 // FNEARBYINT and FRINT are the same, except in their handling of FP
1839 // exceptions. Those aren't really meaningful for us, and OpenCL only has
1840 // rint, so just treat them as equivalent.
1841 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0));
1844 // XXX - May require not supporting f32 denormals?
1846 // Don't handle v2f16. The extra instructions to scalarize and repack around the
1847 // compare and vselect end up producing worse code than scalarizing the whole
1849 SDValue AMDGPUTargetLowering::LowerFROUND32_16(SDValue Op, SelectionDAG &DAG) const {
1851 SDValue X = Op.getOperand(0);
1852 EVT VT = Op.getValueType();
1854 SDValue T = DAG.getNode(ISD::FTRUNC, SL, VT, X);
1856 // TODO: Should this propagate fast-math-flags?
1858 SDValue Diff = DAG.getNode(ISD::FSUB, SL, VT, X, T);
1860 SDValue AbsDiff = DAG.getNode(ISD::FABS, SL, VT, Diff);
1862 const SDValue Zero = DAG.getConstantFP(0.0, SL, VT);
1863 const SDValue One = DAG.getConstantFP(1.0, SL, VT);
1864 const SDValue Half = DAG.getConstantFP(0.5, SL, VT);
1866 SDValue SignOne = DAG.getNode(ISD::FCOPYSIGN, SL, VT, One, X);
1869 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT);
1871 SDValue Cmp = DAG.getSetCC(SL, SetCCVT, AbsDiff, Half, ISD::SETOGE);
1873 SDValue Sel = DAG.getNode(ISD::SELECT, SL, VT, Cmp, SignOne, Zero);
1875 return DAG.getNode(ISD::FADD, SL, VT, T, Sel);
1878 SDValue AMDGPUTargetLowering::LowerFROUND64(SDValue Op, SelectionDAG &DAG) const {
1880 SDValue X = Op.getOperand(0);
1882 SDValue L = DAG.getNode(ISD::BITCAST, SL, MVT::i64, X);
1884 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1885 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1886 const SDValue NegOne = DAG.getConstant(-1, SL, MVT::i32);
1887 const SDValue FiftyOne = DAG.getConstant(51, SL, MVT::i32);
1889 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i32);
1891 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
1893 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, One);
1895 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
1897 const SDValue Mask = DAG.getConstant(INT64_C(0x000fffffffffffff), SL,
1900 SDValue M = DAG.getNode(ISD::SRA, SL, MVT::i64, Mask, Exp);
1901 SDValue D = DAG.getNode(ISD::SRA, SL, MVT::i64,
1902 DAG.getConstant(INT64_C(0x0008000000000000), SL,
1906 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, L, M);
1907 SDValue Tmp1 = DAG.getSetCC(SL, SetCCVT,
1908 DAG.getConstant(0, SL, MVT::i64), Tmp0,
1911 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, Tmp1,
1912 D, DAG.getConstant(0, SL, MVT::i64));
1913 SDValue K = DAG.getNode(ISD::ADD, SL, MVT::i64, L, Tmp2);
1915 K = DAG.getNode(ISD::AND, SL, MVT::i64, K, DAG.getNOT(SL, M, MVT::i64));
1916 K = DAG.getNode(ISD::BITCAST, SL, MVT::f64, K);
1918 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1919 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1920 SDValue ExpEqNegOne = DAG.getSetCC(SL, SetCCVT, NegOne, Exp, ISD::SETEQ);
1922 SDValue Mag = DAG.getNode(ISD::SELECT, SL, MVT::f64,
1924 DAG.getConstantFP(1.0, SL, MVT::f64),
1925 DAG.getConstantFP(0.0, SL, MVT::f64));
1927 SDValue S = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, Mag, X);
1929 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpLt0, S, K);
1930 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpGt51, X, K);
1935 SDValue AMDGPUTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const {
1936 EVT VT = Op.getValueType();
1938 if (VT == MVT::f32 || VT == MVT::f16)
1939 return LowerFROUND32_16(Op, DAG);
1942 return LowerFROUND64(Op, DAG);
1944 llvm_unreachable("unhandled type");
1947 SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const {
1949 SDValue Src = Op.getOperand(0);
1951 // result = trunc(src);
1952 // if (src < 0.0 && src != result)
1955 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1957 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1958 const SDValue NegOne = DAG.getConstantFP(-1.0, SL, MVT::f64);
1961 getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::f64);
1963 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT);
1964 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1965 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1967 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero);
1968 // TODO: Should this propagate fast-math-flags?
1969 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1972 SDValue AMDGPUTargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) const {
1974 SDValue Src = Op.getOperand(0);
1975 bool ZeroUndef = Op.getOpcode() == ISD::CTLZ_ZERO_UNDEF;
1977 if (ZeroUndef && Src.getValueType() == MVT::i32)
1978 return DAG.getNode(AMDGPUISD::FFBH_U32, SL, MVT::i32, Src);
1980 SDValue Vec = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1982 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1983 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1985 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, Zero);
1986 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, Vec, One);
1988 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
1989 *DAG.getContext(), MVT::i32);
1991 SDValue Hi0 = DAG.getSetCC(SL, SetCCVT, Hi, Zero, ISD::SETEQ);
1993 SDValue CtlzLo = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Lo);
1994 SDValue CtlzHi = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i32, Hi);
1996 const SDValue Bits32 = DAG.getConstant(32, SL, MVT::i32);
1997 SDValue Add = DAG.getNode(ISD::ADD, SL, MVT::i32, CtlzLo, Bits32);
1999 // ctlz(x) = hi_32(x) == 0 ? ctlz(lo_32(x)) + 32 : ctlz(hi_32(x))
2000 SDValue NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32, Hi0, Add, CtlzHi);
2003 // Test if the full 64-bit input is zero.
2005 // FIXME: DAG combines turn what should be an s_and_b64 into a v_or_b32,
2006 // which we probably don't want.
2007 SDValue Lo0 = DAG.getSetCC(SL, SetCCVT, Lo, Zero, ISD::SETEQ);
2008 SDValue SrcIsZero = DAG.getNode(ISD::AND, SL, SetCCVT, Lo0, Hi0);
2010 // TODO: If i64 setcc is half rate, it can result in 1 fewer instruction
2011 // with the same cycles, otherwise it is slower.
2012 // SDValue SrcIsZero = DAG.getSetCC(SL, SetCCVT, Src,
2013 // DAG.getConstant(0, SL, MVT::i64), ISD::SETEQ);
2015 const SDValue Bits32 = DAG.getConstant(64, SL, MVT::i32);
2017 // The instruction returns -1 for 0 input, but the defined intrinsic
2018 // behavior is to return the number of bits.
2019 NewCtlz = DAG.getNode(ISD::SELECT, SL, MVT::i32,
2020 SrcIsZero, Bits32, NewCtlz);
2023 return DAG.getNode(ISD::ZERO_EXTEND, SL, MVT::i64, NewCtlz);
2026 SDValue AMDGPUTargetLowering::LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG,
2027 bool Signed) const {
2031 // uint lz = clz(u);
2032 // uint e = (u != 0) ? 127U + 63U - lz : 0;
2033 // u = (u << lz) & 0x7fffffffffffffffUL;
2034 // ulong t = u & 0xffffffffffUL;
2035 // uint v = (e << 23) | (uint)(u >> 40);
2036 // uint r = t > 0x8000000000UL ? 1U : (t == 0x8000000000UL ? v & 1U : 0U);
2037 // return as_float(v + r);
2042 // long s = l >> 63;
2043 // float r = cul2f((l + s) ^ s);
2044 // return s ? -r : r;
2048 SDValue Src = Op.getOperand(0);
2053 const SDValue SignBit = DAG.getConstant(63, SL, MVT::i64);
2054 S = DAG.getNode(ISD::SRA, SL, MVT::i64, L, SignBit);
2056 SDValue LPlusS = DAG.getNode(ISD::ADD, SL, MVT::i64, L, S);
2057 L = DAG.getNode(ISD::XOR, SL, MVT::i64, LPlusS, S);
2060 EVT SetCCVT = getSetCCResultType(DAG.getDataLayout(),
2061 *DAG.getContext(), MVT::f32);
2064 SDValue ZeroI32 = DAG.getConstant(0, SL, MVT::i32);
2065 SDValue ZeroI64 = DAG.getConstant(0, SL, MVT::i64);
2066 SDValue LZ = DAG.getNode(ISD::CTLZ_ZERO_UNDEF, SL, MVT::i64, L);
2067 LZ = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LZ);
2069 SDValue K = DAG.getConstant(127U + 63U, SL, MVT::i32);
2070 SDValue E = DAG.getSelect(SL, MVT::i32,
2071 DAG.getSetCC(SL, SetCCVT, L, ZeroI64, ISD::SETNE),
2072 DAG.getNode(ISD::SUB, SL, MVT::i32, K, LZ),
2075 SDValue U = DAG.getNode(ISD::AND, SL, MVT::i64,
2076 DAG.getNode(ISD::SHL, SL, MVT::i64, L, LZ),
2077 DAG.getConstant((-1ULL) >> 1, SL, MVT::i64));
2079 SDValue T = DAG.getNode(ISD::AND, SL, MVT::i64, U,
2080 DAG.getConstant(0xffffffffffULL, SL, MVT::i64));
2082 SDValue UShl = DAG.getNode(ISD::SRL, SL, MVT::i64,
2083 U, DAG.getConstant(40, SL, MVT::i64));
2085 SDValue V = DAG.getNode(ISD::OR, SL, MVT::i32,
2086 DAG.getNode(ISD::SHL, SL, MVT::i32, E, DAG.getConstant(23, SL, MVT::i32)),
2087 DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, UShl));
2089 SDValue C = DAG.getConstant(0x8000000000ULL, SL, MVT::i64);
2090 SDValue RCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETUGT);
2091 SDValue TCmp = DAG.getSetCC(SL, SetCCVT, T, C, ISD::SETEQ);
2093 SDValue One = DAG.getConstant(1, SL, MVT::i32);
2095 SDValue VTrunc1 = DAG.getNode(ISD::AND, SL, MVT::i32, V, One);
2097 SDValue R = DAG.getSelect(SL, MVT::i32,
2100 DAG.getSelect(SL, MVT::i32, TCmp, VTrunc1, ZeroI32));
2101 R = DAG.getNode(ISD::ADD, SL, MVT::i32, V, R);
2102 R = DAG.getNode(ISD::BITCAST, SL, MVT::f32, R);
2107 SDValue RNeg = DAG.getNode(ISD::FNEG, SL, MVT::f32, R);
2108 return DAG.getSelect(SL, MVT::f32, DAG.getSExtOrTrunc(S, SL, SetCCVT), RNeg, R);
2111 SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG,
2112 bool Signed) const {
2114 SDValue Src = Op.getOperand(0);
2116 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
2118 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
2119 DAG.getConstant(0, SL, MVT::i32));
2120 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
2121 DAG.getConstant(1, SL, MVT::i32));
2123 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
2126 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo);
2128 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi,
2129 DAG.getConstant(32, SL, MVT::i32));
2130 // TODO: Should this propagate fast-math-flags?
2131 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo);
2134 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
2135 SelectionDAG &DAG) const {
2136 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
2137 "operation should be legal");
2139 // TODO: Factor out code common with LowerSINT_TO_FP.
2141 EVT DestVT = Op.getValueType();
2142 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
2144 SDValue Src = Op.getOperand(0);
2146 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
2147 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
2149 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
2154 if (DestVT == MVT::f32)
2155 return LowerINT_TO_FP32(Op, DAG, false);
2157 assert(DestVT == MVT::f64);
2158 return LowerINT_TO_FP64(Op, DAG, false);
2161 SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op,
2162 SelectionDAG &DAG) const {
2163 assert(Op.getOperand(0).getValueType() == MVT::i64 &&
2164 "operation should be legal");
2166 // TODO: Factor out code common with LowerUINT_TO_FP.
2168 EVT DestVT = Op.getValueType();
2169 if (Subtarget->has16BitInsts() && DestVT == MVT::f16) {
2171 SDValue Src = Op.getOperand(0);
2173 SDValue IntToFp32 = DAG.getNode(Op.getOpcode(), DL, MVT::f32, Src);
2174 SDValue FPRoundFlag = DAG.getIntPtrConstant(0, SDLoc(Op));
2176 DAG.getNode(ISD::FP_ROUND, DL, MVT::f16, IntToFp32, FPRoundFlag);
2181 if (DestVT == MVT::f32)
2182 return LowerINT_TO_FP32(Op, DAG, true);
2184 assert(DestVT == MVT::f64);
2185 return LowerINT_TO_FP64(Op, DAG, true);
2188 SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG,
2189 bool Signed) const {
2192 SDValue Src = Op.getOperand(0);
2194 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
2196 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL,
2198 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL,
2200 // TODO: Should this propagate fast-math-flags?
2201 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0);
2203 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul);
2206 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc);
2208 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL,
2209 MVT::i32, FloorMul);
2210 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma);
2212 SDValue Result = DAG.getBuildVector(MVT::v2i32, SL, {Lo, Hi});
2214 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result);
2217 SDValue AMDGPUTargetLowering::LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const {
2219 SDValue N0 = Op.getOperand(0);
2221 // Convert to target node to get known bits
2222 if (N0.getValueType() == MVT::f32)
2223 return DAG.getNode(AMDGPUISD::FP_TO_FP16, DL, Op.getValueType(), N0);
2225 if (getTargetMachine().Options.UnsafeFPMath) {
2226 // There is a generic expand for FP_TO_FP16 with unsafe fast math.
2230 assert(N0.getSimpleValueType() == MVT::f64);
2232 // f64 -> f16 conversion using round-to-nearest-even rounding mode.
2233 const unsigned ExpMask = 0x7ff;
2234 const unsigned ExpBiasf64 = 1023;
2235 const unsigned ExpBiasf16 = 15;
2236 SDValue Zero = DAG.getConstant(0, DL, MVT::i32);
2237 SDValue One = DAG.getConstant(1, DL, MVT::i32);
2238 SDValue U = DAG.getNode(ISD::BITCAST, DL, MVT::i64, N0);
2239 SDValue UH = DAG.getNode(ISD::SRL, DL, MVT::i64, U,
2240 DAG.getConstant(32, DL, MVT::i64));
2241 UH = DAG.getZExtOrTrunc(UH, DL, MVT::i32);
2242 U = DAG.getZExtOrTrunc(U, DL, MVT::i32);
2243 SDValue E = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2244 DAG.getConstant(20, DL, MVT::i64));
2245 E = DAG.getNode(ISD::AND, DL, MVT::i32, E,
2246 DAG.getConstant(ExpMask, DL, MVT::i32));
2247 // Subtract the fp64 exponent bias (1023) to get the real exponent and
2248 // add the f16 bias (15) to get the biased exponent for the f16 format.
2249 E = DAG.getNode(ISD::ADD, DL, MVT::i32, E,
2250 DAG.getConstant(-ExpBiasf64 + ExpBiasf16, DL, MVT::i32));
2252 SDValue M = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2253 DAG.getConstant(8, DL, MVT::i32));
2254 M = DAG.getNode(ISD::AND, DL, MVT::i32, M,
2255 DAG.getConstant(0xffe, DL, MVT::i32));
2257 SDValue MaskedSig = DAG.getNode(ISD::AND, DL, MVT::i32, UH,
2258 DAG.getConstant(0x1ff, DL, MVT::i32));
2259 MaskedSig = DAG.getNode(ISD::OR, DL, MVT::i32, MaskedSig, U);
2261 SDValue Lo40Set = DAG.getSelectCC(DL, MaskedSig, Zero, Zero, One, ISD::SETEQ);
2262 M = DAG.getNode(ISD::OR, DL, MVT::i32, M, Lo40Set);
2264 // (M != 0 ? 0x0200 : 0) | 0x7c00;
2265 SDValue I = DAG.getNode(ISD::OR, DL, MVT::i32,
2266 DAG.getSelectCC(DL, M, Zero, DAG.getConstant(0x0200, DL, MVT::i32),
2267 Zero, ISD::SETNE), DAG.getConstant(0x7c00, DL, MVT::i32));
2269 // N = M | (E << 12);
2270 SDValue N = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2271 DAG.getNode(ISD::SHL, DL, MVT::i32, E,
2272 DAG.getConstant(12, DL, MVT::i32)));
2274 // B = clamp(1-E, 0, 13);
2275 SDValue OneSubExp = DAG.getNode(ISD::SUB, DL, MVT::i32,
2277 SDValue B = DAG.getNode(ISD::SMAX, DL, MVT::i32, OneSubExp, Zero);
2278 B = DAG.getNode(ISD::SMIN, DL, MVT::i32, B,
2279 DAG.getConstant(13, DL, MVT::i32));
2281 SDValue SigSetHigh = DAG.getNode(ISD::OR, DL, MVT::i32, M,
2282 DAG.getConstant(0x1000, DL, MVT::i32));
2284 SDValue D = DAG.getNode(ISD::SRL, DL, MVT::i32, SigSetHigh, B);
2285 SDValue D0 = DAG.getNode(ISD::SHL, DL, MVT::i32, D, B);
2286 SDValue D1 = DAG.getSelectCC(DL, D0, SigSetHigh, One, Zero, ISD::SETNE);
2287 D = DAG.getNode(ISD::OR, DL, MVT::i32, D, D1);
2289 SDValue V = DAG.getSelectCC(DL, E, One, D, N, ISD::SETLT);
2290 SDValue VLow3 = DAG.getNode(ISD::AND, DL, MVT::i32, V,
2291 DAG.getConstant(0x7, DL, MVT::i32));
2292 V = DAG.getNode(ISD::SRL, DL, MVT::i32, V,
2293 DAG.getConstant(2, DL, MVT::i32));
2294 SDValue V0 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(3, DL, MVT::i32),
2295 One, Zero, ISD::SETEQ);
2296 SDValue V1 = DAG.getSelectCC(DL, VLow3, DAG.getConstant(5, DL, MVT::i32),
2297 One, Zero, ISD::SETGT);
2298 V1 = DAG.getNode(ISD::OR, DL, MVT::i32, V0, V1);
2299 V = DAG.getNode(ISD::ADD, DL, MVT::i32, V, V1);
2301 V = DAG.getSelectCC(DL, E, DAG.getConstant(30, DL, MVT::i32),
2302 DAG.getConstant(0x7c00, DL, MVT::i32), V, ISD::SETGT);
2303 V = DAG.getSelectCC(DL, E, DAG.getConstant(1039, DL, MVT::i32),
2306 // Extract the sign bit.
2307 SDValue Sign = DAG.getNode(ISD::SRL, DL, MVT::i32, UH,
2308 DAG.getConstant(16, DL, MVT::i32));
2309 Sign = DAG.getNode(ISD::AND, DL, MVT::i32, Sign,
2310 DAG.getConstant(0x8000, DL, MVT::i32));
2312 V = DAG.getNode(ISD::OR, DL, MVT::i32, Sign, V);
2313 return DAG.getZExtOrTrunc(V, DL, Op.getValueType());
2316 SDValue AMDGPUTargetLowering::LowerFP_TO_SINT(SDValue Op,
2317 SelectionDAG &DAG) const {
2318 SDValue Src = Op.getOperand(0);
2320 // TODO: Factor out code common with LowerFP_TO_UINT.
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, true);
2339 SDValue AMDGPUTargetLowering::LowerFP_TO_UINT(SDValue Op,
2340 SelectionDAG &DAG) const {
2341 SDValue Src = Op.getOperand(0);
2343 // TODO: Factor out code common with LowerFP_TO_SINT.
2345 EVT SrcVT = Src.getValueType();
2346 if (Subtarget->has16BitInsts() && SrcVT == MVT::f16) {
2349 SDValue FPExtend = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f32, Src);
2351 DAG.getNode(Op.getOpcode(), DL, MVT::i64, FPExtend);
2356 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2357 return LowerFP64_TO_INT(Op, DAG, false);
2362 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
2363 SelectionDAG &DAG) const {
2364 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2365 MVT VT = Op.getSimpleValueType();
2366 MVT ScalarVT = VT.getScalarType();
2368 assert(VT.isVector());
2370 SDValue Src = Op.getOperand(0);
2373 // TODO: Don't scalarize on Evergreen?
2374 unsigned NElts = VT.getVectorNumElements();
2375 SmallVector<SDValue, 8> Args;
2376 DAG.ExtractVectorElements(Src, Args, 0, NElts);
2378 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType());
2379 for (unsigned I = 0; I < NElts; ++I)
2380 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp);
2382 return DAG.getBuildVector(VT, DL, Args);
2385 //===----------------------------------------------------------------------===//
2386 // Custom DAG optimizations
2387 //===----------------------------------------------------------------------===//
2389 static bool isU24(SDValue Op, SelectionDAG &DAG) {
2391 EVT VT = Op.getValueType();
2392 DAG.computeKnownBits(Op, Known);
2394 return (VT.getSizeInBits() - Known.countMinLeadingZeros()) <= 24;
2397 static bool isI24(SDValue Op, SelectionDAG &DAG) {
2398 EVT VT = Op.getValueType();
2400 // In order for this to be a signed 24-bit value, bit 23, must
2402 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
2403 // as unsigned 24-bit values.
2404 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24;
2407 static bool simplifyI24(SDNode *Node24, unsigned OpIdx,
2408 TargetLowering::DAGCombinerInfo &DCI) {
2410 SelectionDAG &DAG = DCI.DAG;
2411 SDValue Op = Node24->getOperand(OpIdx);
2412 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2413 EVT VT = Op.getValueType();
2415 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24);
2416 APInt KnownZero, KnownOne;
2417 TargetLowering::TargetLoweringOpt TLO(DAG, true, true);
2418 if (TLI.SimplifyDemandedBits(Node24, OpIdx, Demanded, DCI, TLO))
2424 template <typename IntTy>
2425 static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0, uint32_t Offset,
2426 uint32_t Width, const SDLoc &DL) {
2427 if (Width + Offset < 32) {
2428 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width);
2429 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width);
2430 return DAG.getConstant(Result, DL, MVT::i32);
2433 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32);
2436 static bool hasVolatileUser(SDNode *Val) {
2437 for (SDNode *U : Val->uses()) {
2438 if (MemSDNode *M = dyn_cast<MemSDNode>(U)) {
2439 if (M->isVolatile())
2447 bool AMDGPUTargetLowering::shouldCombineMemoryType(EVT VT) const {
2448 // i32 vectors are the canonical memory type.
2449 if (VT.getScalarType() == MVT::i32 || isTypeLegal(VT))
2452 if (!VT.isByteSized())
2455 unsigned Size = VT.getStoreSize();
2457 if ((Size == 1 || Size == 2 || Size == 4) && !VT.isVector())
2460 if (Size == 3 || (Size > 4 && (Size % 4 != 0)))
2466 // Replace load of an illegal type with a store of a bitcast to a friendlier
2468 SDValue AMDGPUTargetLowering::performLoadCombine(SDNode *N,
2469 DAGCombinerInfo &DCI) const {
2470 if (!DCI.isBeforeLegalize())
2473 LoadSDNode *LN = cast<LoadSDNode>(N);
2474 if (LN->isVolatile() || !ISD::isNormalLoad(LN) || hasVolatileUser(LN))
2478 SelectionDAG &DAG = DCI.DAG;
2479 EVT VT = LN->getMemoryVT();
2481 unsigned Size = VT.getStoreSize();
2482 unsigned Align = LN->getAlignment();
2483 if (Align < Size && isTypeLegal(VT)) {
2485 unsigned AS = LN->getAddressSpace();
2487 // Expand unaligned loads earlier than legalization. Due to visitation order
2488 // problems during legalization, the emitted instructions to pack and unpack
2489 // the bytes again are not eliminated in the case of an unaligned copy.
2490 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
2492 return scalarizeVectorLoad(LN, DAG);
2495 std::tie(Ops[0], Ops[1]) = expandUnalignedLoad(LN, DAG);
2496 return DAG.getMergeValues(Ops, SDLoc(N));
2503 if (!shouldCombineMemoryType(VT))
2506 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
2509 = DAG.getLoad(NewVT, SL, LN->getChain(),
2510 LN->getBasePtr(), LN->getMemOperand());
2512 SDValue BC = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad);
2513 DCI.CombineTo(N, BC, NewLoad.getValue(1));
2514 return SDValue(N, 0);
2517 // Replace store of an illegal type with a store of a bitcast to a friendlier
2519 SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N,
2520 DAGCombinerInfo &DCI) const {
2521 if (!DCI.isBeforeLegalize())
2524 StoreSDNode *SN = cast<StoreSDNode>(N);
2525 if (SN->isVolatile() || !ISD::isNormalStore(SN))
2528 EVT VT = SN->getMemoryVT();
2529 unsigned Size = VT.getStoreSize();
2532 SelectionDAG &DAG = DCI.DAG;
2533 unsigned Align = SN->getAlignment();
2534 if (Align < Size && isTypeLegal(VT)) {
2536 unsigned AS = SN->getAddressSpace();
2538 // Expand unaligned stores earlier than legalization. Due to visitation
2539 // order problems during legalization, the emitted instructions to pack and
2540 // unpack the bytes again are not eliminated in the case of an unaligned
2542 if (!allowsMisalignedMemoryAccesses(VT, AS, Align, &IsFast)) {
2544 return scalarizeVectorStore(SN, DAG);
2546 return expandUnalignedStore(SN, DAG);
2553 if (!shouldCombineMemoryType(VT))
2556 EVT NewVT = getEquivalentMemType(*DAG.getContext(), VT);
2557 SDValue Val = SN->getValue();
2559 //DCI.AddToWorklist(Val.getNode());
2561 bool OtherUses = !Val.hasOneUse();
2562 SDValue CastVal = DAG.getNode(ISD::BITCAST, SL, NewVT, Val);
2564 SDValue CastBack = DAG.getNode(ISD::BITCAST, SL, VT, CastVal);
2565 DAG.ReplaceAllUsesOfValueWith(Val, CastBack);
2568 return DAG.getStore(SN->getChain(), SL, CastVal,
2569 SN->getBasePtr(), SN->getMemOperand());
2572 SDValue AMDGPUTargetLowering::performClampCombine(SDNode *N,
2573 DAGCombinerInfo &DCI) const {
2574 ConstantFPSDNode *CSrc = dyn_cast<ConstantFPSDNode>(N->getOperand(0));
2578 const APFloat &F = CSrc->getValueAPF();
2579 APFloat Zero = APFloat::getZero(F.getSemantics());
2580 APFloat::cmpResult Cmp0 = F.compare(Zero);
2581 if (Cmp0 == APFloat::cmpLessThan ||
2582 (Cmp0 == APFloat::cmpUnordered && Subtarget->enableDX10Clamp())) {
2583 return DCI.DAG.getConstantFP(Zero, SDLoc(N), N->getValueType(0));
2586 APFloat One(F.getSemantics(), "1.0");
2587 APFloat::cmpResult Cmp1 = F.compare(One);
2588 if (Cmp1 == APFloat::cmpGreaterThan)
2589 return DCI.DAG.getConstantFP(One, SDLoc(N), N->getValueType(0));
2591 return SDValue(CSrc, 0);
2594 /// Split the 64-bit value \p LHS into two 32-bit components, and perform the
2595 /// binary operation \p Opc to it with the corresponding constant operands.
2596 SDValue AMDGPUTargetLowering::splitBinaryBitConstantOpImpl(
2597 DAGCombinerInfo &DCI, const SDLoc &SL,
2598 unsigned Opc, SDValue LHS,
2599 uint32_t ValLo, uint32_t ValHi) const {
2600 SelectionDAG &DAG = DCI.DAG;
2602 std::tie(Lo, Hi) = split64BitValue(LHS, DAG);
2604 SDValue LoRHS = DAG.getConstant(ValLo, SL, MVT::i32);
2605 SDValue HiRHS = DAG.getConstant(ValHi, SL, MVT::i32);
2607 SDValue LoAnd = DAG.getNode(Opc, SL, MVT::i32, Lo, LoRHS);
2608 SDValue HiAnd = DAG.getNode(Opc, SL, MVT::i32, Hi, HiRHS);
2610 // Re-visit the ands. It's possible we eliminated one of them and it could
2611 // simplify the vector.
2612 DCI.AddToWorklist(Lo.getNode());
2613 DCI.AddToWorklist(Hi.getNode());
2615 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {LoAnd, HiAnd});
2616 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2619 SDValue AMDGPUTargetLowering::performShlCombine(SDNode *N,
2620 DAGCombinerInfo &DCI) const {
2621 EVT VT = N->getValueType(0);
2623 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2627 SDValue LHS = N->getOperand(0);
2628 unsigned RHSVal = RHS->getZExtValue();
2633 SelectionDAG &DAG = DCI.DAG;
2635 switch (LHS->getOpcode()) {
2638 case ISD::ZERO_EXTEND:
2639 case ISD::SIGN_EXTEND:
2640 case ISD::ANY_EXTEND: {
2641 // shl (ext x) => zext (shl x), if shift does not overflow int
2645 SDValue X = LHS->getOperand(0);
2646 DAG.computeKnownBits(X, Known);
2647 unsigned LZ = Known.countMinLeadingZeros();
2650 EVT XVT = X.getValueType();
2651 SDValue Shl = DAG.getNode(ISD::SHL, SL, XVT, X, SDValue(RHS, 0));
2652 return DAG.getZExtOrTrunc(Shl, SL, VT);
2654 case ISD::OR: if (!isOrEquivalentToAdd(DAG, LHS)) break;
2655 case ISD::ADD: { // Fall through from above
2656 // shl (or|add x, c2), c1 => or|add (shl x, c1), (c2 << c1)
2657 if (ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(LHS->getOperand(1))) {
2658 SDValue Shl = DAG.getNode(ISD::SHL, SL, VT, LHS->getOperand(0),
2660 SDValue C2V = DAG.getConstant(C2->getAPIntValue() << RHSVal,
2662 return DAG.getNode(LHS->getOpcode(), SL, VT, Shl, C2V);
2671 // i64 (shl x, C) -> (build_pair 0, (shl x, C -32))
2673 // On some subtargets, 64-bit shift is a quarter rate instruction. In the
2674 // common case, splitting this into a move and a 32-bit shift is faster and
2675 // the same code size.
2679 SDValue ShiftAmt = DAG.getConstant(RHSVal - 32, SL, MVT::i32);
2681 SDValue Lo = DAG.getNode(ISD::TRUNCATE, SL, MVT::i32, LHS);
2682 SDValue NewShift = DAG.getNode(ISD::SHL, SL, MVT::i32, Lo, ShiftAmt);
2684 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
2686 SDValue Vec = DAG.getBuildVector(MVT::v2i32, SL, {Zero, NewShift});
2687 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Vec);
2690 SDValue AMDGPUTargetLowering::performSraCombine(SDNode *N,
2691 DAGCombinerInfo &DCI) const {
2692 if (N->getValueType(0) != MVT::i64)
2695 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2699 SelectionDAG &DAG = DCI.DAG;
2701 unsigned RHSVal = RHS->getZExtValue();
2703 // (sra i64:x, 32) -> build_pair x, (sra hi_32(x), 31)
2705 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2706 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2707 DAG.getConstant(31, SL, MVT::i32));
2709 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {Hi, NewShift});
2710 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2713 // (sra i64:x, 63) -> build_pair (sra hi_32(x), 31), (sra hi_32(x), 31)
2715 SDValue Hi = getHiHalf64(N->getOperand(0), DAG);
2716 SDValue NewShift = DAG.getNode(ISD::SRA, SL, MVT::i32, Hi,
2717 DAG.getConstant(31, SL, MVT::i32));
2718 SDValue BuildVec = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, NewShift});
2719 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildVec);
2725 SDValue AMDGPUTargetLowering::performSrlCombine(SDNode *N,
2726 DAGCombinerInfo &DCI) const {
2727 if (N->getValueType(0) != MVT::i64)
2730 const ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N->getOperand(1));
2734 unsigned ShiftAmt = RHS->getZExtValue();
2738 // srl i64:x, C for C >= 32
2740 // build_pair (srl hi_32(x), C - 32), 0
2742 SelectionDAG &DAG = DCI.DAG;
2745 SDValue One = DAG.getConstant(1, SL, MVT::i32);
2746 SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
2748 SDValue VecOp = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, N->getOperand(0));
2749 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32,
2752 SDValue NewConst = DAG.getConstant(ShiftAmt - 32, SL, MVT::i32);
2753 SDValue NewShift = DAG.getNode(ISD::SRL, SL, MVT::i32, Hi, NewConst);
2755 SDValue BuildPair = DAG.getBuildVector(MVT::v2i32, SL, {NewShift, Zero});
2757 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, BuildPair);
2760 // We need to specifically handle i64 mul here to avoid unnecessary conversion
2761 // instructions. If we only match on the legalized i64 mul expansion,
2762 // SimplifyDemandedBits will be unable to remove them because there will be
2763 // multiple uses due to the separate mul + mulh[su].
2764 static SDValue getMul24(SelectionDAG &DAG, const SDLoc &SL,
2765 SDValue N0, SDValue N1, unsigned Size, bool Signed) {
2767 unsigned MulOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2768 return DAG.getNode(MulOpc, SL, MVT::i32, N0, N1);
2771 // Because we want to eliminate extension instructions before the
2772 // operation, we need to create a single user here (i.e. not the separate
2773 // mul_lo + mul_hi) so that SimplifyDemandedBits will deal with it.
2775 unsigned MulOpc = Signed ? AMDGPUISD::MUL_LOHI_I24 : AMDGPUISD::MUL_LOHI_U24;
2777 SDValue Mul = DAG.getNode(MulOpc, SL,
2778 DAG.getVTList(MVT::i32, MVT::i32), N0, N1);
2780 return DAG.getNode(ISD::BUILD_PAIR, SL, MVT::i64,
2781 Mul.getValue(0), Mul.getValue(1));
2784 SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N,
2785 DAGCombinerInfo &DCI) const {
2786 EVT VT = N->getValueType(0);
2788 unsigned Size = VT.getSizeInBits();
2789 if (VT.isVector() || Size > 64)
2792 // There are i16 integer mul/mad.
2793 if (Subtarget->has16BitInsts() && VT.getScalarType().bitsLE(MVT::i16))
2796 SelectionDAG &DAG = DCI.DAG;
2799 SDValue N0 = N->getOperand(0);
2800 SDValue N1 = N->getOperand(1);
2803 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) {
2804 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2805 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
2806 Mul = getMul24(DAG, DL, N0, N1, Size, false);
2807 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) {
2808 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2809 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
2810 Mul = getMul24(DAG, DL, N0, N1, Size, true);
2815 // We need to use sext even for MUL_U24, because MUL_U24 is used
2816 // for signed multiply of 8 and 16-bit types.
2817 return DAG.getSExtOrTrunc(Mul, DL, VT);
2820 SDValue AMDGPUTargetLowering::performMulhsCombine(SDNode *N,
2821 DAGCombinerInfo &DCI) const {
2822 EVT VT = N->getValueType(0);
2824 if (!Subtarget->hasMulI24() || VT.isVector())
2827 SelectionDAG &DAG = DCI.DAG;
2830 SDValue N0 = N->getOperand(0);
2831 SDValue N1 = N->getOperand(1);
2833 if (!isI24(N0, DAG) || !isI24(N1, DAG))
2836 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2837 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
2839 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_I24, DL, MVT::i32, N0, N1);
2840 DCI.AddToWorklist(Mulhi.getNode());
2841 return DAG.getSExtOrTrunc(Mulhi, DL, VT);
2844 SDValue AMDGPUTargetLowering::performMulhuCombine(SDNode *N,
2845 DAGCombinerInfo &DCI) const {
2846 EVT VT = N->getValueType(0);
2848 if (!Subtarget->hasMulU24() || VT.isVector() || VT.getSizeInBits() > 32)
2851 SelectionDAG &DAG = DCI.DAG;
2854 SDValue N0 = N->getOperand(0);
2855 SDValue N1 = N->getOperand(1);
2857 if (!isU24(N0, DAG) || !isU24(N1, DAG))
2860 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2861 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
2863 SDValue Mulhi = DAG.getNode(AMDGPUISD::MULHI_U24, DL, MVT::i32, N0, N1);
2864 DCI.AddToWorklist(Mulhi.getNode());
2865 return DAG.getZExtOrTrunc(Mulhi, DL, VT);
2868 SDValue AMDGPUTargetLowering::performMulLoHi24Combine(
2869 SDNode *N, DAGCombinerInfo &DCI) const {
2870 SelectionDAG &DAG = DCI.DAG;
2872 // Simplify demanded bits before splitting into multiple users.
2873 if (simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI))
2876 SDValue N0 = N->getOperand(0);
2877 SDValue N1 = N->getOperand(1);
2879 bool Signed = (N->getOpcode() == AMDGPUISD::MUL_LOHI_I24);
2881 unsigned MulLoOpc = Signed ? AMDGPUISD::MUL_I24 : AMDGPUISD::MUL_U24;
2882 unsigned MulHiOpc = Signed ? AMDGPUISD::MULHI_I24 : AMDGPUISD::MULHI_U24;
2886 SDValue MulLo = DAG.getNode(MulLoOpc, SL, MVT::i32, N0, N1);
2887 SDValue MulHi = DAG.getNode(MulHiOpc, SL, MVT::i32, N0, N1);
2888 return DAG.getMergeValues({ MulLo, MulHi }, SL);
2891 static bool isNegativeOne(SDValue Val) {
2892 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Val))
2893 return C->isAllOnesValue();
2897 static bool isCtlzOpc(unsigned Opc) {
2898 return Opc == ISD::CTLZ || Opc == ISD::CTLZ_ZERO_UNDEF;
2901 SDValue AMDGPUTargetLowering::getFFBH_U32(SelectionDAG &DAG,
2903 const SDLoc &DL) const {
2904 EVT VT = Op.getValueType();
2905 EVT LegalVT = getTypeToTransformTo(*DAG.getContext(), VT);
2906 if (LegalVT != MVT::i32 && (Subtarget->has16BitInsts() &&
2907 LegalVT != MVT::i16))
2911 Op = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Op);
2913 SDValue FFBH = DAG.getNode(AMDGPUISD::FFBH_U32, DL, MVT::i32, Op);
2915 FFBH = DAG.getNode(ISD::TRUNCATE, DL, VT, FFBH);
2920 // The native instructions return -1 on 0 input. Optimize out a select that
2921 // produces -1 on 0.
2923 // TODO: If zero is not undef, we could also do this if the output is compared
2924 // against the bitwidth.
2926 // TODO: Should probably combine against FFBH_U32 instead of ctlz directly.
2927 SDValue AMDGPUTargetLowering::performCtlzCombine(const SDLoc &SL, SDValue Cond,
2928 SDValue LHS, SDValue RHS,
2929 DAGCombinerInfo &DCI) const {
2930 ConstantSDNode *CmpRhs = dyn_cast<ConstantSDNode>(Cond.getOperand(1));
2931 if (!CmpRhs || !CmpRhs->isNullValue())
2934 SelectionDAG &DAG = DCI.DAG;
2935 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
2936 SDValue CmpLHS = Cond.getOperand(0);
2938 // select (setcc x, 0, eq), -1, (ctlz_zero_undef x) -> ffbh_u32 x
2939 if (CCOpcode == ISD::SETEQ &&
2940 isCtlzOpc(RHS.getOpcode()) &&
2941 RHS.getOperand(0) == CmpLHS &&
2942 isNegativeOne(LHS)) {
2943 return getFFBH_U32(DAG, CmpLHS, SL);
2946 // select (setcc x, 0, ne), (ctlz_zero_undef x), -1 -> ffbh_u32 x
2947 if (CCOpcode == ISD::SETNE &&
2948 isCtlzOpc(LHS.getOpcode()) &&
2949 LHS.getOperand(0) == CmpLHS &&
2950 isNegativeOne(RHS)) {
2951 return getFFBH_U32(DAG, CmpLHS, SL);
2957 static SDValue distributeOpThroughSelect(TargetLowering::DAGCombinerInfo &DCI,
2963 SelectionDAG &DAG = DCI.DAG;
2964 EVT VT = N1.getValueType();
2966 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT, Cond,
2967 N1.getOperand(0), N2.getOperand(0));
2968 DCI.AddToWorklist(NewSelect.getNode());
2969 return DAG.getNode(Op, SL, VT, NewSelect);
2972 // Pull a free FP operation out of a select so it may fold into uses.
2974 // select c, (fneg x), (fneg y) -> fneg (select c, x, y)
2975 // select c, (fneg x), k -> fneg (select c, x, (fneg k))
2977 // select c, (fabs x), (fabs y) -> fabs (select c, x, y)
2978 // select c, (fabs x), +k -> fabs (select c, x, k)
2979 static SDValue foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI,
2981 SelectionDAG &DAG = DCI.DAG;
2982 SDValue Cond = N.getOperand(0);
2983 SDValue LHS = N.getOperand(1);
2984 SDValue RHS = N.getOperand(2);
2986 EVT VT = N.getValueType();
2987 if ((LHS.getOpcode() == ISD::FABS && RHS.getOpcode() == ISD::FABS) ||
2988 (LHS.getOpcode() == ISD::FNEG && RHS.getOpcode() == ISD::FNEG)) {
2989 return distributeOpThroughSelect(DCI, LHS.getOpcode(),
2990 SDLoc(N), Cond, LHS, RHS);
2994 if (RHS.getOpcode() == ISD::FABS || RHS.getOpcode() == ISD::FNEG) {
2995 std::swap(LHS, RHS);
2999 // TODO: Support vector constants.
3000 ConstantFPSDNode *CRHS = dyn_cast<ConstantFPSDNode>(RHS);
3001 if ((LHS.getOpcode() == ISD::FNEG || LHS.getOpcode() == ISD::FABS) && CRHS) {
3003 // If one side is an fneg/fabs and the other is a constant, we can push the
3004 // fneg/fabs down. If it's an fabs, the constant needs to be non-negative.
3005 SDValue NewLHS = LHS.getOperand(0);
3006 SDValue NewRHS = RHS;
3008 // Careful: if the neg can be folded up, don't try to pull it back down.
3009 bool ShouldFoldNeg = true;
3011 if (NewLHS.hasOneUse()) {
3012 unsigned Opc = NewLHS.getOpcode();
3013 if (LHS.getOpcode() == ISD::FNEG && fnegFoldsIntoOp(Opc))
3014 ShouldFoldNeg = false;
3015 if (LHS.getOpcode() == ISD::FABS && Opc == ISD::FMUL)
3016 ShouldFoldNeg = false;
3019 if (ShouldFoldNeg) {
3020 if (LHS.getOpcode() == ISD::FNEG)
3021 NewRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3022 else if (CRHS->isNegative())
3026 std::swap(NewLHS, NewRHS);
3028 SDValue NewSelect = DAG.getNode(ISD::SELECT, SL, VT,
3029 Cond, NewLHS, NewRHS);
3030 DCI.AddToWorklist(NewSelect.getNode());
3031 return DAG.getNode(LHS.getOpcode(), SL, VT, NewSelect);
3039 SDValue AMDGPUTargetLowering::performSelectCombine(SDNode *N,
3040 DAGCombinerInfo &DCI) const {
3041 if (SDValue Folded = foldFreeOpFromSelect(DCI, SDValue(N, 0)))
3044 SDValue Cond = N->getOperand(0);
3045 if (Cond.getOpcode() != ISD::SETCC)
3048 EVT VT = N->getValueType(0);
3049 SDValue LHS = Cond.getOperand(0);
3050 SDValue RHS = Cond.getOperand(1);
3051 SDValue CC = Cond.getOperand(2);
3053 SDValue True = N->getOperand(1);
3054 SDValue False = N->getOperand(2);
3056 if (Cond.hasOneUse()) { // TODO: Look for multiple select uses.
3057 SelectionDAG &DAG = DCI.DAG;
3058 if ((DAG.isConstantValueOfAnyType(True) ||
3059 DAG.isConstantValueOfAnyType(True)) &&
3060 (!DAG.isConstantValueOfAnyType(False) &&
3061 !DAG.isConstantValueOfAnyType(False))) {
3062 // Swap cmp + select pair to move constant to false input.
3063 // This will allow using VOPC cndmasks more often.
3064 // select (setcc x, y), k, x -> select (setcc y, x) x, x
3067 ISD::CondCode NewCC = getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
3068 LHS.getValueType().isInteger());
3070 SDValue NewCond = DAG.getSetCC(SL, Cond.getValueType(), LHS, RHS, NewCC);
3071 return DAG.getNode(ISD::SELECT, SL, VT, NewCond, False, True);
3074 if (VT == MVT::f32 && Subtarget->hasFminFmaxLegacy()) {
3076 = combineFMinMaxLegacy(SDLoc(N), VT, LHS, RHS, True, False, CC, DCI);
3077 // Revisit this node so we can catch min3/max3/med3 patterns.
3078 //DCI.AddToWorklist(MinMax.getNode());
3083 // There's no reason to not do this if the condition has other uses.
3084 return performCtlzCombine(SDLoc(N), Cond, True, False, DCI);
3087 static bool isConstantFPZero(SDValue N) {
3088 if (const ConstantFPSDNode *C = isConstOrConstSplatFP(N))
3089 return C->isZero() && !C->isNegative();
3093 static unsigned inverseMinMax(unsigned Opc) {
3096 return ISD::FMINNUM;
3098 return ISD::FMAXNUM;
3099 case AMDGPUISD::FMAX_LEGACY:
3100 return AMDGPUISD::FMIN_LEGACY;
3101 case AMDGPUISD::FMIN_LEGACY:
3102 return AMDGPUISD::FMAX_LEGACY;
3104 llvm_unreachable("invalid min/max opcode");
3108 SDValue AMDGPUTargetLowering::performFNegCombine(SDNode *N,
3109 DAGCombinerInfo &DCI) const {
3110 SelectionDAG &DAG = DCI.DAG;
3111 SDValue N0 = N->getOperand(0);
3112 EVT VT = N->getValueType(0);
3114 unsigned Opc = N0.getOpcode();
3116 // If the input has multiple uses and we can either fold the negate down, or
3117 // the other uses cannot, give up. This both prevents unprofitable
3118 // transformations and infinite loops: we won't repeatedly try to fold around
3119 // a negate that has no 'good' form.
3120 if (N0.hasOneUse()) {
3121 // This may be able to fold into the source, but at a code size cost. Don't
3122 // fold if the fold into the user is free.
3123 if (allUsesHaveSourceMods(N, 0))
3126 if (fnegFoldsIntoOp(Opc) &&
3127 (allUsesHaveSourceMods(N) || !allUsesHaveSourceMods(N0.getNode())))
3134 if (!mayIgnoreSignedZero(N0))
3137 // (fneg (fadd x, y)) -> (fadd (fneg x), (fneg y))
3138 SDValue LHS = N0.getOperand(0);
3139 SDValue RHS = N0.getOperand(1);
3141 if (LHS.getOpcode() != ISD::FNEG)
3142 LHS = DAG.getNode(ISD::FNEG, SL, VT, LHS);
3144 LHS = LHS.getOperand(0);
3146 if (RHS.getOpcode() != ISD::FNEG)
3147 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3149 RHS = RHS.getOperand(0);
3151 SDValue Res = DAG.getNode(ISD::FADD, SL, VT, LHS, RHS, N0->getFlags());
3152 if (!N0.hasOneUse())
3153 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3157 case AMDGPUISD::FMUL_LEGACY: {
3158 // (fneg (fmul x, y)) -> (fmul x, (fneg y))
3159 // (fneg (fmul_legacy x, y)) -> (fmul_legacy x, (fneg y))
3160 SDValue LHS = N0.getOperand(0);
3161 SDValue RHS = N0.getOperand(1);
3163 if (LHS.getOpcode() == ISD::FNEG)
3164 LHS = LHS.getOperand(0);
3165 else if (RHS.getOpcode() == ISD::FNEG)
3166 RHS = RHS.getOperand(0);
3168 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3170 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, RHS, N0->getFlags());
3171 if (!N0.hasOneUse())
3172 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3177 if (!mayIgnoreSignedZero(N0))
3180 // (fneg (fma x, y, z)) -> (fma x, (fneg y), (fneg z))
3181 SDValue LHS = N0.getOperand(0);
3182 SDValue MHS = N0.getOperand(1);
3183 SDValue RHS = N0.getOperand(2);
3185 if (LHS.getOpcode() == ISD::FNEG)
3186 LHS = LHS.getOperand(0);
3187 else if (MHS.getOpcode() == ISD::FNEG)
3188 MHS = MHS.getOperand(0);
3190 MHS = DAG.getNode(ISD::FNEG, SL, VT, MHS);
3192 if (RHS.getOpcode() != ISD::FNEG)
3193 RHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3195 RHS = RHS.getOperand(0);
3197 SDValue Res = DAG.getNode(Opc, SL, VT, LHS, MHS, RHS);
3198 if (!N0.hasOneUse())
3199 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3204 case AMDGPUISD::FMAX_LEGACY:
3205 case AMDGPUISD::FMIN_LEGACY: {
3206 // fneg (fmaxnum x, y) -> fminnum (fneg x), (fneg y)
3207 // fneg (fminnum x, y) -> fmaxnum (fneg x), (fneg y)
3208 // fneg (fmax_legacy x, y) -> fmin_legacy (fneg x), (fneg y)
3209 // fneg (fmin_legacy x, y) -> fmax_legacy (fneg x), (fneg y)
3211 SDValue LHS = N0.getOperand(0);
3212 SDValue RHS = N0.getOperand(1);
3214 // 0 doesn't have a negated inline immediate.
3215 // TODO: Shouldn't fold 1/2pi either, and should be generalized to other
3217 if (isConstantFPZero(RHS))
3220 SDValue NegLHS = DAG.getNode(ISD::FNEG, SL, VT, LHS);
3221 SDValue NegRHS = DAG.getNode(ISD::FNEG, SL, VT, RHS);
3222 unsigned Opposite = inverseMinMax(Opc);
3224 SDValue Res = DAG.getNode(Opposite, SL, VT, NegLHS, NegRHS, N0->getFlags());
3225 if (!N0.hasOneUse())
3226 DAG.ReplaceAllUsesWith(N0, DAG.getNode(ISD::FNEG, SL, VT, Res));
3229 case ISD::FP_EXTEND:
3232 case ISD::FNEARBYINT: // XXX - Should fround be handled?
3234 case AMDGPUISD::RCP:
3235 case AMDGPUISD::RCP_LEGACY:
3236 case AMDGPUISD::SIN_HW: {
3237 SDValue CvtSrc = N0.getOperand(0);
3238 if (CvtSrc.getOpcode() == ISD::FNEG) {
3239 // (fneg (fp_extend (fneg x))) -> (fp_extend x)
3240 // (fneg (rcp (fneg x))) -> (rcp x)
3241 return DAG.getNode(Opc, SL, VT, CvtSrc.getOperand(0));
3244 if (!N0.hasOneUse())
3247 // (fneg (fp_extend x)) -> (fp_extend (fneg x))
3248 // (fneg (rcp x)) -> (rcp (fneg x))
3249 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
3250 return DAG.getNode(Opc, SL, VT, Neg, N0->getFlags());
3252 case ISD::FP_ROUND: {
3253 SDValue CvtSrc = N0.getOperand(0);
3255 if (CvtSrc.getOpcode() == ISD::FNEG) {
3256 // (fneg (fp_round (fneg x))) -> (fp_round x)
3257 return DAG.getNode(ISD::FP_ROUND, SL, VT,
3258 CvtSrc.getOperand(0), N0.getOperand(1));
3261 if (!N0.hasOneUse())
3264 // (fneg (fp_round x)) -> (fp_round (fneg x))
3265 SDValue Neg = DAG.getNode(ISD::FNEG, SL, CvtSrc.getValueType(), CvtSrc);
3266 return DAG.getNode(ISD::FP_ROUND, SL, VT, Neg, N0.getOperand(1));
3268 case ISD::FP16_TO_FP: {
3269 // v_cvt_f32_f16 supports source modifiers on pre-VI targets without legal
3270 // f16, but legalization of f16 fneg ends up pulling it out of the source.
3271 // Put the fneg back as a legal source operation that can be matched later.
3274 SDValue Src = N0.getOperand(0);
3275 EVT SrcVT = Src.getValueType();
3277 // fneg (fp16_to_fp x) -> fp16_to_fp (xor x, 0x8000)
3278 SDValue IntFNeg = DAG.getNode(ISD::XOR, SL, SrcVT, Src,
3279 DAG.getConstant(0x8000, SL, SrcVT));
3280 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFNeg);
3287 SDValue AMDGPUTargetLowering::performFAbsCombine(SDNode *N,
3288 DAGCombinerInfo &DCI) const {
3289 SelectionDAG &DAG = DCI.DAG;
3290 SDValue N0 = N->getOperand(0);
3292 if (!N0.hasOneUse())
3295 switch (N0.getOpcode()) {
3296 case ISD::FP16_TO_FP: {
3297 assert(!Subtarget->has16BitInsts() && "should only see if f16 is illegal");
3299 SDValue Src = N0.getOperand(0);
3300 EVT SrcVT = Src.getValueType();
3302 // fabs (fp16_to_fp x) -> fp16_to_fp (and x, 0x7fff)
3303 SDValue IntFAbs = DAG.getNode(ISD::AND, SL, SrcVT, Src,
3304 DAG.getConstant(0x7fff, SL, SrcVT));
3305 return DAG.getNode(ISD::FP16_TO_FP, SL, N->getValueType(0), IntFAbs);
3312 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N,
3313 DAGCombinerInfo &DCI) const {
3314 SelectionDAG &DAG = DCI.DAG;
3317 switch(N->getOpcode()) {
3320 case ISD::BITCAST: {
3321 EVT DestVT = N->getValueType(0);
3323 // Push casts through vector builds. This helps avoid emitting a large
3324 // number of copies when materializing floating point vector constants.
3326 // vNt1 bitcast (vNt0 (build_vector t0:x, t0:y)) =>
3327 // vnt1 = build_vector (t1 (bitcast t0:x)), (t1 (bitcast t0:y))
3328 if (DestVT.isVector()) {
3329 SDValue Src = N->getOperand(0);
3330 if (Src.getOpcode() == ISD::BUILD_VECTOR) {
3331 EVT SrcVT = Src.getValueType();
3332 unsigned NElts = DestVT.getVectorNumElements();
3334 if (SrcVT.getVectorNumElements() == NElts) {
3335 EVT DestEltVT = DestVT.getVectorElementType();
3337 SmallVector<SDValue, 8> CastedElts;
3339 for (unsigned I = 0, E = SrcVT.getVectorNumElements(); I != E; ++I) {
3340 SDValue Elt = Src.getOperand(I);
3341 CastedElts.push_back(DAG.getNode(ISD::BITCAST, DL, DestEltVT, Elt));
3344 return DAG.getBuildVector(DestVT, SL, CastedElts);
3349 if (DestVT.getSizeInBits() != 64 && !DestVT.isVector())
3352 // Fold bitcasts of constants.
3354 // v2i32 (bitcast i64:k) -> build_vector lo_32(k), hi_32(k)
3355 // TODO: Generalize and move to DAGCombiner
3356 SDValue Src = N->getOperand(0);
3357 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src)) {
3358 assert(Src.getValueType() == MVT::i64);
3360 uint64_t CVal = C->getZExtValue();
3361 return DAG.getNode(ISD::BUILD_VECTOR, SL, DestVT,
3362 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3363 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3366 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Src)) {
3367 const APInt &Val = C->getValueAPF().bitcastToAPInt();
3369 uint64_t CVal = Val.getZExtValue();
3370 SDValue Vec = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
3371 DAG.getConstant(Lo_32(CVal), SL, MVT::i32),
3372 DAG.getConstant(Hi_32(CVal), SL, MVT::i32));
3374 return DAG.getNode(ISD::BITCAST, SL, DestVT, Vec);
3380 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3383 return performShlCombine(N, DCI);
3386 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3389 return performSrlCombine(N, DCI);
3392 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG)
3395 return performSraCombine(N, DCI);
3398 return performMulCombine(N, DCI);
3400 return performMulhsCombine(N, DCI);
3402 return performMulhuCombine(N, DCI);
3403 case AMDGPUISD::MUL_I24:
3404 case AMDGPUISD::MUL_U24:
3405 case AMDGPUISD::MULHI_I24:
3406 case AMDGPUISD::MULHI_U24: {
3407 // If the first call to simplify is successfull, then N may end up being
3408 // deleted, so we shouldn't call simplifyI24 again.
3409 simplifyI24(N, 0, DCI) || simplifyI24(N, 1, DCI);
3412 case AMDGPUISD::MUL_LOHI_I24:
3413 case AMDGPUISD::MUL_LOHI_U24:
3414 return performMulLoHi24Combine(N, DCI);
3416 return performSelectCombine(N, DCI);
3418 return performFNegCombine(N, DCI);
3420 return performFAbsCombine(N, DCI);
3421 case AMDGPUISD::BFE_I32:
3422 case AMDGPUISD::BFE_U32: {
3423 assert(!N->getValueType(0).isVector() &&
3424 "Vector handling of BFE not implemented");
3425 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
3429 uint32_t WidthVal = Width->getZExtValue() & 0x1f;
3431 return DAG.getConstant(0, DL, MVT::i32);
3433 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
3437 SDValue BitsFrom = N->getOperand(0);
3438 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f;
3440 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32;
3442 if (OffsetVal == 0) {
3443 // This is already sign / zero extended, so try to fold away extra BFEs.
3444 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal);
3446 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom);
3447 if (OpSignBits >= SignBits)
3450 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal);
3452 // This is a sign_extend_inreg. Replace it to take advantage of existing
3453 // DAG Combines. If not eliminated, we will match back to BFE during
3456 // TODO: The sext_inreg of extended types ends, although we can could
3457 // handle them in a single BFE.
3458 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom,
3459 DAG.getValueType(SmallVT));
3462 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT);
3465 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) {
3467 return constantFoldBFE<int32_t>(DAG,
3468 CVal->getSExtValue(),
3474 return constantFoldBFE<uint32_t>(DAG,
3475 CVal->getZExtValue(),
3481 if ((OffsetVal + WidthVal) >= 32 &&
3482 !(Subtarget->hasSDWA() && OffsetVal == 16 && WidthVal == 16)) {
3483 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32);
3484 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32,
3485 BitsFrom, ShiftVal);
3488 if (BitsFrom.hasOneUse()) {
3489 APInt Demanded = APInt::getBitsSet(32,
3491 OffsetVal + WidthVal);
3494 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
3495 !DCI.isBeforeLegalizeOps());
3496 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
3497 if (TLI.ShrinkDemandedConstant(BitsFrom, Demanded, TLO) ||
3498 TLI.SimplifyDemandedBits(BitsFrom, Demanded, Known, TLO)) {
3499 DCI.CommitTargetLoweringOpt(TLO);
3506 return performLoadCombine(N, DCI);
3508 return performStoreCombine(N, DCI);
3509 case AMDGPUISD::CLAMP:
3510 return performClampCombine(N, DCI);
3511 case AMDGPUISD::RCP: {
3512 if (const auto *CFP = dyn_cast<ConstantFPSDNode>(N->getOperand(0))) {
3513 // XXX - Should this flush denormals?
3514 const APFloat &Val = CFP->getValueAPF();
3515 APFloat One(Val.getSemantics(), "1.0");
3516 return DAG.getConstantFP(One / Val, SDLoc(N), N->getValueType(0));
3525 //===----------------------------------------------------------------------===//
3527 //===----------------------------------------------------------------------===//
3529 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
3530 const TargetRegisterClass *RC,
3531 unsigned Reg, EVT VT,
3533 bool RawReg) const {
3534 MachineFunction &MF = DAG.getMachineFunction();
3535 MachineRegisterInfo &MRI = MF.getRegInfo();
3538 if (!MRI.isLiveIn(Reg)) {
3539 VReg = MRI.createVirtualRegister(RC);
3540 MRI.addLiveIn(Reg, VReg);
3542 VReg = MRI.getLiveInVirtReg(Reg);
3546 return DAG.getRegister(VReg, VT);
3548 return DAG.getCopyFromReg(DAG.getEntryNode(), SL, VReg, VT);
3551 uint32_t AMDGPUTargetLowering::getImplicitParameterOffset(
3552 const AMDGPUMachineFunction *MFI, const ImplicitParameter Param) const {
3553 unsigned Alignment = Subtarget->getAlignmentForImplicitArgPtr();
3554 uint64_t ArgOffset = alignTo(MFI->getABIArgOffset(), Alignment);
3559 return ArgOffset + 4;
3561 llvm_unreachable("unexpected implicit parameter type");
3564 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
3566 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
3567 switch ((AMDGPUISD::NodeType)Opcode) {
3568 case AMDGPUISD::FIRST_NUMBER: break;
3570 NODE_NAME_CASE(UMUL);
3571 NODE_NAME_CASE(BRANCH_COND);
3575 NODE_NAME_CASE(ELSE)
3576 NODE_NAME_CASE(LOOP)
3577 NODE_NAME_CASE(CALL)
3578 NODE_NAME_CASE(TRAP)
3579 NODE_NAME_CASE(RET_FLAG)
3580 NODE_NAME_CASE(RETURN_TO_EPILOG)
3581 NODE_NAME_CASE(ENDPGM)
3582 NODE_NAME_CASE(DWORDADDR)
3583 NODE_NAME_CASE(FRACT)
3584 NODE_NAME_CASE(SETCC)
3585 NODE_NAME_CASE(SETREG)
3586 NODE_NAME_CASE(FMA_W_CHAIN)
3587 NODE_NAME_CASE(FMUL_W_CHAIN)
3588 NODE_NAME_CASE(CLAMP)
3589 NODE_NAME_CASE(COS_HW)
3590 NODE_NAME_CASE(SIN_HW)
3591 NODE_NAME_CASE(FMAX_LEGACY)
3592 NODE_NAME_CASE(FMIN_LEGACY)
3593 NODE_NAME_CASE(FMAX3)
3594 NODE_NAME_CASE(SMAX3)
3595 NODE_NAME_CASE(UMAX3)
3596 NODE_NAME_CASE(FMIN3)
3597 NODE_NAME_CASE(SMIN3)
3598 NODE_NAME_CASE(UMIN3)
3599 NODE_NAME_CASE(FMED3)
3600 NODE_NAME_CASE(SMED3)
3601 NODE_NAME_CASE(UMED3)
3602 NODE_NAME_CASE(URECIP)
3603 NODE_NAME_CASE(DIV_SCALE)
3604 NODE_NAME_CASE(DIV_FMAS)
3605 NODE_NAME_CASE(DIV_FIXUP)
3606 NODE_NAME_CASE(FMAD_FTZ)
3607 NODE_NAME_CASE(TRIG_PREOP)
3610 NODE_NAME_CASE(RCP_LEGACY)
3611 NODE_NAME_CASE(RSQ_LEGACY)
3612 NODE_NAME_CASE(FMUL_LEGACY)
3613 NODE_NAME_CASE(RSQ_CLAMP)
3614 NODE_NAME_CASE(LDEXP)
3615 NODE_NAME_CASE(FP_CLASS)
3616 NODE_NAME_CASE(DOT4)
3617 NODE_NAME_CASE(CARRY)
3618 NODE_NAME_CASE(BORROW)
3619 NODE_NAME_CASE(BFE_U32)
3620 NODE_NAME_CASE(BFE_I32)
3623 NODE_NAME_CASE(FFBH_U32)
3624 NODE_NAME_CASE(FFBH_I32)
3625 NODE_NAME_CASE(MUL_U24)
3626 NODE_NAME_CASE(MUL_I24)
3627 NODE_NAME_CASE(MULHI_U24)
3628 NODE_NAME_CASE(MULHI_I24)
3629 NODE_NAME_CASE(MUL_LOHI_U24)
3630 NODE_NAME_CASE(MUL_LOHI_I24)
3631 NODE_NAME_CASE(MAD_U24)
3632 NODE_NAME_CASE(MAD_I24)
3633 NODE_NAME_CASE(TEXTURE_FETCH)
3634 NODE_NAME_CASE(EXPORT)
3635 NODE_NAME_CASE(EXPORT_DONE)
3636 NODE_NAME_CASE(R600_EXPORT)
3637 NODE_NAME_CASE(CONST_ADDRESS)
3638 NODE_NAME_CASE(REGISTER_LOAD)
3639 NODE_NAME_CASE(REGISTER_STORE)
3640 NODE_NAME_CASE(SAMPLE)
3641 NODE_NAME_CASE(SAMPLEB)
3642 NODE_NAME_CASE(SAMPLED)
3643 NODE_NAME_CASE(SAMPLEL)
3644 NODE_NAME_CASE(CVT_F32_UBYTE0)
3645 NODE_NAME_CASE(CVT_F32_UBYTE1)
3646 NODE_NAME_CASE(CVT_F32_UBYTE2)
3647 NODE_NAME_CASE(CVT_F32_UBYTE3)
3648 NODE_NAME_CASE(CVT_PKRTZ_F16_F32)
3649 NODE_NAME_CASE(FP_TO_FP16)
3650 NODE_NAME_CASE(FP16_ZEXT)
3651 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR)
3652 NODE_NAME_CASE(CONST_DATA_PTR)
3653 NODE_NAME_CASE(PC_ADD_REL_OFFSET)
3654 NODE_NAME_CASE(KILL)
3655 NODE_NAME_CASE(DUMMY_CHAIN)
3656 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break;
3657 NODE_NAME_CASE(INIT_EXEC)
3658 NODE_NAME_CASE(INIT_EXEC_FROM_INPUT)
3659 NODE_NAME_CASE(SENDMSG)
3660 NODE_NAME_CASE(SENDMSGHALT)
3661 NODE_NAME_CASE(INTERP_MOV)
3662 NODE_NAME_CASE(INTERP_P1)
3663 NODE_NAME_CASE(INTERP_P2)
3664 NODE_NAME_CASE(STORE_MSKOR)
3665 NODE_NAME_CASE(LOAD_CONSTANT)
3666 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
3667 NODE_NAME_CASE(TBUFFER_STORE_FORMAT_X3)
3668 NODE_NAME_CASE(TBUFFER_LOAD_FORMAT)
3669 NODE_NAME_CASE(ATOMIC_CMP_SWAP)
3670 NODE_NAME_CASE(ATOMIC_INC)
3671 NODE_NAME_CASE(ATOMIC_DEC)
3672 NODE_NAME_CASE(BUFFER_LOAD)
3673 NODE_NAME_CASE(BUFFER_LOAD_FORMAT)
3674 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break;
3679 SDValue AMDGPUTargetLowering::getSqrtEstimate(SDValue Operand,
3680 SelectionDAG &DAG, int Enabled,
3681 int &RefinementSteps,
3682 bool &UseOneConstNR,
3683 bool Reciprocal) const {
3684 EVT VT = Operand.getValueType();
3686 if (VT == MVT::f32) {
3687 RefinementSteps = 0;
3688 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand);
3691 // TODO: There is also f64 rsq instruction, but the documentation is less
3692 // clear on its precision.
3697 SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand,
3698 SelectionDAG &DAG, int Enabled,
3699 int &RefinementSteps) const {
3700 EVT VT = Operand.getValueType();
3702 if (VT == MVT::f32) {
3703 // Reciprocal, < 1 ulp error.
3705 // This reciprocal approximation converges to < 0.5 ulp error with one
3706 // newton rhapson performed with two fused multiple adds (FMAs).
3708 RefinementSteps = 0;
3709 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand);
3712 // TODO: There is also f64 rcp instruction, but the documentation is less
3713 // clear on its precision.
3718 void AMDGPUTargetLowering::computeKnownBitsForTargetNode(
3719 const SDValue Op, KnownBits &Known,
3720 const APInt &DemandedElts, const SelectionDAG &DAG, unsigned Depth) const {
3722 Known.resetAll(); // Don't know anything.
3725 unsigned Opc = Op.getOpcode();
3730 case AMDGPUISD::CARRY:
3731 case AMDGPUISD::BORROW: {
3732 Known.Zero = APInt::getHighBitsSet(32, 31);
3736 case AMDGPUISD::BFE_I32:
3737 case AMDGPUISD::BFE_U32: {
3738 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3742 uint32_t Width = CWidth->getZExtValue() & 0x1f;
3744 if (Opc == AMDGPUISD::BFE_U32)
3745 Known.Zero = APInt::getHighBitsSet(32, 32 - Width);
3749 case AMDGPUISD::FP_TO_FP16:
3750 case AMDGPUISD::FP16_ZEXT: {
3751 unsigned BitWidth = Known.getBitWidth();
3753 // High bits are zero.
3754 Known.Zero = APInt::getHighBitsSet(BitWidth, BitWidth - 16);
3760 unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode(
3761 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG,
3762 unsigned Depth) const {
3763 switch (Op.getOpcode()) {
3764 case AMDGPUISD::BFE_I32: {
3765 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3769 unsigned SignBits = 32 - Width->getZExtValue() + 1;
3770 if (!isNullConstant(Op.getOperand(1)))
3773 // TODO: Could probably figure something out with non-0 offsets.
3774 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1);
3775 return std::max(SignBits, Op0SignBits);
3778 case AMDGPUISD::BFE_U32: {
3779 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3780 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1;
3783 case AMDGPUISD::CARRY:
3784 case AMDGPUISD::BORROW:
3786 case AMDGPUISD::FP_TO_FP16:
3787 case AMDGPUISD::FP16_ZEXT: