1 //===-- AMDGPUISelLowering.cpp - AMDGPU Common DAG lowering functions -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This is the parent TargetLowering class for hardware code gen
14 //===----------------------------------------------------------------------===//
16 #include "AMDGPUISelLowering.h"
18 #include "AMDGPUFrameLowering.h"
19 #include "AMDGPUIntrinsicInfo.h"
20 #include "AMDGPURegisterInfo.h"
21 #include "AMDGPUSubtarget.h"
22 #include "R600MachineFunctionInfo.h"
23 #include "SIMachineFunctionInfo.h"
24 #include "llvm/CodeGen/CallingConvLower.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/SelectionDAG.h"
28 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/DiagnosticInfo.h"
31 #include "llvm/IR/DiagnosticPrinter.h"
37 /// Diagnostic information for unimplemented or unsupported feature reporting.
38 class DiagnosticInfoUnsupported : public DiagnosticInfo {
40 const Twine &Description;
45 static int getKindID() {
47 KindID = llvm::getNextAvailablePluginDiagnosticKind();
52 DiagnosticInfoUnsupported(const Function &Fn, const Twine &Desc,
53 DiagnosticSeverity Severity = DS_Error)
54 : DiagnosticInfo(getKindID(), Severity),
58 const Function &getFunction() const { return Fn; }
59 const Twine &getDescription() const { return Description; }
61 void print(DiagnosticPrinter &DP) const override {
62 DP << "unsupported " << getDescription() << " in " << Fn.getName();
65 static bool classof(const DiagnosticInfo *DI) {
66 return DI->getKind() == getKindID();
70 int DiagnosticInfoUnsupported::KindID = 0;
74 static bool allocateStack(unsigned ValNo, MVT ValVT, MVT LocVT,
75 CCValAssign::LocInfo LocInfo,
76 ISD::ArgFlagsTy ArgFlags, CCState &State) {
77 unsigned Offset = State.AllocateStack(ValVT.getStoreSize(),
78 ArgFlags.getOrigAlign());
79 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
84 #include "AMDGPUGenCallingConv.inc"
86 // Find a larger type to do a load / store of a vector with.
87 EVT AMDGPUTargetLowering::getEquivalentMemType(LLVMContext &Ctx, EVT VT) {
88 unsigned StoreSize = VT.getStoreSizeInBits();
90 return EVT::getIntegerVT(Ctx, StoreSize);
92 assert(StoreSize % 32 == 0 && "Store size not a multiple of 32");
93 return EVT::getVectorVT(Ctx, MVT::i32, StoreSize / 32);
96 // Type for a vector that will be loaded to.
97 EVT AMDGPUTargetLowering::getEquivalentLoadRegType(LLVMContext &Ctx, EVT VT) {
98 unsigned StoreSize = VT.getStoreSizeInBits();
100 return EVT::getIntegerVT(Ctx, 32);
102 return EVT::getVectorVT(Ctx, MVT::i32, StoreSize / 32);
105 AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM,
106 const AMDGPUSubtarget &STI)
107 : TargetLowering(TM), Subtarget(&STI) {
108 setOperationAction(ISD::Constant, MVT::i32, Legal);
109 setOperationAction(ISD::Constant, MVT::i64, Legal);
110 setOperationAction(ISD::ConstantFP, MVT::f32, Legal);
111 setOperationAction(ISD::ConstantFP, MVT::f64, Legal);
113 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
114 setOperationAction(ISD::BRIND, MVT::Other, Expand);
116 // We need to custom lower some of the intrinsics
117 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
119 // Library functions. These default to Expand, but we have instructions
121 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
122 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
123 setOperationAction(ISD::FPOW, MVT::f32, Legal);
124 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
125 setOperationAction(ISD::FABS, MVT::f32, Legal);
126 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
127 setOperationAction(ISD::FRINT, MVT::f32, Legal);
128 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
129 setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
130 setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
132 setOperationAction(ISD::FROUND, MVT::f32, Custom);
133 setOperationAction(ISD::FROUND, MVT::f64, Custom);
135 setOperationAction(ISD::FREM, MVT::f32, Custom);
136 setOperationAction(ISD::FREM, MVT::f64, Custom);
138 // v_mad_f32 does not support denormals according to some sources.
139 if (!Subtarget->hasFP32Denormals())
140 setOperationAction(ISD::FMAD, MVT::f32, Legal);
142 // Expand to fneg + fadd.
143 setOperationAction(ISD::FSUB, MVT::f64, Expand);
145 // Lower floating point store/load to integer store/load to reduce the number
146 // of patterns in tablegen.
147 setOperationAction(ISD::STORE, MVT::f32, Promote);
148 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
150 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
151 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
153 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
154 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
156 setOperationAction(ISD::STORE, MVT::v8f32, Promote);
157 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
159 setOperationAction(ISD::STORE, MVT::v16f32, Promote);
160 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
162 setOperationAction(ISD::STORE, MVT::f64, Promote);
163 AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64);
165 setOperationAction(ISD::STORE, MVT::v2f64, Promote);
166 AddPromotedToType(ISD::STORE, MVT::v2f64, MVT::v2i64);
168 // Custom lowering of vector stores is required for local address space
170 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
172 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
173 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
174 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
176 // XXX: This can be change to Custom, once ExpandVectorStores can
177 // handle 64-bit stores.
178 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
180 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
181 setTruncStoreAction(MVT::i64, MVT::i8, Expand);
182 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
183 setTruncStoreAction(MVT::v2i64, MVT::v2i1, Expand);
184 setTruncStoreAction(MVT::v4i64, MVT::v4i1, Expand);
187 setOperationAction(ISD::LOAD, MVT::f32, Promote);
188 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
190 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
191 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
193 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
194 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
196 setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
197 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
199 setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
200 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
202 setOperationAction(ISD::LOAD, MVT::f64, Promote);
203 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64);
205 setOperationAction(ISD::LOAD, MVT::v2f64, Promote);
206 AddPromotedToType(ISD::LOAD, MVT::v2f64, MVT::v2i64);
208 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
209 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
210 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
211 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
212 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
213 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
214 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f32, Custom);
215 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
216 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f32, Custom);
217 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i32, Custom);
219 // There are no 64-bit extloads. These should be done as a 32-bit extload and
220 // an extension to 64-bit.
221 for (MVT VT : MVT::integer_valuetypes()) {
222 setLoadExtAction(ISD::EXTLOAD, MVT::i64, VT, Expand);
223 setLoadExtAction(ISD::SEXTLOAD, MVT::i64, VT, Expand);
224 setLoadExtAction(ISD::ZEXTLOAD, MVT::i64, VT, Expand);
227 for (MVT VT : MVT::integer_vector_valuetypes()) {
228 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i8, Expand);
229 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i8, Expand);
230 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i8, Expand);
231 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i8, Expand);
232 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i8, Expand);
233 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i8, Expand);
234 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i16, Expand);
235 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i16, Expand);
236 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v2i16, Expand);
237 setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i16, Expand);
238 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i16, Expand);
239 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::v4i16, Expand);
242 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
244 if (Subtarget->getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
245 setOperationAction(ISD::FCEIL, MVT::f64, Custom);
246 setOperationAction(ISD::FTRUNC, MVT::f64, Custom);
247 setOperationAction(ISD::FRINT, MVT::f64, Custom);
248 setOperationAction(ISD::FFLOOR, MVT::f64, Custom);
251 if (!Subtarget->hasBFI()) {
252 // fcopysign can be done in a single instruction with BFI.
253 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
254 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
257 setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
259 setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
260 setLoadExtAction(ISD::EXTLOAD, MVT::v2f32, MVT::v2f16, Expand);
261 setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, MVT::v4f16, Expand);
262 setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, MVT::v8f16, Expand);
264 setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
265 setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Expand);
266 setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f16, Expand);
267 setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f16, Expand);
269 setTruncStoreAction(MVT::f32, MVT::f16, Expand);
270 setTruncStoreAction(MVT::v2f32, MVT::v2f16, Expand);
271 setTruncStoreAction(MVT::v4f32, MVT::v4f16, Expand);
272 setTruncStoreAction(MVT::v8f32, MVT::v8f16, Expand);
274 setTruncStoreAction(MVT::f64, MVT::f16, Expand);
275 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
277 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
278 for (MVT VT : ScalarIntVTs) {
279 setOperationAction(ISD::SREM, VT, Expand);
280 setOperationAction(ISD::SDIV, VT, Expand);
282 // GPU does not have divrem function for signed or unsigned.
283 setOperationAction(ISD::SDIVREM, VT, Custom);
284 setOperationAction(ISD::UDIVREM, VT, Custom);
286 // GPU does not have [S|U]MUL_LOHI functions as a single instruction.
287 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
288 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
290 setOperationAction(ISD::BSWAP, VT, Expand);
291 setOperationAction(ISD::CTTZ, VT, Expand);
292 setOperationAction(ISD::CTLZ, VT, Expand);
295 if (!Subtarget->hasBCNT(32))
296 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
298 if (!Subtarget->hasBCNT(64))
299 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
301 // The hardware supports 32-bit ROTR, but not ROTL.
302 setOperationAction(ISD::ROTL, MVT::i32, Expand);
303 setOperationAction(ISD::ROTL, MVT::i64, Expand);
304 setOperationAction(ISD::ROTR, MVT::i64, Expand);
306 setOperationAction(ISD::MUL, MVT::i64, Expand);
307 setOperationAction(ISD::MULHU, MVT::i64, Expand);
308 setOperationAction(ISD::MULHS, MVT::i64, Expand);
309 setOperationAction(ISD::UDIV, MVT::i32, Expand);
310 setOperationAction(ISD::UREM, MVT::i32, Expand);
311 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
312 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
313 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
314 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
315 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
317 if (!Subtarget->hasFFBH())
318 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
320 if (!Subtarget->hasFFBL())
321 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
323 static const MVT::SimpleValueType VectorIntTypes[] = {
324 MVT::v2i32, MVT::v4i32
327 for (MVT VT : VectorIntTypes) {
328 // Expand the following operations for the current type by default.
329 setOperationAction(ISD::ADD, VT, Expand);
330 setOperationAction(ISD::AND, VT, Expand);
331 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
332 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
333 setOperationAction(ISD::MUL, VT, Expand);
334 setOperationAction(ISD::OR, VT, Expand);
335 setOperationAction(ISD::SHL, VT, Expand);
336 setOperationAction(ISD::SRA, VT, Expand);
337 setOperationAction(ISD::SRL, VT, Expand);
338 setOperationAction(ISD::ROTL, VT, Expand);
339 setOperationAction(ISD::ROTR, VT, Expand);
340 setOperationAction(ISD::SUB, VT, Expand);
341 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
342 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
343 setOperationAction(ISD::SDIV, VT, Expand);
344 setOperationAction(ISD::UDIV, VT, Expand);
345 setOperationAction(ISD::SREM, VT, Expand);
346 setOperationAction(ISD::UREM, VT, Expand);
347 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
348 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
349 setOperationAction(ISD::SDIVREM, VT, Custom);
350 setOperationAction(ISD::UDIVREM, VT, Custom);
351 setOperationAction(ISD::ADDC, VT, Expand);
352 setOperationAction(ISD::SUBC, VT, Expand);
353 setOperationAction(ISD::ADDE, VT, Expand);
354 setOperationAction(ISD::SUBE, VT, Expand);
355 setOperationAction(ISD::SELECT, VT, Expand);
356 setOperationAction(ISD::VSELECT, VT, Expand);
357 setOperationAction(ISD::SELECT_CC, VT, Expand);
358 setOperationAction(ISD::XOR, VT, Expand);
359 setOperationAction(ISD::BSWAP, VT, Expand);
360 setOperationAction(ISD::CTPOP, VT, Expand);
361 setOperationAction(ISD::CTTZ, VT, Expand);
362 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
363 setOperationAction(ISD::CTLZ, VT, Expand);
364 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
365 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
368 static const MVT::SimpleValueType FloatVectorTypes[] = {
369 MVT::v2f32, MVT::v4f32
372 for (MVT VT : FloatVectorTypes) {
373 setOperationAction(ISD::FABS, VT, Expand);
374 setOperationAction(ISD::FMINNUM, VT, Expand);
375 setOperationAction(ISD::FMAXNUM, VT, Expand);
376 setOperationAction(ISD::FADD, VT, Expand);
377 setOperationAction(ISD::FCEIL, VT, Expand);
378 setOperationAction(ISD::FCOS, VT, Expand);
379 setOperationAction(ISD::FDIV, VT, Expand);
380 setOperationAction(ISD::FEXP2, VT, Expand);
381 setOperationAction(ISD::FLOG2, VT, Expand);
382 setOperationAction(ISD::FREM, VT, Expand);
383 setOperationAction(ISD::FPOW, VT, Expand);
384 setOperationAction(ISD::FFLOOR, VT, Expand);
385 setOperationAction(ISD::FTRUNC, VT, Expand);
386 setOperationAction(ISD::FMUL, VT, Expand);
387 setOperationAction(ISD::FMA, VT, Expand);
388 setOperationAction(ISD::FRINT, VT, Expand);
389 setOperationAction(ISD::FNEARBYINT, VT, Expand);
390 setOperationAction(ISD::FSQRT, VT, Expand);
391 setOperationAction(ISD::FSIN, VT, Expand);
392 setOperationAction(ISD::FSUB, VT, Expand);
393 setOperationAction(ISD::FNEG, VT, Expand);
394 setOperationAction(ISD::SELECT, VT, Expand);
395 setOperationAction(ISD::VSELECT, VT, Expand);
396 setOperationAction(ISD::SELECT_CC, VT, Expand);
397 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
398 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
401 setOperationAction(ISD::FNEARBYINT, MVT::f32, Custom);
402 setOperationAction(ISD::FNEARBYINT, MVT::f64, Custom);
404 setTargetDAGCombine(ISD::MUL);
405 setTargetDAGCombine(ISD::SELECT);
406 setTargetDAGCombine(ISD::SELECT_CC);
407 setTargetDAGCombine(ISD::STORE);
409 setTargetDAGCombine(ISD::FADD);
410 setTargetDAGCombine(ISD::FSUB);
412 setBooleanContents(ZeroOrNegativeOneBooleanContent);
413 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
415 setSchedulingPreference(Sched::RegPressure);
416 setJumpIsExpensive(true);
418 // SI at least has hardware support for floating point exceptions, but no way
419 // of using or handling them is implemented. They are also optional in OpenCL
421 setHasFloatingPointExceptions(false);
423 setSelectIsExpensive(false);
424 PredictableSelectIsExpensive = false;
426 // There are no integer divide instructions, and these expand to a pretty
427 // large sequence of instructions.
428 setIntDivIsCheap(false);
429 setPow2SDivIsCheap(false);
430 setFsqrtIsCheap(true);
432 // FIXME: Need to really handle these.
433 MaxStoresPerMemcpy = 4096;
434 MaxStoresPerMemmove = 4096;
435 MaxStoresPerMemset = 4096;
438 //===----------------------------------------------------------------------===//
439 // Target Information
440 //===----------------------------------------------------------------------===//
442 MVT AMDGPUTargetLowering::getVectorIdxTy() const {
446 bool AMDGPUTargetLowering::isSelectSupported(SelectSupportKind SelType) const {
450 // The backend supports 32 and 64 bit floating point immediates.
451 // FIXME: Why are we reporting vectors of FP immediates as legal?
452 bool AMDGPUTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
453 EVT ScalarVT = VT.getScalarType();
454 return (ScalarVT == MVT::f32 || ScalarVT == MVT::f64);
457 // We don't want to shrink f64 / f32 constants.
458 bool AMDGPUTargetLowering::ShouldShrinkFPConstant(EVT VT) const {
459 EVT ScalarVT = VT.getScalarType();
460 return (ScalarVT != MVT::f32 && ScalarVT != MVT::f64);
463 bool AMDGPUTargetLowering::shouldReduceLoadWidth(SDNode *N,
467 unsigned NewSize = NewVT.getStoreSizeInBits();
469 // If we are reducing to a 32-bit load, this is always better.
473 EVT OldVT = N->getValueType(0);
474 unsigned OldSize = OldVT.getStoreSizeInBits();
476 // Don't produce extloads from sub 32-bit types. SI doesn't have scalar
477 // extloads, so doing one requires using a buffer_load. In cases where we
478 // still couldn't use a scalar load, using the wider load shouldn't really
481 // If the old size already had to be an extload, there's no harm in continuing
482 // to reduce the width.
483 return (OldSize < 32);
486 bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy,
488 if (LoadTy.getSizeInBits() != CastTy.getSizeInBits())
491 unsigned LScalarSize = LoadTy.getScalarType().getSizeInBits();
492 unsigned CastScalarSize = CastTy.getScalarType().getSizeInBits();
494 return ((LScalarSize <= CastScalarSize) ||
495 (CastScalarSize >= 32) ||
499 // SI+ has instructions for cttz / ctlz for 32-bit values. This is probably also
500 // profitable with the expansion for 64-bit since it's generally good to
502 // FIXME: These should really have the size as a parameter.
503 bool AMDGPUTargetLowering::isCheapToSpeculateCttz() const {
507 bool AMDGPUTargetLowering::isCheapToSpeculateCtlz() const {
511 //===---------------------------------------------------------------------===//
513 //===---------------------------------------------------------------------===//
515 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
516 assert(VT.isFloatingPoint());
517 return VT == MVT::f32 || VT == MVT::f64;
520 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
521 assert(VT.isFloatingPoint());
522 return VT == MVT::f32 || VT == MVT::f64;
525 bool AMDGPUTargetLowering:: storeOfVectorConstantIsCheap(EVT MemVT,
531 bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const {
532 // Truncate is just accessing a subregister.
533 return Dest.bitsLT(Source) && (Dest.getSizeInBits() % 32 == 0);
536 bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const {
537 // Truncate is just accessing a subregister.
538 return Dest->getPrimitiveSizeInBits() < Source->getPrimitiveSizeInBits() &&
539 (Dest->getPrimitiveSizeInBits() % 32 == 0);
542 bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const {
543 const DataLayout *DL = getDataLayout();
544 unsigned SrcSize = DL->getTypeSizeInBits(Src->getScalarType());
545 unsigned DestSize = DL->getTypeSizeInBits(Dest->getScalarType());
547 return SrcSize == 32 && DestSize == 64;
550 bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const {
551 // Any register load of a 64-bit value really requires 2 32-bit moves. For all
552 // practical purposes, the extra mov 0 to load a 64-bit is free. As used,
553 // this will enable reducing 64-bit operations the 32-bit, which is always
555 return Src == MVT::i32 && Dest == MVT::i64;
558 bool AMDGPUTargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
559 return isZExtFree(Val.getValueType(), VT2);
562 bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const {
563 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a
564 // limited number of native 64-bit operations. Shrinking an operation to fit
565 // in a single 32-bit register should always be helpful. As currently used,
566 // this is much less general than the name suggests, and is only used in
567 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is
568 // not profitable, and may actually be harmful.
569 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32;
572 //===---------------------------------------------------------------------===//
573 // TargetLowering Callbacks
574 //===---------------------------------------------------------------------===//
576 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
577 const SmallVectorImpl<ISD::InputArg> &Ins) const {
579 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
582 SDValue AMDGPUTargetLowering::LowerReturn(
584 CallingConv::ID CallConv,
586 const SmallVectorImpl<ISD::OutputArg> &Outs,
587 const SmallVectorImpl<SDValue> &OutVals,
588 SDLoc DL, SelectionDAG &DAG) const {
589 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
592 //===---------------------------------------------------------------------===//
593 // Target specific lowering
594 //===---------------------------------------------------------------------===//
596 SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI,
597 SmallVectorImpl<SDValue> &InVals) const {
598 SDValue Callee = CLI.Callee;
599 SelectionDAG &DAG = CLI.DAG;
601 const Function &Fn = *DAG.getMachineFunction().getFunction();
603 StringRef FuncName("<unknown>");
605 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee))
606 FuncName = G->getSymbol();
607 else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
608 FuncName = G->getGlobal()->getName();
610 DiagnosticInfoUnsupported NoCalls(Fn, "call to function " + FuncName);
611 DAG.getContext()->diagnose(NoCalls);
615 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op,
616 SelectionDAG &DAG) const {
617 switch (Op.getOpcode()) {
619 Op.getNode()->dump();
620 llvm_unreachable("Custom lowering code for this"
621 "instruction is not implemented yet!");
623 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
624 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
625 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
626 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
627 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
628 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
629 case ISD::SDIVREM: return LowerSDIVREM(Op, DAG);
630 case ISD::FREM: return LowerFREM(Op, DAG);
631 case ISD::FCEIL: return LowerFCEIL(Op, DAG);
632 case ISD::FTRUNC: return LowerFTRUNC(Op, DAG);
633 case ISD::FRINT: return LowerFRINT(Op, DAG);
634 case ISD::FNEARBYINT: return LowerFNEARBYINT(Op, DAG);
635 case ISD::FROUND: return LowerFROUND(Op, DAG);
636 case ISD::FFLOOR: return LowerFFLOOR(Op, DAG);
637 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
638 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
639 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
640 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
645 void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N,
646 SmallVectorImpl<SDValue> &Results,
647 SelectionDAG &DAG) const {
648 switch (N->getOpcode()) {
649 case ISD::SIGN_EXTEND_INREG:
650 // Different parts of legalization seem to interpret which type of
651 // sign_extend_inreg is the one to check for custom lowering. The extended
652 // from type is what really matters, but some places check for custom
653 // lowering of the result type. This results in trying to use
654 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do
655 // nothing here and let the illegal result integer be handled normally.
658 SDNode *Node = LowerLOAD(SDValue(N, 0), DAG).getNode();
662 Results.push_back(SDValue(Node, 0));
663 Results.push_back(SDValue(Node, 1));
664 // XXX: LLVM seems not to replace Chain Value inside CustomWidenLowerNode
666 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), SDValue(Node, 1));
670 SDValue Lowered = LowerSTORE(SDValue(N, 0), DAG);
671 if (Lowered.getNode())
672 Results.push_back(Lowered);
680 // FIXME: This implements accesses to initialized globals in the constant
681 // address space by copying them to private and accessing that. It does not
682 // properly handle illegal types or vectors. The private vector loads are not
683 // scalarized, and the illegal scalars hit an assertion. This technique will not
684 // work well with large initializers, and this should eventually be
685 // removed. Initialized globals should be placed into a data section that the
686 // runtime will load into a buffer before the kernel is executed. Uses of the
687 // global need to be replaced with a pointer loaded from an implicit kernel
688 // argument into this buffer holding the copy of the data, which will remove the
689 // need for any of this.
690 SDValue AMDGPUTargetLowering::LowerConstantInitializer(const Constant* Init,
691 const GlobalValue *GV,
692 const SDValue &InitPtr,
694 SelectionDAG &DAG) const {
695 const DataLayout *TD = getDataLayout();
697 Type *InitTy = Init->getType();
699 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Init)) {
700 EVT VT = EVT::getEVT(InitTy);
701 PointerType *PtrTy = PointerType::get(InitTy, AMDGPUAS::PRIVATE_ADDRESS);
702 return DAG.getStore(Chain, DL, DAG.getConstant(*CI, DL, VT), InitPtr,
703 MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
704 TD->getPrefTypeAlignment(InitTy));
707 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Init)) {
708 EVT VT = EVT::getEVT(CFP->getType());
709 PointerType *PtrTy = PointerType::get(CFP->getType(), 0);
710 return DAG.getStore(Chain, DL, DAG.getConstantFP(*CFP, DL, VT), InitPtr,
711 MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
712 TD->getPrefTypeAlignment(CFP->getType()));
715 if (StructType *ST = dyn_cast<StructType>(InitTy)) {
716 const StructLayout *SL = TD->getStructLayout(ST);
718 EVT PtrVT = InitPtr.getValueType();
719 SmallVector<SDValue, 8> Chains;
721 for (unsigned I = 0, N = ST->getNumElements(); I != N; ++I) {
722 SDValue Offset = DAG.getConstant(SL->getElementOffset(I), DL, PtrVT);
723 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, InitPtr, Offset);
725 Constant *Elt = Init->getAggregateElement(I);
726 Chains.push_back(LowerConstantInitializer(Elt, GV, Ptr, Chain, DAG));
729 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
732 if (SequentialType *SeqTy = dyn_cast<SequentialType>(InitTy)) {
733 EVT PtrVT = InitPtr.getValueType();
735 unsigned NumElements;
736 if (ArrayType *AT = dyn_cast<ArrayType>(SeqTy))
737 NumElements = AT->getNumElements();
738 else if (VectorType *VT = dyn_cast<VectorType>(SeqTy))
739 NumElements = VT->getNumElements();
741 llvm_unreachable("Unexpected type");
743 unsigned EltSize = TD->getTypeAllocSize(SeqTy->getElementType());
744 SmallVector<SDValue, 8> Chains;
745 for (unsigned i = 0; i < NumElements; ++i) {
746 SDValue Offset = DAG.getConstant(i * EltSize, DL, PtrVT);
747 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, InitPtr, Offset);
749 Constant *Elt = Init->getAggregateElement(i);
750 Chains.push_back(LowerConstantInitializer(Elt, GV, Ptr, Chain, DAG));
753 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
756 if (isa<UndefValue>(Init)) {
757 EVT VT = EVT::getEVT(InitTy);
758 PointerType *PtrTy = PointerType::get(InitTy, AMDGPUAS::PRIVATE_ADDRESS);
759 return DAG.getStore(Chain, DL, DAG.getUNDEF(VT), InitPtr,
760 MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
761 TD->getPrefTypeAlignment(InitTy));
765 llvm_unreachable("Unhandled constant initializer");
768 static bool hasDefinedInitializer(const GlobalValue *GV) {
769 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV);
770 if (!GVar || !GVar->hasInitializer())
773 if (isa<UndefValue>(GVar->getInitializer()))
779 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
781 SelectionDAG &DAG) const {
783 const DataLayout *TD = getDataLayout();
784 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
785 const GlobalValue *GV = G->getGlobal();
787 switch (G->getAddressSpace()) {
788 case AMDGPUAS::LOCAL_ADDRESS: {
789 // XXX: What does the value of G->getOffset() mean?
790 assert(G->getOffset() == 0 &&
791 "Do not know what to do with an non-zero offset");
793 // TODO: We could emit code to handle the initialization somewhere.
794 if (hasDefinedInitializer(GV))
798 if (MFI->LocalMemoryObjects.count(GV) == 0) {
799 uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
800 Offset = MFI->LDSSize;
801 MFI->LocalMemoryObjects[GV] = Offset;
802 // XXX: Account for alignment?
803 MFI->LDSSize += Size;
805 Offset = MFI->LocalMemoryObjects[GV];
808 return DAG.getConstant(Offset, SDLoc(Op),
809 getPointerTy(AMDGPUAS::LOCAL_ADDRESS));
811 case AMDGPUAS::CONSTANT_ADDRESS: {
812 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
813 Type *EltType = GV->getType()->getElementType();
814 unsigned Size = TD->getTypeAllocSize(EltType);
815 unsigned Alignment = TD->getPrefTypeAlignment(EltType);
817 MVT PrivPtrVT = getPointerTy(AMDGPUAS::PRIVATE_ADDRESS);
818 MVT ConstPtrVT = getPointerTy(AMDGPUAS::CONSTANT_ADDRESS);
820 int FI = FrameInfo->CreateStackObject(Size, Alignment, false);
821 SDValue InitPtr = DAG.getFrameIndex(FI, PrivPtrVT);
823 const GlobalVariable *Var = cast<GlobalVariable>(GV);
824 if (!Var->hasInitializer()) {
825 // This has no use, but bugpoint will hit it.
826 return DAG.getZExtOrTrunc(InitPtr, SDLoc(Op), ConstPtrVT);
829 const Constant *Init = Var->getInitializer();
830 SmallVector<SDNode*, 8> WorkList;
832 for (SDNode::use_iterator I = DAG.getEntryNode()->use_begin(),
833 E = DAG.getEntryNode()->use_end(); I != E; ++I) {
834 if (I->getOpcode() != AMDGPUISD::REGISTER_LOAD && I->getOpcode() != ISD::LOAD)
836 WorkList.push_back(*I);
838 SDValue Chain = LowerConstantInitializer(Init, GV, InitPtr, DAG.getEntryNode(), DAG);
839 for (SmallVector<SDNode*, 8>::iterator I = WorkList.begin(),
840 E = WorkList.end(); I != E; ++I) {
841 SmallVector<SDValue, 8> Ops;
842 Ops.push_back(Chain);
843 for (unsigned i = 1; i < (*I)->getNumOperands(); ++i) {
844 Ops.push_back((*I)->getOperand(i));
846 DAG.UpdateNodeOperands(*I, Ops);
848 return DAG.getZExtOrTrunc(InitPtr, SDLoc(Op), ConstPtrVT);
852 const Function &Fn = *DAG.getMachineFunction().getFunction();
853 DiagnosticInfoUnsupported BadInit(Fn,
854 "initializer for address space");
855 DAG.getContext()->diagnose(BadInit);
859 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
860 SelectionDAG &DAG) const {
861 SmallVector<SDValue, 8> Args;
863 for (const SDUse &U : Op->ops())
864 DAG.ExtractVectorElements(U.get(), Args);
866 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(), Args);
869 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
870 SelectionDAG &DAG) const {
872 SmallVector<SDValue, 8> Args;
873 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
874 EVT VT = Op.getValueType();
875 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start,
876 VT.getVectorNumElements());
878 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(), Args);
881 SDValue AMDGPUTargetLowering::LowerFrameIndex(SDValue Op,
882 SelectionDAG &DAG) const {
884 MachineFunction &MF = DAG.getMachineFunction();
885 const AMDGPUFrameLowering *TFL = Subtarget->getFrameLowering();
887 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op);
889 unsigned FrameIndex = FIN->getIndex();
890 unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
891 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), SDLoc(Op),
895 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
896 SelectionDAG &DAG) const {
897 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
899 EVT VT = Op.getValueType();
901 switch (IntrinsicID) {
903 case AMDGPUIntrinsic::AMDGPU_abs:
904 case AMDGPUIntrinsic::AMDIL_abs: // Legacy name.
905 return LowerIntrinsicIABS(Op, DAG);
906 case AMDGPUIntrinsic::AMDGPU_lrp:
907 return LowerIntrinsicLRP(Op, DAG);
909 case AMDGPUIntrinsic::AMDGPU_clamp:
910 case AMDGPUIntrinsic::AMDIL_clamp: // Legacy name.
911 return DAG.getNode(AMDGPUISD::CLAMP, DL, VT,
912 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
914 case Intrinsic::AMDGPU_div_scale: {
915 // 3rd parameter required to be a constant.
916 const ConstantSDNode *Param = dyn_cast<ConstantSDNode>(Op.getOperand(3));
918 return DAG.getUNDEF(VT);
920 // Translate to the operands expected by the machine instruction. The
921 // first parameter must be the same as the first instruction.
922 SDValue Numerator = Op.getOperand(1);
923 SDValue Denominator = Op.getOperand(2);
925 // Note this order is opposite of the machine instruction's operations,
926 // which is s0.f = Quotient, s1.f = Denominator, s2.f = Numerator. The
927 // intrinsic has the numerator as the first operand to match a normal
928 // division operation.
930 SDValue Src0 = Param->isAllOnesValue() ? Numerator : Denominator;
932 return DAG.getNode(AMDGPUISD::DIV_SCALE, DL, Op->getVTList(), Src0,
933 Denominator, Numerator);
936 case Intrinsic::AMDGPU_div_fmas:
937 return DAG.getNode(AMDGPUISD::DIV_FMAS, DL, VT,
938 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3),
941 case Intrinsic::AMDGPU_div_fixup:
942 return DAG.getNode(AMDGPUISD::DIV_FIXUP, DL, VT,
943 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
945 case Intrinsic::AMDGPU_trig_preop:
946 return DAG.getNode(AMDGPUISD::TRIG_PREOP, DL, VT,
947 Op.getOperand(1), Op.getOperand(2));
949 case Intrinsic::AMDGPU_rcp:
950 return DAG.getNode(AMDGPUISD::RCP, DL, VT, Op.getOperand(1));
952 case Intrinsic::AMDGPU_rsq:
953 return DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
955 case AMDGPUIntrinsic::AMDGPU_legacy_rsq:
956 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
958 case Intrinsic::AMDGPU_rsq_clamped:
959 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) {
960 Type *Type = VT.getTypeForEVT(*DAG.getContext());
961 APFloat Max = APFloat::getLargest(Type->getFltSemantics());
962 APFloat Min = APFloat::getLargest(Type->getFltSemantics(), true);
964 SDValue Rsq = DAG.getNode(AMDGPUISD::RSQ, DL, VT, Op.getOperand(1));
965 SDValue Tmp = DAG.getNode(ISD::FMINNUM, DL, VT, Rsq,
966 DAG.getConstantFP(Max, DL, VT));
967 return DAG.getNode(ISD::FMAXNUM, DL, VT, Tmp,
968 DAG.getConstantFP(Min, DL, VT));
970 return DAG.getNode(AMDGPUISD::RSQ_CLAMPED, DL, VT, Op.getOperand(1));
973 case Intrinsic::AMDGPU_ldexp:
974 return DAG.getNode(AMDGPUISD::LDEXP, DL, VT, Op.getOperand(1),
977 case AMDGPUIntrinsic::AMDGPU_imax:
978 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
980 case AMDGPUIntrinsic::AMDGPU_umax:
981 return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
983 case AMDGPUIntrinsic::AMDGPU_imin:
984 return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
986 case AMDGPUIntrinsic::AMDGPU_umin:
987 return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
990 case AMDGPUIntrinsic::AMDGPU_umul24:
991 return DAG.getNode(AMDGPUISD::MUL_U24, DL, VT,
992 Op.getOperand(1), Op.getOperand(2));
994 case AMDGPUIntrinsic::AMDGPU_imul24:
995 return DAG.getNode(AMDGPUISD::MUL_I24, DL, VT,
996 Op.getOperand(1), Op.getOperand(2));
998 case AMDGPUIntrinsic::AMDGPU_umad24:
999 return DAG.getNode(AMDGPUISD::MAD_U24, DL, VT,
1000 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
1002 case AMDGPUIntrinsic::AMDGPU_imad24:
1003 return DAG.getNode(AMDGPUISD::MAD_I24, DL, VT,
1004 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
1006 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte0:
1007 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Op.getOperand(1));
1009 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte1:
1010 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE1, DL, VT, Op.getOperand(1));
1012 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte2:
1013 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE2, DL, VT, Op.getOperand(1));
1015 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte3:
1016 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE3, DL, VT, Op.getOperand(1));
1018 case AMDGPUIntrinsic::AMDGPU_bfe_i32:
1019 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT,
1024 case AMDGPUIntrinsic::AMDGPU_bfe_u32:
1025 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT,
1030 case AMDGPUIntrinsic::AMDGPU_bfi:
1031 return DAG.getNode(AMDGPUISD::BFI, DL, VT,
1036 case AMDGPUIntrinsic::AMDGPU_bfm:
1037 return DAG.getNode(AMDGPUISD::BFM, DL, VT,
1041 case AMDGPUIntrinsic::AMDGPU_brev:
1042 return DAG.getNode(AMDGPUISD::BREV, DL, VT, Op.getOperand(1));
1044 case Intrinsic::AMDGPU_class:
1045 return DAG.getNode(AMDGPUISD::FP_CLASS, DL, VT,
1046 Op.getOperand(1), Op.getOperand(2));
1048 case AMDGPUIntrinsic::AMDIL_exp: // Legacy name.
1049 return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
1051 case AMDGPUIntrinsic::AMDIL_round_nearest: // Legacy name.
1052 return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
1053 case AMDGPUIntrinsic::AMDGPU_trunc: // Legacy name.
1054 return DAG.getNode(ISD::FTRUNC, DL, VT, Op.getOperand(1));
1058 ///IABS(a) = SMAX(sub(0, a), a)
1059 SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
1060 SelectionDAG &DAG) const {
1062 EVT VT = Op.getValueType();
1063 SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
1066 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
1069 /// Linear Interpolation
1070 /// LRP(a, b, c) = muladd(a, b, (1 - a) * c)
1071 SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
1072 SelectionDAG &DAG) const {
1074 EVT VT = Op.getValueType();
1075 SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
1076 DAG.getConstantFP(1.0f, DL, MVT::f32),
1078 SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
1080 return DAG.getNode(ISD::FADD, DL, VT,
1081 DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
1085 /// \brief Generate Min/Max node
1086 SDValue AMDGPUTargetLowering::CombineFMinMaxLegacy(SDLoc DL,
1093 DAGCombinerInfo &DCI) const {
1094 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS)
1097 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True))
1100 SelectionDAG &DAG = DCI.DAG;
1101 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1110 case ISD::SETFALSE2:
1119 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1120 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1126 // Ordered. Assume ordered for undefined.
1128 // Only do this after legalization to avoid interfering with other combines
1129 // which might occur.
1130 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1131 !DCI.isCalledByLegalizer())
1134 // We need to permute the operands to get the correct NaN behavior. The
1135 // selected operand is the second one based on the failing compare with NaN,
1136 // so permute it based on the compare type the hardware uses.
1138 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
1139 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
1144 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, RHS, LHS);
1145 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, LHS, RHS);
1151 if (DCI.getDAGCombineLevel() < AfterLegalizeDAG &&
1152 !DCI.isCalledByLegalizer())
1156 return DAG.getNode(AMDGPUISD::FMAX_LEGACY, DL, VT, LHS, RHS);
1157 return DAG.getNode(AMDGPUISD::FMIN_LEGACY, DL, VT, RHS, LHS);
1159 case ISD::SETCC_INVALID:
1160 llvm_unreachable("Invalid setcc condcode!");
1165 /// \brief Generate Min/Max node
1166 SDValue AMDGPUTargetLowering::CombineIMinMax(SDLoc DL,
1173 SelectionDAG &DAG) const {
1174 if (!(LHS == True && RHS == False) && !(LHS == False && RHS == True))
1177 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1181 unsigned Opc = (LHS == True) ? AMDGPUISD::UMIN : AMDGPUISD::UMAX;
1182 return DAG.getNode(Opc, DL, VT, LHS, RHS);
1186 unsigned Opc = (LHS == True) ? AMDGPUISD::SMIN : AMDGPUISD::SMAX;
1187 return DAG.getNode(Opc, DL, VT, LHS, RHS);
1191 unsigned Opc = (LHS == True) ? AMDGPUISD::SMAX : AMDGPUISD::SMIN;
1192 return DAG.getNode(Opc, DL, VT, LHS, RHS);
1196 unsigned Opc = (LHS == True) ? AMDGPUISD::UMAX : AMDGPUISD::UMIN;
1197 return DAG.getNode(Opc, DL, VT, LHS, RHS);
1204 SDValue AMDGPUTargetLowering::ScalarizeVectorLoad(const SDValue Op,
1205 SelectionDAG &DAG) const {
1206 LoadSDNode *Load = cast<LoadSDNode>(Op);
1207 EVT MemVT = Load->getMemoryVT();
1208 EVT MemEltVT = MemVT.getVectorElementType();
1210 EVT LoadVT = Op.getValueType();
1211 EVT EltVT = LoadVT.getVectorElementType();
1212 EVT PtrVT = Load->getBasePtr().getValueType();
1214 unsigned NumElts = Load->getMemoryVT().getVectorNumElements();
1215 SmallVector<SDValue, 8> Loads;
1216 SmallVector<SDValue, 8> Chains;
1219 unsigned MemEltSize = MemEltVT.getStoreSize();
1220 MachinePointerInfo SrcValue(Load->getMemOperand()->getValue());
1222 for (unsigned i = 0; i < NumElts; ++i) {
1223 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, Load->getBasePtr(),
1224 DAG.getConstant(i * MemEltSize, SL, PtrVT));
1227 = DAG.getExtLoad(Load->getExtensionType(), SL, EltVT,
1228 Load->getChain(), Ptr,
1229 SrcValue.getWithOffset(i * MemEltSize),
1230 MemEltVT, Load->isVolatile(), Load->isNonTemporal(),
1231 Load->isInvariant(), Load->getAlignment());
1232 Loads.push_back(NewLoad.getValue(0));
1233 Chains.push_back(NewLoad.getValue(1));
1237 DAG.getNode(ISD::BUILD_VECTOR, SL, LoadVT, Loads),
1238 DAG.getNode(ISD::TokenFactor, SL, MVT::Other, Chains)
1241 return DAG.getMergeValues(Ops, SL);
1244 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue Op,
1245 SelectionDAG &DAG) const {
1246 EVT VT = Op.getValueType();
1248 // If this is a 2 element vector, we really want to scalarize and not create
1249 // weird 1 element vectors.
1250 if (VT.getVectorNumElements() == 2)
1251 return ScalarizeVectorLoad(Op, DAG);
1253 LoadSDNode *Load = cast<LoadSDNode>(Op);
1254 SDValue BasePtr = Load->getBasePtr();
1255 EVT PtrVT = BasePtr.getValueType();
1256 EVT MemVT = Load->getMemoryVT();
1258 MachinePointerInfo SrcValue(Load->getMemOperand()->getValue());
1261 EVT LoMemVT, HiMemVT;
1264 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1265 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1266 std::tie(Lo, Hi) = DAG.SplitVector(Op, SL, LoVT, HiVT);
1268 = DAG.getExtLoad(Load->getExtensionType(), SL, LoVT,
1269 Load->getChain(), BasePtr,
1271 LoMemVT, Load->isVolatile(), Load->isNonTemporal(),
1272 Load->isInvariant(), Load->getAlignment());
1274 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
1275 DAG.getConstant(LoMemVT.getStoreSize(), SL,
1279 = DAG.getExtLoad(Load->getExtensionType(), SL, HiVT,
1280 Load->getChain(), HiPtr,
1281 SrcValue.getWithOffset(LoMemVT.getStoreSize()),
1282 HiMemVT, Load->isVolatile(), Load->isNonTemporal(),
1283 Load->isInvariant(), Load->getAlignment());
1286 DAG.getNode(ISD::CONCAT_VECTORS, SL, VT, LoLoad, HiLoad),
1287 DAG.getNode(ISD::TokenFactor, SL, MVT::Other,
1288 LoLoad.getValue(1), HiLoad.getValue(1))
1291 return DAG.getMergeValues(Ops, SL);
1294 SDValue AMDGPUTargetLowering::MergeVectorStore(const SDValue &Op,
1295 SelectionDAG &DAG) const {
1296 StoreSDNode *Store = cast<StoreSDNode>(Op);
1297 EVT MemVT = Store->getMemoryVT();
1298 unsigned MemBits = MemVT.getSizeInBits();
1300 // Byte stores are really expensive, so if possible, try to pack 32-bit vector
1301 // truncating store into an i32 store.
1302 // XXX: We could also handle optimize other vector bitwidths.
1303 if (!MemVT.isVector() || MemBits > 32) {
1308 SDValue Value = Store->getValue();
1309 EVT VT = Value.getValueType();
1310 EVT ElemVT = VT.getVectorElementType();
1311 SDValue Ptr = Store->getBasePtr();
1312 EVT MemEltVT = MemVT.getVectorElementType();
1313 unsigned MemEltBits = MemEltVT.getSizeInBits();
1314 unsigned MemNumElements = MemVT.getVectorNumElements();
1315 unsigned PackedSize = MemVT.getStoreSizeInBits();
1316 SDValue Mask = DAG.getConstant((1 << MemEltBits) - 1, DL, MVT::i32);
1318 assert(Value.getValueType().getScalarSizeInBits() >= 32);
1320 SDValue PackedValue;
1321 for (unsigned i = 0; i < MemNumElements; ++i) {
1322 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT, Value,
1323 DAG.getConstant(i, DL, MVT::i32));
1324 Elt = DAG.getZExtOrTrunc(Elt, DL, MVT::i32);
1325 Elt = DAG.getNode(ISD::AND, DL, MVT::i32, Elt, Mask); // getZeroExtendInReg
1327 SDValue Shift = DAG.getConstant(MemEltBits * i, DL, MVT::i32);
1328 Elt = DAG.getNode(ISD::SHL, DL, MVT::i32, Elt, Shift);
1333 PackedValue = DAG.getNode(ISD::OR, DL, MVT::i32, PackedValue, Elt);
1337 if (PackedSize < 32) {
1338 EVT PackedVT = EVT::getIntegerVT(*DAG.getContext(), PackedSize);
1339 return DAG.getTruncStore(Store->getChain(), DL, PackedValue, Ptr,
1340 Store->getMemOperand()->getPointerInfo(),
1342 Store->isNonTemporal(), Store->isVolatile(),
1343 Store->getAlignment());
1346 return DAG.getStore(Store->getChain(), DL, PackedValue, Ptr,
1347 Store->getMemOperand()->getPointerInfo(),
1348 Store->isVolatile(), Store->isNonTemporal(),
1349 Store->getAlignment());
1352 SDValue AMDGPUTargetLowering::ScalarizeVectorStore(SDValue Op,
1353 SelectionDAG &DAG) const {
1354 StoreSDNode *Store = cast<StoreSDNode>(Op);
1355 EVT MemEltVT = Store->getMemoryVT().getVectorElementType();
1356 EVT EltVT = Store->getValue().getValueType().getVectorElementType();
1357 EVT PtrVT = Store->getBasePtr().getValueType();
1358 unsigned NumElts = Store->getMemoryVT().getVectorNumElements();
1361 SmallVector<SDValue, 8> Chains;
1363 unsigned EltSize = MemEltVT.getStoreSize();
1364 MachinePointerInfo SrcValue(Store->getMemOperand()->getValue());
1366 for (unsigned i = 0, e = NumElts; i != e; ++i) {
1367 SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
1369 DAG.getConstant(i, SL, MVT::i32));
1371 SDValue Offset = DAG.getConstant(i * MemEltVT.getStoreSize(), SL, PtrVT);
1372 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, Store->getBasePtr(), Offset);
1374 DAG.getTruncStore(Store->getChain(), SL, Val, Ptr,
1375 SrcValue.getWithOffset(i * EltSize),
1376 MemEltVT, Store->isNonTemporal(), Store->isVolatile(),
1377 Store->getAlignment());
1378 Chains.push_back(NewStore);
1381 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, Chains);
1384 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
1385 SelectionDAG &DAG) const {
1386 StoreSDNode *Store = cast<StoreSDNode>(Op);
1387 SDValue Val = Store->getValue();
1388 EVT VT = Val.getValueType();
1390 // If this is a 2 element vector, we really want to scalarize and not create
1391 // weird 1 element vectors.
1392 if (VT.getVectorNumElements() == 2)
1393 return ScalarizeVectorStore(Op, DAG);
1395 EVT MemVT = Store->getMemoryVT();
1396 SDValue Chain = Store->getChain();
1397 SDValue BasePtr = Store->getBasePtr();
1401 EVT LoMemVT, HiMemVT;
1404 std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT);
1405 std::tie(LoMemVT, HiMemVT) = DAG.GetSplitDestVTs(MemVT);
1406 std::tie(Lo, Hi) = DAG.SplitVector(Val, SL, LoVT, HiVT);
1408 EVT PtrVT = BasePtr.getValueType();
1409 SDValue HiPtr = DAG.getNode(ISD::ADD, SL, PtrVT, BasePtr,
1410 DAG.getConstant(LoMemVT.getStoreSize(), SL,
1413 MachinePointerInfo SrcValue(Store->getMemOperand()->getValue());
1415 = DAG.getTruncStore(Chain, SL, Lo,
1419 Store->isNonTemporal(),
1420 Store->isVolatile(),
1421 Store->getAlignment());
1423 = DAG.getTruncStore(Chain, SL, Hi,
1425 SrcValue.getWithOffset(LoMemVT.getStoreSize()),
1427 Store->isNonTemporal(),
1428 Store->isVolatile(),
1429 Store->getAlignment());
1431 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, LoStore, HiStore);
1435 SDValue AMDGPUTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1437 LoadSDNode *Load = cast<LoadSDNode>(Op);
1438 ISD::LoadExtType ExtType = Load->getExtensionType();
1439 EVT VT = Op.getValueType();
1440 EVT MemVT = Load->getMemoryVT();
1442 if (ExtType == ISD::NON_EXTLOAD && VT.getSizeInBits() < 32) {
1443 assert(VT == MVT::i1 && "Only i1 non-extloads expected");
1444 // FIXME: Copied from PPC
1445 // First, load into 32 bits, then truncate to 1 bit.
1447 SDValue Chain = Load->getChain();
1448 SDValue BasePtr = Load->getBasePtr();
1449 MachineMemOperand *MMO = Load->getMemOperand();
1451 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
1452 BasePtr, MVT::i8, MMO);
1455 DAG.getNode(ISD::TRUNCATE, DL, VT, NewLD),
1459 return DAG.getMergeValues(Ops, DL);
1462 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS ||
1463 Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS ||
1464 ExtType == ISD::NON_EXTLOAD || Load->getMemoryVT().bitsGE(MVT::i32))
1467 // <SI && AS=PRIVATE && EXTLOAD && size < 32bit,
1468 // register (2-)byte extract.
1470 // Get Register holding the target.
1471 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
1472 DAG.getConstant(2, DL, MVT::i32));
1473 // Load the Register.
1474 SDValue Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
1475 Load->getChain(), Ptr,
1476 DAG.getTargetConstant(0, DL, MVT::i32),
1479 // Get offset within the register.
1480 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
1482 DAG.getConstant(0x3, DL, MVT::i32));
1484 // Bit offset of target byte (byteIdx * 8).
1485 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1486 DAG.getConstant(3, DL, MVT::i32));
1488 // Shift to the right.
1489 Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Ret, ShiftAmt);
1491 // Eliminate the upper bits by setting them to ...
1492 EVT MemEltVT = MemVT.getScalarType();
1495 if (ExtType == ISD::SEXTLOAD) {
1496 SDValue MemEltVTNode = DAG.getValueType(MemEltVT);
1499 DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode),
1503 return DAG.getMergeValues(Ops, DL);
1508 DAG.getZeroExtendInReg(Ret, DL, MemEltVT),
1512 return DAG.getMergeValues(Ops, DL);
1515 SDValue AMDGPUTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1517 SDValue Result = AMDGPUTargetLowering::MergeVectorStore(Op, DAG);
1518 if (Result.getNode()) {
1522 StoreSDNode *Store = cast<StoreSDNode>(Op);
1523 SDValue Chain = Store->getChain();
1524 if ((Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1525 Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) &&
1526 Store->getValue().getValueType().isVector()) {
1527 return ScalarizeVectorStore(Op, DAG);
1530 EVT MemVT = Store->getMemoryVT();
1531 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS &&
1532 MemVT.bitsLT(MVT::i32)) {
1534 if (Store->getMemoryVT() == MVT::i8) {
1536 } else if (Store->getMemoryVT() == MVT::i16) {
1539 SDValue BasePtr = Store->getBasePtr();
1540 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, BasePtr,
1541 DAG.getConstant(2, DL, MVT::i32));
1542 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
1544 DAG.getTargetConstant(0, DL, MVT::i32));
1546 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, BasePtr,
1547 DAG.getConstant(0x3, DL, MVT::i32));
1549 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1550 DAG.getConstant(3, DL, MVT::i32));
1552 SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
1555 SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT);
1557 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1558 MaskedValue, ShiftAmt);
1560 SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32,
1561 DAG.getConstant(Mask, DL, MVT::i32),
1563 DstMask = DAG.getNode(ISD::XOR, DL, MVT::i32, DstMask,
1564 DAG.getConstant(0xffffffff, DL, MVT::i32));
1565 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1567 SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
1568 return DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1570 DAG.getTargetConstant(0, DL, MVT::i32));
1575 // This is a shortcut for integer division because we have fast i32<->f32
1576 // conversions, and fast f32 reciprocal instructions. The fractional part of a
1577 // float is enough to accurately represent up to a 24-bit integer.
1578 SDValue AMDGPUTargetLowering::LowerDIVREM24(SDValue Op, SelectionDAG &DAG, bool sign) const {
1580 EVT VT = Op.getValueType();
1581 SDValue LHS = Op.getOperand(0);
1582 SDValue RHS = Op.getOperand(1);
1583 MVT IntVT = MVT::i32;
1584 MVT FltVT = MVT::f32;
1586 ISD::NodeType ToFp = sign ? ISD::SINT_TO_FP : ISD::UINT_TO_FP;
1587 ISD::NodeType ToInt = sign ? ISD::FP_TO_SINT : ISD::FP_TO_UINT;
1589 if (VT.isVector()) {
1590 unsigned NElts = VT.getVectorNumElements();
1591 IntVT = MVT::getVectorVT(MVT::i32, NElts);
1592 FltVT = MVT::getVectorVT(MVT::f32, NElts);
1595 unsigned BitSize = VT.getScalarType().getSizeInBits();
1597 SDValue jq = DAG.getConstant(1, DL, IntVT);
1600 // char|short jq = ia ^ ib;
1601 jq = DAG.getNode(ISD::XOR, DL, VT, LHS, RHS);
1603 // jq = jq >> (bitsize - 2)
1604 jq = DAG.getNode(ISD::SRA, DL, VT, jq,
1605 DAG.getConstant(BitSize - 2, DL, VT));
1608 jq = DAG.getNode(ISD::OR, DL, VT, jq, DAG.getConstant(1, DL, VT));
1611 jq = DAG.getSExtOrTrunc(jq, DL, IntVT);
1614 // int ia = (int)LHS;
1616 DAG.getSExtOrTrunc(LHS, DL, IntVT) : DAG.getZExtOrTrunc(LHS, DL, IntVT);
1618 // int ib, (int)RHS;
1620 DAG.getSExtOrTrunc(RHS, DL, IntVT) : DAG.getZExtOrTrunc(RHS, DL, IntVT);
1622 // float fa = (float)ia;
1623 SDValue fa = DAG.getNode(ToFp, DL, FltVT, ia);
1625 // float fb = (float)ib;
1626 SDValue fb = DAG.getNode(ToFp, DL, FltVT, ib);
1628 // float fq = native_divide(fa, fb);
1629 SDValue fq = DAG.getNode(ISD::FMUL, DL, FltVT,
1630 fa, DAG.getNode(AMDGPUISD::RCP, DL, FltVT, fb));
1633 fq = DAG.getNode(ISD::FTRUNC, DL, FltVT, fq);
1635 // float fqneg = -fq;
1636 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FltVT, fq);
1638 // float fr = mad(fqneg, fb, fa);
1639 SDValue fr = DAG.getNode(ISD::FADD, DL, FltVT,
1640 DAG.getNode(ISD::FMUL, DL, FltVT, fqneg, fb), fa);
1642 // int iq = (int)fq;
1643 SDValue iq = DAG.getNode(ToInt, DL, IntVT, fq);
1646 fr = DAG.getNode(ISD::FABS, DL, FltVT, fr);
1649 fb = DAG.getNode(ISD::FABS, DL, FltVT, fb);
1651 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), VT);
1653 // int cv = fr >= fb;
1654 SDValue cv = DAG.getSetCC(DL, SetCCVT, fr, fb, ISD::SETOGE);
1656 // jq = (cv ? jq : 0);
1657 jq = DAG.getNode(ISD::SELECT, DL, VT, cv, jq, DAG.getConstant(0, DL, VT));
1659 // dst = trunc/extend to legal type
1660 iq = sign ? DAG.getSExtOrTrunc(iq, DL, VT) : DAG.getZExtOrTrunc(iq, DL, VT);
1663 SDValue Div = DAG.getNode(ISD::ADD, DL, VT, iq, jq);
1665 // Rem needs compensation, it's easier to recompute it
1666 SDValue Rem = DAG.getNode(ISD::MUL, DL, VT, Div, RHS);
1667 Rem = DAG.getNode(ISD::SUB, DL, VT, LHS, Rem);
1673 return DAG.getMergeValues(Res, DL);
1676 void AMDGPUTargetLowering::LowerUDIVREM64(SDValue Op,
1678 SmallVectorImpl<SDValue> &Results) const {
1679 assert(Op.getValueType() == MVT::i64);
1682 EVT VT = Op.getValueType();
1683 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1685 SDValue one = DAG.getConstant(1, DL, HalfVT);
1686 SDValue zero = DAG.getConstant(0, DL, HalfVT);
1689 SDValue LHS = Op.getOperand(0);
1690 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero);
1691 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one);
1693 SDValue RHS = Op.getOperand(1);
1694 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero);
1695 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one);
1697 if (VT == MVT::i64 &&
1698 DAG.MaskedValueIsZero(RHS, APInt::getHighBitsSet(64, 32)) &&
1699 DAG.MaskedValueIsZero(LHS, APInt::getHighBitsSet(64, 32))) {
1701 SDValue Res = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1704 SDValue DIV = DAG.getNode(ISD::BUILD_PAIR, DL, VT, Res.getValue(0), zero);
1705 SDValue REM = DAG.getNode(ISD::BUILD_PAIR, DL, VT, Res.getValue(1), zero);
1706 Results.push_back(DIV);
1707 Results.push_back(REM);
1711 // Get Speculative values
1712 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo);
1713 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo);
1715 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ);
1716 SDValue REM = DAG.getNode(ISD::BUILD_PAIR, DL, VT, REM_Lo, zero);
1718 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ);
1719 SDValue DIV_Lo = zero;
1721 const unsigned halfBitWidth = HalfVT.getSizeInBits();
1723 for (unsigned i = 0; i < halfBitWidth; ++i) {
1724 const unsigned bitPos = halfBitWidth - i - 1;
1725 SDValue POS = DAG.getConstant(bitPos, DL, HalfVT);
1726 // Get value of high bit
1727 SDValue HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS);
1728 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one);
1729 HBit = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, HBit);
1732 REM = DAG.getNode(ISD::SHL, DL, VT, REM, DAG.getConstant(1, DL, VT));
1734 REM = DAG.getNode(ISD::OR, DL, VT, REM, HBit);
1736 SDValue BIT = DAG.getConstant(1 << bitPos, DL, HalfVT);
1737 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETUGE);
1739 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT);
1742 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS);
1743 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETUGE);
1746 SDValue DIV = DAG.getNode(ISD::BUILD_PAIR, DL, VT, DIV_Lo, DIV_Hi);
1747 Results.push_back(DIV);
1748 Results.push_back(REM);
1751 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
1752 SelectionDAG &DAG) const {
1754 EVT VT = Op.getValueType();
1756 if (VT == MVT::i64) {
1757 SmallVector<SDValue, 2> Results;
1758 LowerUDIVREM64(Op, DAG, Results);
1759 return DAG.getMergeValues(Results, DL);
1762 SDValue Num = Op.getOperand(0);
1763 SDValue Den = Op.getOperand(1);
1765 if (VT == MVT::i32) {
1766 if (DAG.MaskedValueIsZero(Num, APInt::getHighBitsSet(32, 8)) &&
1767 DAG.MaskedValueIsZero(Den, APInt::getHighBitsSet(32, 8))) {
1768 // TODO: We technically could do this for i64, but shouldn't that just be
1769 // handled by something generally reducing 64-bit division on 32-bit
1770 // values to 32-bit?
1771 return LowerDIVREM24(Op, DAG, false);
1775 // RCP = URECIP(Den) = 2^32 / Den + e
1776 // e is rounding error.
1777 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
1779 // RCP_LO = mul(RCP, Den) */
1780 SDValue RCP_LO = DAG.getNode(ISD::MUL, DL, VT, RCP, Den);
1782 // RCP_HI = mulhu (RCP, Den) */
1783 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
1785 // NEG_RCP_LO = -RCP_LO
1786 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT),
1789 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
1790 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
1793 // Calculate the rounding error from the URECIP instruction
1794 // E = mulhu(ABS_RCP_LO, RCP)
1795 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
1797 // RCP_A_E = RCP + E
1798 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
1800 // RCP_S_E = RCP - E
1801 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
1803 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
1804 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, DL, VT),
1807 // Quotient = mulhu(Tmp0, Num)
1808 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
1810 // Num_S_Remainder = Quotient * Den
1811 SDValue Num_S_Remainder = DAG.getNode(ISD::MUL, DL, VT, Quotient, Den);
1813 // Remainder = Num - Num_S_Remainder
1814 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
1816 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
1817 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
1818 DAG.getConstant(-1, DL, VT),
1819 DAG.getConstant(0, DL, VT),
1821 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
1822 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
1824 DAG.getConstant(-1, DL, VT),
1825 DAG.getConstant(0, DL, VT),
1827 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
1828 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
1831 // Calculate Division result:
1833 // Quotient_A_One = Quotient + 1
1834 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
1835 DAG.getConstant(1, DL, VT));
1837 // Quotient_S_One = Quotient - 1
1838 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
1839 DAG.getConstant(1, DL, VT));
1841 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
1842 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
1843 Quotient, Quotient_A_One, ISD::SETEQ);
1845 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
1846 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
1847 Quotient_S_One, Div, ISD::SETEQ);
1849 // Calculate Rem result:
1851 // Remainder_S_Den = Remainder - Den
1852 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
1854 // Remainder_A_Den = Remainder + Den
1855 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
1857 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
1858 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, DL, VT),
1859 Remainder, Remainder_S_Den, ISD::SETEQ);
1861 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
1862 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, DL, VT),
1863 Remainder_A_Den, Rem, ISD::SETEQ);
1868 return DAG.getMergeValues(Ops, DL);
1871 SDValue AMDGPUTargetLowering::LowerSDIVREM(SDValue Op,
1872 SelectionDAG &DAG) const {
1874 EVT VT = Op.getValueType();
1876 SDValue LHS = Op.getOperand(0);
1877 SDValue RHS = Op.getOperand(1);
1879 SDValue Zero = DAG.getConstant(0, DL, VT);
1880 SDValue NegOne = DAG.getConstant(-1, DL, VT);
1882 if (VT == MVT::i32 &&
1883 DAG.ComputeNumSignBits(LHS) > 8 &&
1884 DAG.ComputeNumSignBits(RHS) > 8) {
1885 return LowerDIVREM24(Op, DAG, true);
1887 if (VT == MVT::i64 &&
1888 DAG.ComputeNumSignBits(LHS) > 32 &&
1889 DAG.ComputeNumSignBits(RHS) > 32) {
1890 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
1893 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, Zero);
1894 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, Zero);
1895 SDValue DIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(HalfVT, HalfVT),
1898 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(0)),
1899 DAG.getNode(ISD::SIGN_EXTEND, DL, VT, DIVREM.getValue(1))
1901 return DAG.getMergeValues(Res, DL);
1904 SDValue LHSign = DAG.getSelectCC(DL, LHS, Zero, NegOne, Zero, ISD::SETLT);
1905 SDValue RHSign = DAG.getSelectCC(DL, RHS, Zero, NegOne, Zero, ISD::SETLT);
1906 SDValue DSign = DAG.getNode(ISD::XOR, DL, VT, LHSign, RHSign);
1907 SDValue RSign = LHSign; // Remainder sign is the same as LHS
1909 LHS = DAG.getNode(ISD::ADD, DL, VT, LHS, LHSign);
1910 RHS = DAG.getNode(ISD::ADD, DL, VT, RHS, RHSign);
1912 LHS = DAG.getNode(ISD::XOR, DL, VT, LHS, LHSign);
1913 RHS = DAG.getNode(ISD::XOR, DL, VT, RHS, RHSign);
1915 SDValue Div = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT), LHS, RHS);
1916 SDValue Rem = Div.getValue(1);
1918 Div = DAG.getNode(ISD::XOR, DL, VT, Div, DSign);
1919 Rem = DAG.getNode(ISD::XOR, DL, VT, Rem, RSign);
1921 Div = DAG.getNode(ISD::SUB, DL, VT, Div, DSign);
1922 Rem = DAG.getNode(ISD::SUB, DL, VT, Rem, RSign);
1928 return DAG.getMergeValues(Res, DL);
1931 // (frem x, y) -> (fsub x, (fmul (ftrunc (fdiv x, y)), y))
1932 SDValue AMDGPUTargetLowering::LowerFREM(SDValue Op, SelectionDAG &DAG) const {
1934 EVT VT = Op.getValueType();
1935 SDValue X = Op.getOperand(0);
1936 SDValue Y = Op.getOperand(1);
1938 SDValue Div = DAG.getNode(ISD::FDIV, SL, VT, X, Y);
1939 SDValue Floor = DAG.getNode(ISD::FTRUNC, SL, VT, Div);
1940 SDValue Mul = DAG.getNode(ISD::FMUL, SL, VT, Floor, Y);
1942 return DAG.getNode(ISD::FSUB, SL, VT, X, Mul);
1945 SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const {
1947 SDValue Src = Op.getOperand(0);
1949 // result = trunc(src)
1950 // if (src > 0.0 && src != result)
1953 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1955 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
1956 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f64);
1958 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f64);
1960 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT);
1961 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1962 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1964 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero);
1965 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1968 static SDValue extractF64Exponent(SDValue Hi, SDLoc SL, SelectionDAG &DAG) {
1969 const unsigned FractBits = 52;
1970 const unsigned ExpBits = 11;
1972 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_U32, SL, MVT::i32,
1974 DAG.getConstant(FractBits - 32, SL, MVT::i32),
1975 DAG.getConstant(ExpBits, SL, MVT::i32));
1976 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart,
1977 DAG.getConstant(1023, SL, MVT::i32));
1982 SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const {
1984 SDValue Src = Op.getOperand(0);
1986 assert(Op.getValueType() == MVT::f64);
1988 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
1989 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
1991 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1993 // Extract the upper half, since this is where we will find the sign and
1995 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One);
1997 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
1999 const unsigned FractBits = 52;
2001 // Extract the sign bit.
2002 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, SL, MVT::i32);
2003 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask);
2005 // Extend back to to 64-bits.
2006 SDValue SignBit64 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
2008 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64);
2010 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src);
2011 const SDValue FractMask
2012 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, SL, MVT::i64);
2014 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp);
2015 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64);
2016 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not);
2018 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::i32);
2020 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, SL, MVT::i32);
2022 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
2023 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
2025 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0);
2026 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1);
2028 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2);
2031 SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const {
2033 SDValue Src = Op.getOperand(0);
2035 assert(Op.getValueType() == MVT::f64);
2037 APFloat C1Val(APFloat::IEEEdouble, "0x1.0p+52");
2038 SDValue C1 = DAG.getConstantFP(C1Val, SL, MVT::f64);
2039 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src);
2041 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign);
2042 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign);
2044 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src);
2046 APFloat C2Val(APFloat::IEEEdouble, "0x1.fffffffffffffp+51");
2047 SDValue C2 = DAG.getConstantFP(C2Val, SL, MVT::f64);
2049 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f64);
2050 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT);
2052 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2);
2055 SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const {
2056 // FNEARBYINT and FRINT are the same, except in their handling of FP
2057 // exceptions. Those aren't really meaningful for us, and OpenCL only has
2058 // rint, so just treat them as equivalent.
2059 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0));
2062 // XXX - May require not supporting f32 denormals?
2063 SDValue AMDGPUTargetLowering::LowerFROUND32(SDValue Op, SelectionDAG &DAG) const {
2065 SDValue X = Op.getOperand(0);
2067 SDValue T = DAG.getNode(ISD::FTRUNC, SL, MVT::f32, X);
2069 SDValue Diff = DAG.getNode(ISD::FSUB, SL, MVT::f32, X, T);
2071 SDValue AbsDiff = DAG.getNode(ISD::FABS, SL, MVT::f32, Diff);
2073 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f32);
2074 const SDValue One = DAG.getConstantFP(1.0, SL, MVT::f32);
2075 const SDValue Half = DAG.getConstantFP(0.5, SL, MVT::f32);
2077 SDValue SignOne = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f32, One, X);
2079 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f32);
2081 SDValue Cmp = DAG.getSetCC(SL, SetCCVT, AbsDiff, Half, ISD::SETOGE);
2083 SDValue Sel = DAG.getNode(ISD::SELECT, SL, MVT::f32, Cmp, SignOne, Zero);
2085 return DAG.getNode(ISD::FADD, SL, MVT::f32, T, Sel);
2088 SDValue AMDGPUTargetLowering::LowerFROUND64(SDValue Op, SelectionDAG &DAG) const {
2090 SDValue X = Op.getOperand(0);
2092 SDValue L = DAG.getNode(ISD::BITCAST, SL, MVT::i64, X);
2094 const SDValue Zero = DAG.getConstant(0, SL, MVT::i32);
2095 const SDValue One = DAG.getConstant(1, SL, MVT::i32);
2096 const SDValue NegOne = DAG.getConstant(-1, SL, MVT::i32);
2097 const SDValue FiftyOne = DAG.getConstant(51, SL, MVT::i32);
2098 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::i32);
2101 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, X);
2103 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC, One);
2105 SDValue Exp = extractF64Exponent(Hi, SL, DAG);
2107 const SDValue Mask = DAG.getConstant(INT64_C(0x000fffffffffffff), SL,
2110 SDValue M = DAG.getNode(ISD::SRA, SL, MVT::i64, Mask, Exp);
2111 SDValue D = DAG.getNode(ISD::SRA, SL, MVT::i64,
2112 DAG.getConstant(INT64_C(0x0008000000000000), SL,
2116 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, L, M);
2117 SDValue Tmp1 = DAG.getSetCC(SL, SetCCVT,
2118 DAG.getConstant(0, SL, MVT::i64), Tmp0,
2121 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, Tmp1,
2122 D, DAG.getConstant(0, SL, MVT::i64));
2123 SDValue K = DAG.getNode(ISD::ADD, SL, MVT::i64, L, Tmp2);
2125 K = DAG.getNode(ISD::AND, SL, MVT::i64, K, DAG.getNOT(SL, M, MVT::i64));
2126 K = DAG.getNode(ISD::BITCAST, SL, MVT::f64, K);
2128 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
2129 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
2130 SDValue ExpEqNegOne = DAG.getSetCC(SL, SetCCVT, NegOne, Exp, ISD::SETEQ);
2132 SDValue Mag = DAG.getNode(ISD::SELECT, SL, MVT::f64,
2134 DAG.getConstantFP(1.0, SL, MVT::f64),
2135 DAG.getConstantFP(0.0, SL, MVT::f64));
2137 SDValue S = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, Mag, X);
2139 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpLt0, S, K);
2140 K = DAG.getNode(ISD::SELECT, SL, MVT::f64, ExpGt51, X, K);
2145 SDValue AMDGPUTargetLowering::LowerFROUND(SDValue Op, SelectionDAG &DAG) const {
2146 EVT VT = Op.getValueType();
2149 return LowerFROUND32(Op, DAG);
2152 return LowerFROUND64(Op, DAG);
2154 llvm_unreachable("unhandled type");
2157 SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const {
2159 SDValue Src = Op.getOperand(0);
2161 // result = trunc(src);
2162 // if (src < 0.0 && src != result)
2165 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
2167 const SDValue Zero = DAG.getConstantFP(0.0, SL, MVT::f64);
2168 const SDValue NegOne = DAG.getConstantFP(-1.0, SL, MVT::f64);
2170 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f64);
2172 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT);
2173 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
2174 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
2176 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero);
2177 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
2180 SDValue AMDGPUTargetLowering::LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG,
2181 bool Signed) const {
2183 SDValue Src = Op.getOperand(0);
2185 SDValue BC = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
2187 SDValue Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
2188 DAG.getConstant(0, SL, MVT::i32));
2189 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, BC,
2190 DAG.getConstant(1, SL, MVT::i32));
2192 SDValue CvtHi = DAG.getNode(Signed ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
2195 SDValue CvtLo = DAG.getNode(ISD::UINT_TO_FP, SL, MVT::f64, Lo);
2197 SDValue LdExp = DAG.getNode(AMDGPUISD::LDEXP, SL, MVT::f64, CvtHi,
2198 DAG.getConstant(32, SL, MVT::i32));
2200 return DAG.getNode(ISD::FADD, SL, MVT::f64, LdExp, CvtLo);
2203 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
2204 SelectionDAG &DAG) const {
2205 SDValue S0 = Op.getOperand(0);
2206 if (S0.getValueType() != MVT::i64)
2209 EVT DestVT = Op.getValueType();
2210 if (DestVT == MVT::f64)
2211 return LowerINT_TO_FP64(Op, DAG, false);
2213 assert(DestVT == MVT::f32);
2217 // f32 uint_to_fp i64
2218 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
2219 DAG.getConstant(0, DL, MVT::i32));
2220 SDValue FloatLo = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Lo);
2221 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
2222 DAG.getConstant(1, DL, MVT::i32));
2223 SDValue FloatHi = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Hi);
2224 FloatHi = DAG.getNode(ISD::FMUL, DL, MVT::f32, FloatHi,
2225 DAG.getConstantFP(4294967296.0f, DL, MVT::f32)); // 2^32
2226 return DAG.getNode(ISD::FADD, DL, MVT::f32, FloatLo, FloatHi);
2229 SDValue AMDGPUTargetLowering::LowerSINT_TO_FP(SDValue Op,
2230 SelectionDAG &DAG) const {
2231 SDValue Src = Op.getOperand(0);
2232 if (Src.getValueType() == MVT::i64 && Op.getValueType() == MVT::f64)
2233 return LowerINT_TO_FP64(Op, DAG, true);
2238 SDValue AMDGPUTargetLowering::LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG,
2239 bool Signed) const {
2242 SDValue Src = Op.getOperand(0);
2244 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
2246 SDValue K0 = DAG.getConstantFP(BitsToDouble(UINT64_C(0x3df0000000000000)), SL,
2248 SDValue K1 = DAG.getConstantFP(BitsToDouble(UINT64_C(0xc1f0000000000000)), SL,
2251 SDValue Mul = DAG.getNode(ISD::FMUL, SL, MVT::f64, Trunc, K0);
2253 SDValue FloorMul = DAG.getNode(ISD::FFLOOR, SL, MVT::f64, Mul);
2256 SDValue Fma = DAG.getNode(ISD::FMA, SL, MVT::f64, FloorMul, K1, Trunc);
2258 SDValue Hi = DAG.getNode(Signed ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, SL,
2259 MVT::i32, FloorMul);
2260 SDValue Lo = DAG.getNode(ISD::FP_TO_UINT, SL, MVT::i32, Fma);
2262 SDValue Result = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32, Lo, Hi);
2264 return DAG.getNode(ISD::BITCAST, SL, MVT::i64, Result);
2267 SDValue AMDGPUTargetLowering::LowerFP_TO_SINT(SDValue Op,
2268 SelectionDAG &DAG) const {
2269 SDValue Src = Op.getOperand(0);
2271 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2272 return LowerFP64_TO_INT(Op, DAG, true);
2277 SDValue AMDGPUTargetLowering::LowerFP_TO_UINT(SDValue Op,
2278 SelectionDAG &DAG) const {
2279 SDValue Src = Op.getOperand(0);
2281 if (Op.getValueType() == MVT::i64 && Src.getValueType() == MVT::f64)
2282 return LowerFP64_TO_INT(Op, DAG, false);
2287 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
2288 SelectionDAG &DAG) const {
2289 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
2290 MVT VT = Op.getSimpleValueType();
2291 MVT ScalarVT = VT.getScalarType();
2296 SDValue Src = Op.getOperand(0);
2299 // TODO: Don't scalarize on Evergreen?
2300 unsigned NElts = VT.getVectorNumElements();
2301 SmallVector<SDValue, 8> Args;
2302 DAG.ExtractVectorElements(Src, Args, 0, NElts);
2304 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType());
2305 for (unsigned I = 0; I < NElts; ++I)
2306 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp);
2308 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Args);
2311 //===----------------------------------------------------------------------===//
2312 // Custom DAG optimizations
2313 //===----------------------------------------------------------------------===//
2315 static bool isU24(SDValue Op, SelectionDAG &DAG) {
2316 APInt KnownZero, KnownOne;
2317 EVT VT = Op.getValueType();
2318 DAG.computeKnownBits(Op, KnownZero, KnownOne);
2320 return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24;
2323 static bool isI24(SDValue Op, SelectionDAG &DAG) {
2324 EVT VT = Op.getValueType();
2326 // In order for this to be a signed 24-bit value, bit 23, must
2328 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
2329 // as unsigned 24-bit values.
2330 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24;
2333 static void simplifyI24(SDValue Op, TargetLowering::DAGCombinerInfo &DCI) {
2335 SelectionDAG &DAG = DCI.DAG;
2336 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2337 EVT VT = Op.getValueType();
2339 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24);
2340 APInt KnownZero, KnownOne;
2341 TargetLowering::TargetLoweringOpt TLO(DAG, true, true);
2342 if (TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
2343 DCI.CommitTargetLoweringOpt(TLO);
2346 template <typename IntTy>
2347 static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0,
2348 uint32_t Offset, uint32_t Width, SDLoc DL) {
2349 if (Width + Offset < 32) {
2350 uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width);
2351 IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width);
2352 return DAG.getConstant(Result, DL, MVT::i32);
2355 return DAG.getConstant(Src0 >> Offset, DL, MVT::i32);
2358 static bool usesAllNormalStores(SDNode *LoadVal) {
2359 for (SDNode::use_iterator I = LoadVal->use_begin(); !I.atEnd(); ++I) {
2360 if (!ISD::isNormalStore(*I))
2367 // If we have a copy of an illegal type, replace it with a load / store of an
2368 // equivalently sized legal type. This avoids intermediate bit pack / unpack
2369 // instructions emitted when handling extloads and truncstores. Ideally we could
2370 // recognize the pack / unpack pattern to eliminate it.
2371 SDValue AMDGPUTargetLowering::performStoreCombine(SDNode *N,
2372 DAGCombinerInfo &DCI) const {
2373 if (!DCI.isBeforeLegalize())
2376 StoreSDNode *SN = cast<StoreSDNode>(N);
2377 SDValue Value = SN->getValue();
2378 EVT VT = Value.getValueType();
2380 if (isTypeLegal(VT) || SN->isVolatile() ||
2381 !ISD::isNormalLoad(Value.getNode()) || VT.getSizeInBits() < 8)
2384 LoadSDNode *LoadVal = cast<LoadSDNode>(Value);
2385 if (LoadVal->isVolatile() || !usesAllNormalStores(LoadVal))
2388 EVT MemVT = LoadVal->getMemoryVT();
2391 SelectionDAG &DAG = DCI.DAG;
2392 EVT LoadVT = getEquivalentMemType(*DAG.getContext(), MemVT);
2394 SDValue NewLoad = DAG.getLoad(ISD::UNINDEXED, ISD::NON_EXTLOAD,
2396 LoadVal->getChain(),
2397 LoadVal->getBasePtr(),
2398 LoadVal->getOffset(),
2400 LoadVal->getMemOperand());
2402 SDValue CastLoad = DAG.getNode(ISD::BITCAST, SL, VT, NewLoad.getValue(0));
2403 DCI.CombineTo(LoadVal, CastLoad, NewLoad.getValue(1), false);
2405 return DAG.getStore(SN->getChain(), SL, NewLoad,
2406 SN->getBasePtr(), SN->getMemOperand());
2409 SDValue AMDGPUTargetLowering::performMulCombine(SDNode *N,
2410 DAGCombinerInfo &DCI) const {
2411 EVT VT = N->getValueType(0);
2413 if (VT.isVector() || VT.getSizeInBits() > 32)
2416 SelectionDAG &DAG = DCI.DAG;
2419 SDValue N0 = N->getOperand(0);
2420 SDValue N1 = N->getOperand(1);
2423 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) {
2424 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
2425 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
2426 Mul = DAG.getNode(AMDGPUISD::MUL_U24, DL, MVT::i32, N0, N1);
2427 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) {
2428 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
2429 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
2430 Mul = DAG.getNode(AMDGPUISD::MUL_I24, DL, MVT::i32, N0, N1);
2435 // We need to use sext even for MUL_U24, because MUL_U24 is used
2436 // for signed multiply of 8 and 16-bit types.
2437 return DAG.getSExtOrTrunc(Mul, DL, VT);
2440 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N,
2441 DAGCombinerInfo &DCI) const {
2442 SelectionDAG &DAG = DCI.DAG;
2445 switch(N->getOpcode()) {
2448 return performMulCombine(N, DCI);
2449 case AMDGPUISD::MUL_I24:
2450 case AMDGPUISD::MUL_U24: {
2451 SDValue N0 = N->getOperand(0);
2452 SDValue N1 = N->getOperand(1);
2453 simplifyI24(N0, DCI);
2454 simplifyI24(N1, DCI);
2458 SDValue Cond = N->getOperand(0);
2459 if (Cond.getOpcode() == ISD::SETCC && Cond.hasOneUse()) {
2460 EVT VT = N->getValueType(0);
2461 SDValue LHS = Cond.getOperand(0);
2462 SDValue RHS = Cond.getOperand(1);
2463 SDValue CC = Cond.getOperand(2);
2465 SDValue True = N->getOperand(1);
2466 SDValue False = N->getOperand(2);
2469 return CombineFMinMaxLegacy(DL, VT, LHS, RHS, True, False, CC, DCI);
2471 // TODO: Implement min / max Evergreen instructions.
2472 if (VT == MVT::i32 &&
2473 Subtarget->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
2474 return CombineIMinMax(DL, VT, LHS, RHS, True, False, CC, DAG);
2480 case AMDGPUISD::BFE_I32:
2481 case AMDGPUISD::BFE_U32: {
2482 assert(!N->getValueType(0).isVector() &&
2483 "Vector handling of BFE not implemented");
2484 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
2488 uint32_t WidthVal = Width->getZExtValue() & 0x1f;
2490 return DAG.getConstant(0, DL, MVT::i32);
2492 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
2496 SDValue BitsFrom = N->getOperand(0);
2497 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f;
2499 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32;
2501 if (OffsetVal == 0) {
2502 // This is already sign / zero extended, so try to fold away extra BFEs.
2503 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal);
2505 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom);
2506 if (OpSignBits >= SignBits)
2509 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal);
2511 // This is a sign_extend_inreg. Replace it to take advantage of existing
2512 // DAG Combines. If not eliminated, we will match back to BFE during
2515 // TODO: The sext_inreg of extended types ends, although we can could
2516 // handle them in a single BFE.
2517 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom,
2518 DAG.getValueType(SmallVT));
2521 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT);
2524 if (ConstantSDNode *CVal = dyn_cast<ConstantSDNode>(BitsFrom)) {
2526 return constantFoldBFE<int32_t>(DAG,
2527 CVal->getSExtValue(),
2533 return constantFoldBFE<uint32_t>(DAG,
2534 CVal->getZExtValue(),
2540 if ((OffsetVal + WidthVal) >= 32) {
2541 SDValue ShiftVal = DAG.getConstant(OffsetVal, DL, MVT::i32);
2542 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32,
2543 BitsFrom, ShiftVal);
2546 if (BitsFrom.hasOneUse()) {
2547 APInt Demanded = APInt::getBitsSet(32,
2549 OffsetVal + WidthVal);
2551 APInt KnownZero, KnownOne;
2552 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
2553 !DCI.isBeforeLegalizeOps());
2554 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
2555 if (TLO.ShrinkDemandedConstant(BitsFrom, Demanded) ||
2556 TLI.SimplifyDemandedBits(BitsFrom, Demanded,
2557 KnownZero, KnownOne, TLO)) {
2558 DCI.CommitTargetLoweringOpt(TLO);
2566 return performStoreCombine(N, DCI);
2571 //===----------------------------------------------------------------------===//
2573 //===----------------------------------------------------------------------===//
2575 void AMDGPUTargetLowering::getOriginalFunctionArgs(
2578 const SmallVectorImpl<ISD::InputArg> &Ins,
2579 SmallVectorImpl<ISD::InputArg> &OrigIns) const {
2581 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
2582 if (Ins[i].ArgVT == Ins[i].VT) {
2583 OrigIns.push_back(Ins[i]);
2588 if (Ins[i].ArgVT.isVector() && !Ins[i].VT.isVector()) {
2589 // Vector has been split into scalars.
2590 VT = Ins[i].ArgVT.getVectorElementType();
2591 } else if (Ins[i].VT.isVector() && Ins[i].ArgVT.isVector() &&
2592 Ins[i].ArgVT.getVectorElementType() !=
2593 Ins[i].VT.getVectorElementType()) {
2594 // Vector elements have been promoted
2597 // Vector has been spilt into smaller vectors.
2601 ISD::InputArg Arg(Ins[i].Flags, VT, VT, Ins[i].Used,
2602 Ins[i].OrigArgIndex, Ins[i].PartOffset);
2603 OrigIns.push_back(Arg);
2607 bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
2608 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
2609 return CFP->isExactlyValue(1.0);
2611 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
2612 return C->isAllOnesValue();
2617 bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
2618 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
2619 return CFP->getValueAPF().isZero();
2621 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
2622 return C->isNullValue();
2627 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2628 const TargetRegisterClass *RC,
2629 unsigned Reg, EVT VT) const {
2630 MachineFunction &MF = DAG.getMachineFunction();
2631 MachineRegisterInfo &MRI = MF.getRegInfo();
2632 unsigned VirtualRegister;
2633 if (!MRI.isLiveIn(Reg)) {
2634 VirtualRegister = MRI.createVirtualRegister(RC);
2635 MRI.addLiveIn(Reg, VirtualRegister);
2637 VirtualRegister = MRI.getLiveInVirtReg(Reg);
2639 return DAG.getRegister(VirtualRegister, VT);
2642 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
2644 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
2645 switch ((AMDGPUISD::NodeType)Opcode) {
2646 case AMDGPUISD::FIRST_NUMBER: break;
2648 NODE_NAME_CASE(CALL);
2649 NODE_NAME_CASE(UMUL);
2650 NODE_NAME_CASE(RET_FLAG);
2651 NODE_NAME_CASE(BRANCH_COND);
2654 NODE_NAME_CASE(DWORDADDR)
2655 NODE_NAME_CASE(FRACT)
2656 NODE_NAME_CASE(CLAMP)
2657 NODE_NAME_CASE(COS_HW)
2658 NODE_NAME_CASE(SIN_HW)
2659 NODE_NAME_CASE(FMAX_LEGACY)
2660 NODE_NAME_CASE(SMAX)
2661 NODE_NAME_CASE(UMAX)
2662 NODE_NAME_CASE(FMIN_LEGACY)
2663 NODE_NAME_CASE(SMIN)
2664 NODE_NAME_CASE(UMIN)
2665 NODE_NAME_CASE(FMAX3)
2666 NODE_NAME_CASE(SMAX3)
2667 NODE_NAME_CASE(UMAX3)
2668 NODE_NAME_CASE(FMIN3)
2669 NODE_NAME_CASE(SMIN3)
2670 NODE_NAME_CASE(UMIN3)
2671 NODE_NAME_CASE(URECIP)
2672 NODE_NAME_CASE(DIV_SCALE)
2673 NODE_NAME_CASE(DIV_FMAS)
2674 NODE_NAME_CASE(DIV_FIXUP)
2675 NODE_NAME_CASE(TRIG_PREOP)
2678 NODE_NAME_CASE(RSQ_LEGACY)
2679 NODE_NAME_CASE(RSQ_CLAMPED)
2680 NODE_NAME_CASE(LDEXP)
2681 NODE_NAME_CASE(FP_CLASS)
2682 NODE_NAME_CASE(DOT4)
2683 NODE_NAME_CASE(CARRY)
2684 NODE_NAME_CASE(BORROW)
2685 NODE_NAME_CASE(BFE_U32)
2686 NODE_NAME_CASE(BFE_I32)
2689 NODE_NAME_CASE(BREV)
2690 NODE_NAME_CASE(MUL_U24)
2691 NODE_NAME_CASE(MUL_I24)
2692 NODE_NAME_CASE(MAD_U24)
2693 NODE_NAME_CASE(MAD_I24)
2694 NODE_NAME_CASE(TEXTURE_FETCH)
2695 NODE_NAME_CASE(EXPORT)
2696 NODE_NAME_CASE(CONST_ADDRESS)
2697 NODE_NAME_CASE(REGISTER_LOAD)
2698 NODE_NAME_CASE(REGISTER_STORE)
2699 NODE_NAME_CASE(LOAD_CONSTANT)
2700 NODE_NAME_CASE(LOAD_INPUT)
2701 NODE_NAME_CASE(SAMPLE)
2702 NODE_NAME_CASE(SAMPLEB)
2703 NODE_NAME_CASE(SAMPLED)
2704 NODE_NAME_CASE(SAMPLEL)
2705 NODE_NAME_CASE(CVT_F32_UBYTE0)
2706 NODE_NAME_CASE(CVT_F32_UBYTE1)
2707 NODE_NAME_CASE(CVT_F32_UBYTE2)
2708 NODE_NAME_CASE(CVT_F32_UBYTE3)
2709 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR)
2710 NODE_NAME_CASE(CONST_DATA_PTR)
2711 case AMDGPUISD::FIRST_MEM_OPCODE_NUMBER: break;
2712 NODE_NAME_CASE(SENDMSG)
2713 NODE_NAME_CASE(INTERP_MOV)
2714 NODE_NAME_CASE(INTERP_P1)
2715 NODE_NAME_CASE(INTERP_P2)
2716 NODE_NAME_CASE(STORE_MSKOR)
2717 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
2718 case AMDGPUISD::LAST_AMDGPU_ISD_NUMBER: break;
2723 SDValue AMDGPUTargetLowering::getRsqrtEstimate(SDValue Operand,
2724 DAGCombinerInfo &DCI,
2725 unsigned &RefinementSteps,
2726 bool &UseOneConstNR) const {
2727 SelectionDAG &DAG = DCI.DAG;
2728 EVT VT = Operand.getValueType();
2730 if (VT == MVT::f32) {
2731 RefinementSteps = 0;
2732 return DAG.getNode(AMDGPUISD::RSQ, SDLoc(Operand), VT, Operand);
2735 // TODO: There is also f64 rsq instruction, but the documentation is less
2736 // clear on its precision.
2741 SDValue AMDGPUTargetLowering::getRecipEstimate(SDValue Operand,
2742 DAGCombinerInfo &DCI,
2743 unsigned &RefinementSteps) const {
2744 SelectionDAG &DAG = DCI.DAG;
2745 EVT VT = Operand.getValueType();
2747 if (VT == MVT::f32) {
2748 // Reciprocal, < 1 ulp error.
2750 // This reciprocal approximation converges to < 0.5 ulp error with one
2751 // newton rhapson performed with two fused multiple adds (FMAs).
2753 RefinementSteps = 0;
2754 return DAG.getNode(AMDGPUISD::RCP, SDLoc(Operand), VT, Operand);
2757 // TODO: There is also f64 rcp instruction, but the documentation is less
2758 // clear on its precision.
2763 static void computeKnownBitsForMinMax(const SDValue Op0,
2767 const SelectionDAG &DAG,
2769 APInt Op0Zero, Op0One;
2770 APInt Op1Zero, Op1One;
2771 DAG.computeKnownBits(Op0, Op0Zero, Op0One, Depth);
2772 DAG.computeKnownBits(Op1, Op1Zero, Op1One, Depth);
2774 KnownZero = Op0Zero & Op1Zero;
2775 KnownOne = Op0One & Op1One;
2778 void AMDGPUTargetLowering::computeKnownBitsForTargetNode(
2782 const SelectionDAG &DAG,
2783 unsigned Depth) const {
2785 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything.
2789 unsigned Opc = Op.getOpcode();
2794 case ISD::INTRINSIC_WO_CHAIN: {
2795 // FIXME: The intrinsic should just use the node.
2796 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
2797 case AMDGPUIntrinsic::AMDGPU_imax:
2798 case AMDGPUIntrinsic::AMDGPU_umax:
2799 case AMDGPUIntrinsic::AMDGPU_imin:
2800 case AMDGPUIntrinsic::AMDGPU_umin:
2801 computeKnownBitsForMinMax(Op.getOperand(1), Op.getOperand(2),
2802 KnownZero, KnownOne, DAG, Depth);
2810 case AMDGPUISD::SMAX:
2811 case AMDGPUISD::UMAX:
2812 case AMDGPUISD::SMIN:
2813 case AMDGPUISD::UMIN:
2814 computeKnownBitsForMinMax(Op.getOperand(0), Op.getOperand(1),
2815 KnownZero, KnownOne, DAG, Depth);
2818 case AMDGPUISD::CARRY:
2819 case AMDGPUISD::BORROW: {
2820 KnownZero = APInt::getHighBitsSet(32, 31);
2824 case AMDGPUISD::BFE_I32:
2825 case AMDGPUISD::BFE_U32: {
2826 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2830 unsigned BitWidth = 32;
2831 uint32_t Width = CWidth->getZExtValue() & 0x1f;
2833 if (Opc == AMDGPUISD::BFE_U32)
2834 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - Width);
2841 unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode(
2843 const SelectionDAG &DAG,
2844 unsigned Depth) const {
2845 switch (Op.getOpcode()) {
2846 case AMDGPUISD::BFE_I32: {
2847 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2851 unsigned SignBits = 32 - Width->getZExtValue() + 1;
2852 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2853 if (!Offset || !Offset->isNullValue())
2856 // TODO: Could probably figure something out with non-0 offsets.
2857 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1);
2858 return std::max(SignBits, Op0SignBits);
2861 case AMDGPUISD::BFE_U32: {
2862 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2863 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1;
2866 case AMDGPUISD::CARRY:
2867 case AMDGPUISD::BORROW: