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 "AMDGPURegisterInfo.h"
20 #include "AMDGPUSubtarget.h"
21 #include "AMDILIntrinsicInfo.h"
22 #include "R600MachineFunctionInfo.h"
23 #include "SIMachineFunctionInfo.h"
24 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/CodeGen/CallingConvLower.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/SelectionDAG.h"
29 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DiagnosticInfo.h"
32 #include "llvm/IR/DiagnosticPrinter.h"
38 /// Diagnostic information for unimplemented or unsupported feature reporting.
39 class DiagnosticInfoUnsupported : public DiagnosticInfo {
41 const Twine &Description;
46 static int getKindID() {
48 KindID = llvm::getNextAvailablePluginDiagnosticKind();
53 DiagnosticInfoUnsupported(const Function &Fn, const Twine &Desc,
54 DiagnosticSeverity Severity = DS_Error)
55 : DiagnosticInfo(getKindID(), Severity),
59 const Function &getFunction() const { return Fn; }
60 const Twine &getDescription() const { return Description; }
62 void print(DiagnosticPrinter &DP) const override {
63 DP << "unsupported " << getDescription() << " in " << Fn.getName();
66 static bool classof(const DiagnosticInfo *DI) {
67 return DI->getKind() == getKindID();
71 int DiagnosticInfoUnsupported::KindID = 0;
75 static bool allocateStack(unsigned ValNo, MVT ValVT, MVT LocVT,
76 CCValAssign::LocInfo LocInfo,
77 ISD::ArgFlagsTy ArgFlags, CCState &State) {
78 unsigned Offset = State.AllocateStack(ValVT.getStoreSize(),
79 ArgFlags.getOrigAlign());
80 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
85 #include "AMDGPUGenCallingConv.inc"
87 // Find a larger type to do a load / store of a vector with.
88 EVT AMDGPUTargetLowering::getEquivalentMemType(LLVMContext &Ctx, EVT VT) {
89 unsigned StoreSize = VT.getStoreSizeInBits();
91 return EVT::getIntegerVT(Ctx, StoreSize);
93 assert(StoreSize % 32 == 0 && "Store size not a multiple of 32");
94 return EVT::getVectorVT(Ctx, MVT::i32, StoreSize / 32);
97 // Type for a vector that will be loaded to.
98 EVT AMDGPUTargetLowering::getEquivalentLoadRegType(LLVMContext &Ctx, EVT VT) {
99 unsigned StoreSize = VT.getStoreSizeInBits();
101 return EVT::getIntegerVT(Ctx, 32);
103 return EVT::getVectorVT(Ctx, MVT::i32, StoreSize / 32);
106 AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM) :
107 TargetLowering(TM, new TargetLoweringObjectFileELF()) {
109 Subtarget = &TM.getSubtarget<AMDGPUSubtarget>();
111 // Initialize target lowering borrowed from AMDIL
114 // We need to custom lower some of the intrinsics
115 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
117 // Library functions. These default to Expand, but we have instructions
119 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
120 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
121 setOperationAction(ISD::FPOW, MVT::f32, Legal);
122 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
123 setOperationAction(ISD::FABS, MVT::f32, Legal);
124 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
125 setOperationAction(ISD::FRINT, MVT::f32, Legal);
126 setOperationAction(ISD::FROUND, MVT::f32, Legal);
127 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
129 // Lower floating point store/load to integer store/load to reduce the number
130 // of patterns in tablegen.
131 setOperationAction(ISD::STORE, MVT::f32, Promote);
132 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
134 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
135 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
137 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
138 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
140 setOperationAction(ISD::STORE, MVT::v8f32, Promote);
141 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
143 setOperationAction(ISD::STORE, MVT::v16f32, Promote);
144 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
146 setOperationAction(ISD::STORE, MVT::f64, Promote);
147 AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64);
149 setOperationAction(ISD::STORE, MVT::v2f64, Promote);
150 AddPromotedToType(ISD::STORE, MVT::v2f64, MVT::v2i64);
152 // Custom lowering of vector stores is required for local address space
154 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
155 // XXX: Native v2i32 local address space stores are possible, but not
156 // currently implemented.
157 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
159 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
160 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
161 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
163 // XXX: This can be change to Custom, once ExpandVectorStores can
164 // handle 64-bit stores.
165 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
167 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
168 setTruncStoreAction(MVT::i64, MVT::i8, Expand);
169 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
170 setTruncStoreAction(MVT::v2i64, MVT::v2i1, Expand);
171 setTruncStoreAction(MVT::v4i64, MVT::v4i1, Expand);
174 setOperationAction(ISD::LOAD, MVT::f32, Promote);
175 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
177 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
178 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
180 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
181 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
183 setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
184 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
186 setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
187 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
189 setOperationAction(ISD::LOAD, MVT::f64, Promote);
190 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64);
192 setOperationAction(ISD::LOAD, MVT::v2f64, Promote);
193 AddPromotedToType(ISD::LOAD, MVT::v2f64, MVT::v2i64);
195 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
196 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
197 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
198 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
199 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
200 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
201 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f32, Custom);
202 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
203 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f32, Custom);
204 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i32, Custom);
206 setLoadExtAction(ISD::EXTLOAD, MVT::v2i8, Expand);
207 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i8, Expand);
208 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i8, Expand);
209 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
210 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i8, Expand);
211 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i8, Expand);
212 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, Expand);
213 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, Expand);
214 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, Expand);
215 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, Expand);
216 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, Expand);
217 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, Expand);
219 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
221 if (Subtarget->getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
222 setOperationAction(ISD::FCEIL, MVT::f64, Custom);
223 setOperationAction(ISD::FTRUNC, MVT::f64, Custom);
224 setOperationAction(ISD::FRINT, MVT::f64, Custom);
225 setOperationAction(ISD::FFLOOR, MVT::f64, Custom);
228 if (!Subtarget->hasBFI()) {
229 // fcopysign can be done in a single instruction with BFI.
230 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
231 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
234 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
235 for (MVT VT : ScalarIntVTs) {
236 setOperationAction(ISD::SREM, VT, Expand);
237 setOperationAction(ISD::SDIV, VT, Custom);
239 // GPU does not have divrem function for signed or unsigned.
240 setOperationAction(ISD::SDIVREM, VT, Expand);
241 setOperationAction(ISD::UDIVREM, VT, Custom);
243 // GPU does not have [S|U]MUL_LOHI functions as a single instruction.
244 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
245 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
247 setOperationAction(ISD::BSWAP, VT, Expand);
248 setOperationAction(ISD::CTTZ, VT, Expand);
249 setOperationAction(ISD::CTLZ, VT, Expand);
252 if (!Subtarget->hasBCNT(32))
253 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
255 if (!Subtarget->hasBCNT(64))
256 setOperationAction(ISD::CTPOP, MVT::i64, Expand);
258 // The hardware supports 32-bit ROTR, but not ROTL.
259 setOperationAction(ISD::ROTL, MVT::i32, Expand);
260 setOperationAction(ISD::ROTL, MVT::i64, Expand);
261 setOperationAction(ISD::ROTR, MVT::i64, Expand);
263 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Expand);
264 setOperationAction(ISD::MUL, MVT::i64, Expand);
265 setOperationAction(ISD::MULHU, MVT::i64, Expand);
266 setOperationAction(ISD::MULHS, MVT::i64, Expand);
267 setOperationAction(ISD::SUB, MVT::i64, Expand);
268 setOperationAction(ISD::UDIV, MVT::i32, Expand);
269 setOperationAction(ISD::UREM, MVT::i32, Expand);
270 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
271 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
273 static const MVT::SimpleValueType VectorIntTypes[] = {
274 MVT::v2i32, MVT::v4i32
277 for (MVT VT : VectorIntTypes) {
278 // Expand the following operations for the current type by default.
279 setOperationAction(ISD::ADD, VT, Expand);
280 setOperationAction(ISD::AND, VT, Expand);
281 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
282 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
283 setOperationAction(ISD::MUL, VT, Expand);
284 setOperationAction(ISD::OR, VT, Expand);
285 setOperationAction(ISD::SHL, VT, Expand);
286 setOperationAction(ISD::SRA, VT, Expand);
287 setOperationAction(ISD::SRL, VT, Expand);
288 setOperationAction(ISD::ROTL, VT, Expand);
289 setOperationAction(ISD::ROTR, VT, Expand);
290 setOperationAction(ISD::SUB, VT, Expand);
291 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
292 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
293 // TODO: Implement custom UREM / SREM routines.
294 setOperationAction(ISD::SDIV, VT, Custom);
295 setOperationAction(ISD::UDIV, VT, Expand);
296 setOperationAction(ISD::SREM, VT, Expand);
297 setOperationAction(ISD::UREM, VT, Expand);
298 setOperationAction(ISD::SMUL_LOHI, VT, Expand);
299 setOperationAction(ISD::UMUL_LOHI, VT, Expand);
300 setOperationAction(ISD::SDIVREM, VT, Expand);
301 setOperationAction(ISD::UDIVREM, VT, Custom);
302 setOperationAction(ISD::SELECT, VT, Expand);
303 setOperationAction(ISD::VSELECT, VT, Expand);
304 setOperationAction(ISD::XOR, VT, Expand);
305 setOperationAction(ISD::BSWAP, VT, Expand);
306 setOperationAction(ISD::CTPOP, VT, Expand);
307 setOperationAction(ISD::CTTZ, VT, Expand);
308 setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
309 setOperationAction(ISD::CTLZ, VT, Expand);
310 setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
313 static const MVT::SimpleValueType FloatVectorTypes[] = {
314 MVT::v2f32, MVT::v4f32
317 for (MVT VT : FloatVectorTypes) {
318 setOperationAction(ISD::FABS, VT, Expand);
319 setOperationAction(ISD::FADD, VT, Expand);
320 setOperationAction(ISD::FCEIL, VT, Expand);
321 setOperationAction(ISD::FCOS, VT, Expand);
322 setOperationAction(ISD::FDIV, VT, Expand);
323 setOperationAction(ISD::FPOW, VT, Expand);
324 setOperationAction(ISD::FFLOOR, VT, Expand);
325 setOperationAction(ISD::FTRUNC, VT, Expand);
326 setOperationAction(ISD::FMUL, VT, Expand);
327 setOperationAction(ISD::FRINT, VT, Expand);
328 setOperationAction(ISD::FNEARBYINT, VT, Expand);
329 setOperationAction(ISD::FSQRT, VT, Expand);
330 setOperationAction(ISD::FSIN, VT, Expand);
331 setOperationAction(ISD::FSUB, VT, Expand);
332 setOperationAction(ISD::FNEG, VT, Expand);
333 setOperationAction(ISD::SELECT, VT, Expand);
334 setOperationAction(ISD::VSELECT, VT, Expand);
335 setOperationAction(ISD::FCOPYSIGN, VT, Expand);
338 setOperationAction(ISD::FNEARBYINT, MVT::f32, Custom);
339 setOperationAction(ISD::FNEARBYINT, MVT::f64, Custom);
341 setTargetDAGCombine(ISD::MUL);
342 setTargetDAGCombine(ISD::SELECT_CC);
344 setSchedulingPreference(Sched::RegPressure);
345 setJumpIsExpensive(true);
347 // There are no integer divide instructions, and these expand to a pretty
348 // large sequence of instructions.
349 setIntDivIsCheap(false);
351 // TODO: Investigate this when 64-bit divides are implemented.
352 addBypassSlowDiv(64, 32);
354 // FIXME: Need to really handle these.
355 MaxStoresPerMemcpy = 4096;
356 MaxStoresPerMemmove = 4096;
357 MaxStoresPerMemset = 4096;
360 //===----------------------------------------------------------------------===//
361 // Target Information
362 //===----------------------------------------------------------------------===//
364 MVT AMDGPUTargetLowering::getVectorIdxTy() const {
368 // The backend supports 32 and 64 bit floating point immediates.
369 // FIXME: Why are we reporting vectors of FP immediates as legal?
370 bool AMDGPUTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
371 EVT ScalarVT = VT.getScalarType();
372 return (ScalarVT == MVT::f32 || ScalarVT == MVT::f64);
375 // We don't want to shrink f64 / f32 constants.
376 bool AMDGPUTargetLowering::ShouldShrinkFPConstant(EVT VT) const {
377 EVT ScalarVT = VT.getScalarType();
378 return (ScalarVT != MVT::f32 && ScalarVT != MVT::f64);
381 bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy,
383 if (LoadTy.getSizeInBits() != CastTy.getSizeInBits())
386 unsigned LScalarSize = LoadTy.getScalarType().getSizeInBits();
387 unsigned CastScalarSize = CastTy.getScalarType().getSizeInBits();
389 return ((LScalarSize <= CastScalarSize) ||
390 (CastScalarSize >= 32) ||
394 //===---------------------------------------------------------------------===//
396 //===---------------------------------------------------------------------===//
398 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
399 assert(VT.isFloatingPoint());
400 return VT == MVT::f32;
403 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
404 assert(VT.isFloatingPoint());
405 return VT == MVT::f32;
408 bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const {
409 // Truncate is just accessing a subregister.
410 return Dest.bitsLT(Source) && (Dest.getSizeInBits() % 32 == 0);
413 bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const {
414 // Truncate is just accessing a subregister.
415 return Dest->getPrimitiveSizeInBits() < Source->getPrimitiveSizeInBits() &&
416 (Dest->getPrimitiveSizeInBits() % 32 == 0);
419 bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const {
420 const DataLayout *DL = getDataLayout();
421 unsigned SrcSize = DL->getTypeSizeInBits(Src->getScalarType());
422 unsigned DestSize = DL->getTypeSizeInBits(Dest->getScalarType());
424 return SrcSize == 32 && DestSize == 64;
427 bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const {
428 // Any register load of a 64-bit value really requires 2 32-bit moves. For all
429 // practical purposes, the extra mov 0 to load a 64-bit is free. As used,
430 // this will enable reducing 64-bit operations the 32-bit, which is always
432 return Src == MVT::i32 && Dest == MVT::i64;
435 bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const {
436 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a
437 // limited number of native 64-bit operations. Shrinking an operation to fit
438 // in a single 32-bit register should always be helpful. As currently used,
439 // this is much less general than the name suggests, and is only used in
440 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is
441 // not profitable, and may actually be harmful.
442 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32;
445 //===---------------------------------------------------------------------===//
446 // TargetLowering Callbacks
447 //===---------------------------------------------------------------------===//
449 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
450 const SmallVectorImpl<ISD::InputArg> &Ins) const {
452 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
455 SDValue AMDGPUTargetLowering::LowerReturn(
457 CallingConv::ID CallConv,
459 const SmallVectorImpl<ISD::OutputArg> &Outs,
460 const SmallVectorImpl<SDValue> &OutVals,
461 SDLoc DL, SelectionDAG &DAG) const {
462 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
465 //===---------------------------------------------------------------------===//
466 // Target specific lowering
467 //===---------------------------------------------------------------------===//
469 SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI,
470 SmallVectorImpl<SDValue> &InVals) const {
471 SDValue Callee = CLI.Callee;
472 SelectionDAG &DAG = CLI.DAG;
474 const Function &Fn = *DAG.getMachineFunction().getFunction();
476 StringRef FuncName("<unknown>");
478 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee))
479 FuncName = G->getSymbol();
480 else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
481 FuncName = G->getGlobal()->getName();
483 DiagnosticInfoUnsupported NoCalls(Fn, "call to function " + FuncName);
484 DAG.getContext()->diagnose(NoCalls);
488 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op,
489 SelectionDAG &DAG) const {
490 switch (Op.getOpcode()) {
492 Op.getNode()->dump();
493 llvm_unreachable("Custom lowering code for this"
494 "instruction is not implemented yet!");
496 // AMDGPU DAG lowering.
497 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
498 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
499 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
500 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
501 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
502 case ISD::SDIV: return LowerSDIV(Op, DAG);
503 case ISD::SREM: return LowerSREM(Op, DAG);
504 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
505 case ISD::FCEIL: return LowerFCEIL(Op, DAG);
506 case ISD::FTRUNC: return LowerFTRUNC(Op, DAG);
507 case ISD::FRINT: return LowerFRINT(Op, DAG);
508 case ISD::FNEARBYINT: return LowerFNEARBYINT(Op, DAG);
509 case ISD::FFLOOR: return LowerFFLOOR(Op, DAG);
510 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
512 // AMDIL DAG lowering.
513 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
518 void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N,
519 SmallVectorImpl<SDValue> &Results,
520 SelectionDAG &DAG) const {
521 switch (N->getOpcode()) {
522 case ISD::SIGN_EXTEND_INREG:
523 // Different parts of legalization seem to interpret which type of
524 // sign_extend_inreg is the one to check for custom lowering. The extended
525 // from type is what really matters, but some places check for custom
526 // lowering of the result type. This results in trying to use
527 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do
528 // nothing here and let the illegal result integer be handled normally.
531 SDValue Op = SDValue(N, 0);
533 EVT VT = Op.getValueType();
534 SDValue UDIVREM = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT),
535 N->getOperand(0), N->getOperand(1));
536 Results.push_back(UDIVREM);
540 SDValue Op = SDValue(N, 0);
542 EVT VT = Op.getValueType();
543 SDValue UDIVREM = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT),
544 N->getOperand(0), N->getOperand(1));
545 Results.push_back(UDIVREM.getValue(1));
549 SDValue Op = SDValue(N, 0);
551 EVT VT = Op.getValueType();
552 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
554 SDValue one = DAG.getConstant(1, HalfVT);
555 SDValue zero = DAG.getConstant(0, HalfVT);
558 SDValue LHS = N->getOperand(0);
559 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero);
560 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one);
562 SDValue RHS = N->getOperand(1);
563 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero);
564 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one);
566 // Get Speculative values
567 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo);
568 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo);
570 SDValue REM_Hi = zero;
571 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ);
573 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ);
574 SDValue DIV_Lo = zero;
576 const unsigned halfBitWidth = HalfVT.getSizeInBits();
578 for (unsigned i = 0; i < halfBitWidth; ++i) {
579 SDValue POS = DAG.getConstant(halfBitWidth - i - 1, HalfVT);
580 // Get Value of high bit
582 if (halfBitWidth == 32 && Subtarget->hasBFE()) {
583 HBit = DAG.getNode(AMDGPUISD::BFE_U32, DL, HalfVT, LHS_Lo, POS, one);
585 HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS);
586 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one);
589 SDValue Carry = DAG.getNode(ISD::SRL, DL, HalfVT, REM_Lo,
590 DAG.getConstant(halfBitWidth - 1, HalfVT));
591 REM_Hi = DAG.getNode(ISD::SHL, DL, HalfVT, REM_Hi, one);
592 REM_Hi = DAG.getNode(ISD::OR, DL, HalfVT, REM_Hi, Carry);
594 REM_Lo = DAG.getNode(ISD::SHL, DL, HalfVT, REM_Lo, one);
595 REM_Lo = DAG.getNode(ISD::OR, DL, HalfVT, REM_Lo, HBit);
598 SDValue REM = DAG.getNode(ISD::BUILD_PAIR, DL, VT, REM_Lo, REM_Hi);
600 SDValue BIT = DAG.getConstant(1 << (halfBitWidth - i - 1), HalfVT);
601 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETGE);
603 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT);
607 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS);
609 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETGE);
610 REM_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, REM, zero);
611 REM_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, REM, one);
614 SDValue REM = DAG.getNode(ISD::BUILD_PAIR, DL, VT, REM_Lo, REM_Hi);
615 SDValue DIV = DAG.getNode(ISD::BUILD_PAIR, DL, VT, DIV_Lo, DIV_Hi);
616 Results.push_back(DIV);
617 Results.push_back(REM);
625 // FIXME: This implements accesses to initialized globals in the constant
626 // address space by copying them to private and accessing that. It does not
627 // properly handle illegal types or vectors. The private vector loads are not
628 // scalarized, and the illegal scalars hit an assertion. This technique will not
629 // work well with large initializers, and this should eventually be
630 // removed. Initialized globals should be placed into a data section that the
631 // runtime will load into a buffer before the kernel is executed. Uses of the
632 // global need to be replaced with a pointer loaded from an implicit kernel
633 // argument into this buffer holding the copy of the data, which will remove the
634 // need for any of this.
635 SDValue AMDGPUTargetLowering::LowerConstantInitializer(const Constant* Init,
636 const GlobalValue *GV,
637 const SDValue &InitPtr,
639 SelectionDAG &DAG) const {
640 const DataLayout *TD = getTargetMachine().getDataLayout();
642 Type *InitTy = Init->getType();
644 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Init)) {
645 EVT VT = EVT::getEVT(InitTy);
646 PointerType *PtrTy = PointerType::get(InitTy, AMDGPUAS::PRIVATE_ADDRESS);
647 return DAG.getStore(Chain, DL, DAG.getConstant(*CI, VT), InitPtr,
648 MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
649 TD->getPrefTypeAlignment(InitTy));
652 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Init)) {
653 EVT VT = EVT::getEVT(CFP->getType());
654 PointerType *PtrTy = PointerType::get(CFP->getType(), 0);
655 return DAG.getStore(Chain, DL, DAG.getConstantFP(*CFP, VT), InitPtr,
656 MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
657 TD->getPrefTypeAlignment(CFP->getType()));
660 if (StructType *ST = dyn_cast<StructType>(InitTy)) {
661 const StructLayout *SL = TD->getStructLayout(ST);
663 EVT PtrVT = InitPtr.getValueType();
664 SmallVector<SDValue, 8> Chains;
666 for (unsigned I = 0, N = ST->getNumElements(); I != N; ++I) {
667 SDValue Offset = DAG.getConstant(SL->getElementOffset(I), PtrVT);
668 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, InitPtr, Offset);
670 Constant *Elt = Init->getAggregateElement(I);
671 Chains.push_back(LowerConstantInitializer(Elt, GV, Ptr, Chain, DAG));
674 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
677 if (SequentialType *SeqTy = dyn_cast<SequentialType>(InitTy)) {
678 EVT PtrVT = InitPtr.getValueType();
680 unsigned NumElements;
681 if (ArrayType *AT = dyn_cast<ArrayType>(SeqTy))
682 NumElements = AT->getNumElements();
683 else if (VectorType *VT = dyn_cast<VectorType>(SeqTy))
684 NumElements = VT->getNumElements();
686 llvm_unreachable("Unexpected type");
688 unsigned EltSize = TD->getTypeAllocSize(SeqTy->getElementType());
689 SmallVector<SDValue, 8> Chains;
690 for (unsigned i = 0; i < NumElements; ++i) {
691 SDValue Offset = DAG.getConstant(i * EltSize, PtrVT);
692 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, InitPtr, Offset);
694 Constant *Elt = Init->getAggregateElement(i);
695 Chains.push_back(LowerConstantInitializer(Elt, GV, Ptr, Chain, DAG));
698 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
701 if (isa<UndefValue>(Init)) {
702 EVT VT = EVT::getEVT(InitTy);
703 PointerType *PtrTy = PointerType::get(InitTy, AMDGPUAS::PRIVATE_ADDRESS);
704 return DAG.getStore(Chain, DL, DAG.getUNDEF(VT), InitPtr,
705 MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
706 TD->getPrefTypeAlignment(InitTy));
710 llvm_unreachable("Unhandled constant initializer");
713 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
715 SelectionDAG &DAG) const {
717 const DataLayout *TD = getTargetMachine().getDataLayout();
718 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
719 const GlobalValue *GV = G->getGlobal();
721 switch (G->getAddressSpace()) {
722 default: llvm_unreachable("Global Address lowering not implemented for this "
724 case AMDGPUAS::LOCAL_ADDRESS: {
725 // XXX: What does the value of G->getOffset() mean?
726 assert(G->getOffset() == 0 &&
727 "Do not know what to do with an non-zero offset");
730 if (MFI->LocalMemoryObjects.count(GV) == 0) {
731 uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
732 Offset = MFI->LDSSize;
733 MFI->LocalMemoryObjects[GV] = Offset;
734 // XXX: Account for alignment?
735 MFI->LDSSize += Size;
737 Offset = MFI->LocalMemoryObjects[GV];
740 return DAG.getConstant(Offset, getPointerTy(G->getAddressSpace()));
742 case AMDGPUAS::CONSTANT_ADDRESS: {
743 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
744 Type *EltType = GV->getType()->getElementType();
745 unsigned Size = TD->getTypeAllocSize(EltType);
746 unsigned Alignment = TD->getPrefTypeAlignment(EltType);
748 MVT PrivPtrVT = getPointerTy(AMDGPUAS::PRIVATE_ADDRESS);
749 MVT ConstPtrVT = getPointerTy(AMDGPUAS::CONSTANT_ADDRESS);
751 int FI = FrameInfo->CreateStackObject(Size, Alignment, false);
752 SDValue InitPtr = DAG.getFrameIndex(FI, PrivPtrVT);
754 const GlobalVariable *Var = cast<GlobalVariable>(GV);
755 if (!Var->hasInitializer()) {
756 // This has no use, but bugpoint will hit it.
757 return DAG.getZExtOrTrunc(InitPtr, SDLoc(Op), ConstPtrVT);
760 const Constant *Init = Var->getInitializer();
761 SmallVector<SDNode*, 8> WorkList;
763 for (SDNode::use_iterator I = DAG.getEntryNode()->use_begin(),
764 E = DAG.getEntryNode()->use_end(); I != E; ++I) {
765 if (I->getOpcode() != AMDGPUISD::REGISTER_LOAD && I->getOpcode() != ISD::LOAD)
767 WorkList.push_back(*I);
769 SDValue Chain = LowerConstantInitializer(Init, GV, InitPtr, DAG.getEntryNode(), DAG);
770 for (SmallVector<SDNode*, 8>::iterator I = WorkList.begin(),
771 E = WorkList.end(); I != E; ++I) {
772 SmallVector<SDValue, 8> Ops;
773 Ops.push_back(Chain);
774 for (unsigned i = 1; i < (*I)->getNumOperands(); ++i) {
775 Ops.push_back((*I)->getOperand(i));
777 DAG.UpdateNodeOperands(*I, Ops);
779 return DAG.getZExtOrTrunc(InitPtr, SDLoc(Op), ConstPtrVT);
784 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
785 SelectionDAG &DAG) const {
786 SmallVector<SDValue, 8> Args;
787 SDValue A = Op.getOperand(0);
788 SDValue B = Op.getOperand(1);
790 DAG.ExtractVectorElements(A, Args);
791 DAG.ExtractVectorElements(B, Args);
793 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(), Args);
796 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
797 SelectionDAG &DAG) const {
799 SmallVector<SDValue, 8> Args;
800 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
801 EVT VT = Op.getValueType();
802 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start,
803 VT.getVectorNumElements());
805 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(), Args);
808 SDValue AMDGPUTargetLowering::LowerFrameIndex(SDValue Op,
809 SelectionDAG &DAG) const {
811 MachineFunction &MF = DAG.getMachineFunction();
812 const AMDGPUFrameLowering *TFL =
813 static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
815 FrameIndexSDNode *FIN = cast<FrameIndexSDNode>(Op);
817 unsigned FrameIndex = FIN->getIndex();
818 unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
819 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF),
823 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
824 SelectionDAG &DAG) const {
825 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
827 EVT VT = Op.getValueType();
829 switch (IntrinsicID) {
831 case AMDGPUIntrinsic::AMDGPU_abs:
832 case AMDGPUIntrinsic::AMDIL_abs: // Legacy name.
833 return LowerIntrinsicIABS(Op, DAG);
834 case AMDGPUIntrinsic::AMDGPU_lrp:
835 return LowerIntrinsicLRP(Op, DAG);
836 case AMDGPUIntrinsic::AMDGPU_fract:
837 case AMDGPUIntrinsic::AMDIL_fraction: // Legacy name.
838 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
840 case AMDGPUIntrinsic::AMDGPU_clamp:
841 case AMDGPUIntrinsic::AMDIL_clamp: // Legacy name.
842 return DAG.getNode(AMDGPUISD::CLAMP, DL, VT,
843 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
845 case AMDGPUIntrinsic::AMDGPU_imax:
846 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
848 case AMDGPUIntrinsic::AMDGPU_umax:
849 return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
851 case AMDGPUIntrinsic::AMDGPU_imin:
852 return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
854 case AMDGPUIntrinsic::AMDGPU_umin:
855 return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
858 case AMDGPUIntrinsic::AMDGPU_umul24:
859 return DAG.getNode(AMDGPUISD::MUL_U24, DL, VT,
860 Op.getOperand(1), Op.getOperand(2));
862 case AMDGPUIntrinsic::AMDGPU_imul24:
863 return DAG.getNode(AMDGPUISD::MUL_I24, DL, VT,
864 Op.getOperand(1), Op.getOperand(2));
866 case AMDGPUIntrinsic::AMDGPU_umad24:
867 return DAG.getNode(AMDGPUISD::MAD_U24, DL, VT,
868 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
870 case AMDGPUIntrinsic::AMDGPU_imad24:
871 return DAG.getNode(AMDGPUISD::MAD_I24, DL, VT,
872 Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
874 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte0:
875 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE0, DL, VT, Op.getOperand(1));
877 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte1:
878 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE1, DL, VT, Op.getOperand(1));
880 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte2:
881 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE2, DL, VT, Op.getOperand(1));
883 case AMDGPUIntrinsic::AMDGPU_cvt_f32_ubyte3:
884 return DAG.getNode(AMDGPUISD::CVT_F32_UBYTE3, DL, VT, Op.getOperand(1));
886 case AMDGPUIntrinsic::AMDGPU_bfe_i32:
887 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT,
892 case AMDGPUIntrinsic::AMDGPU_bfe_u32:
893 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT,
898 case AMDGPUIntrinsic::AMDGPU_bfi:
899 return DAG.getNode(AMDGPUISD::BFI, DL, VT,
904 case AMDGPUIntrinsic::AMDGPU_bfm:
905 return DAG.getNode(AMDGPUISD::BFM, DL, VT,
909 case AMDGPUIntrinsic::AMDGPU_brev:
910 return DAG.getNode(AMDGPUISD::BREV, DL, VT, Op.getOperand(1));
912 case AMDGPUIntrinsic::AMDIL_exp: // Legacy name.
913 return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
915 case AMDGPUIntrinsic::AMDIL_round_nearest: // Legacy name.
916 return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
920 ///IABS(a) = SMAX(sub(0, a), a)
921 SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
922 SelectionDAG &DAG) const {
924 EVT VT = Op.getValueType();
925 SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
928 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
931 /// Linear Interpolation
932 /// LRP(a, b, c) = muladd(a, b, (1 - a) * c)
933 SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
934 SelectionDAG &DAG) const {
936 EVT VT = Op.getValueType();
937 SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
938 DAG.getConstantFP(1.0f, MVT::f32),
940 SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
942 return DAG.getNode(ISD::FADD, DL, VT,
943 DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
947 /// \brief Generate Min/Max node
948 SDValue AMDGPUTargetLowering::CombineMinMax(SDNode *N,
949 SelectionDAG &DAG) const {
951 EVT VT = N->getValueType(0);
953 SDValue LHS = N->getOperand(0);
954 SDValue RHS = N->getOperand(1);
955 SDValue True = N->getOperand(2);
956 SDValue False = N->getOperand(3);
957 SDValue CC = N->getOperand(4);
959 if (VT != MVT::f32 ||
960 !((LHS == True && RHS == False) || (LHS == False && RHS == True))) {
964 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
978 llvm_unreachable("Operation should already be optimised!");
985 unsigned Opc = (LHS == True) ? AMDGPUISD::FMIN : AMDGPUISD::FMAX;
986 return DAG.getNode(Opc, DL, VT, LHS, RHS);
994 unsigned Opc = (LHS == True) ? AMDGPUISD::FMAX : AMDGPUISD::FMIN;
995 return DAG.getNode(Opc, DL, VT, LHS, RHS);
997 case ISD::SETCC_INVALID:
998 llvm_unreachable("Invalid setcc condcode!");
1003 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue &Op,
1004 SelectionDAG &DAG) const {
1005 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
1006 EVT MemEltVT = Load->getMemoryVT().getVectorElementType();
1007 EVT EltVT = Op.getValueType().getVectorElementType();
1008 EVT PtrVT = Load->getBasePtr().getValueType();
1009 unsigned NumElts = Load->getMemoryVT().getVectorNumElements();
1010 SmallVector<SDValue, 8> Loads;
1013 for (unsigned i = 0, e = NumElts; i != e; ++i) {
1014 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, Load->getBasePtr(),
1015 DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8), PtrVT));
1016 Loads.push_back(DAG.getExtLoad(Load->getExtensionType(), SL, EltVT,
1017 Load->getChain(), Ptr,
1018 MachinePointerInfo(Load->getMemOperand()->getValue()),
1019 MemEltVT, Load->isVolatile(), Load->isNonTemporal(),
1020 Load->getAlignment()));
1022 return DAG.getNode(ISD::BUILD_VECTOR, SL, Op.getValueType(), Loads);
1025 SDValue AMDGPUTargetLowering::MergeVectorStore(const SDValue &Op,
1026 SelectionDAG &DAG) const {
1027 StoreSDNode *Store = cast<StoreSDNode>(Op);
1028 EVT MemVT = Store->getMemoryVT();
1029 unsigned MemBits = MemVT.getSizeInBits();
1031 // Byte stores are really expensive, so if possible, try to pack 32-bit vector
1032 // truncating store into an i32 store.
1033 // XXX: We could also handle optimize other vector bitwidths.
1034 if (!MemVT.isVector() || MemBits > 32) {
1039 SDValue Value = Store->getValue();
1040 EVT VT = Value.getValueType();
1041 EVT ElemVT = VT.getVectorElementType();
1042 SDValue Ptr = Store->getBasePtr();
1043 EVT MemEltVT = MemVT.getVectorElementType();
1044 unsigned MemEltBits = MemEltVT.getSizeInBits();
1045 unsigned MemNumElements = MemVT.getVectorNumElements();
1046 unsigned PackedSize = MemVT.getStoreSizeInBits();
1047 SDValue Mask = DAG.getConstant((1 << MemEltBits) - 1, MVT::i32);
1049 assert(Value.getValueType().getScalarSizeInBits() >= 32);
1051 SDValue PackedValue;
1052 for (unsigned i = 0; i < MemNumElements; ++i) {
1053 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT, Value,
1054 DAG.getConstant(i, MVT::i32));
1055 Elt = DAG.getZExtOrTrunc(Elt, DL, MVT::i32);
1056 Elt = DAG.getNode(ISD::AND, DL, MVT::i32, Elt, Mask); // getZeroExtendInReg
1058 SDValue Shift = DAG.getConstant(MemEltBits * i, MVT::i32);
1059 Elt = DAG.getNode(ISD::SHL, DL, MVT::i32, Elt, Shift);
1064 PackedValue = DAG.getNode(ISD::OR, DL, MVT::i32, PackedValue, Elt);
1068 if (PackedSize < 32) {
1069 EVT PackedVT = EVT::getIntegerVT(*DAG.getContext(), PackedSize);
1070 return DAG.getTruncStore(Store->getChain(), DL, PackedValue, Ptr,
1071 Store->getMemOperand()->getPointerInfo(),
1073 Store->isNonTemporal(), Store->isVolatile(),
1074 Store->getAlignment());
1077 return DAG.getStore(Store->getChain(), DL, PackedValue, Ptr,
1078 Store->getMemOperand()->getPointerInfo(),
1079 Store->isVolatile(), Store->isNonTemporal(),
1080 Store->getAlignment());
1083 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
1084 SelectionDAG &DAG) const {
1085 StoreSDNode *Store = cast<StoreSDNode>(Op);
1086 EVT MemEltVT = Store->getMemoryVT().getVectorElementType();
1087 EVT EltVT = Store->getValue().getValueType().getVectorElementType();
1088 EVT PtrVT = Store->getBasePtr().getValueType();
1089 unsigned NumElts = Store->getMemoryVT().getVectorNumElements();
1092 SmallVector<SDValue, 8> Chains;
1094 for (unsigned i = 0, e = NumElts; i != e; ++i) {
1095 SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
1096 Store->getValue(), DAG.getConstant(i, MVT::i32));
1097 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT,
1098 Store->getBasePtr(),
1099 DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8),
1101 Chains.push_back(DAG.getTruncStore(Store->getChain(), SL, Val, Ptr,
1102 MachinePointerInfo(Store->getMemOperand()->getValue()),
1103 MemEltVT, Store->isVolatile(), Store->isNonTemporal(),
1104 Store->getAlignment()));
1106 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, Chains);
1109 SDValue AMDGPUTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
1111 LoadSDNode *Load = cast<LoadSDNode>(Op);
1112 ISD::LoadExtType ExtType = Load->getExtensionType();
1113 EVT VT = Op.getValueType();
1114 EVT MemVT = Load->getMemoryVT();
1116 if (ExtType != ISD::NON_EXTLOAD && !VT.isVector() && VT.getSizeInBits() > 32) {
1117 // We can do the extload to 32-bits, and then need to separately extend to
1120 SDValue ExtLoad32 = DAG.getExtLoad(ExtType, DL, MVT::i32,
1124 Load->getMemOperand());
1125 return DAG.getNode(ISD::getExtForLoadExtType(ExtType), DL, VT, ExtLoad32);
1128 if (ExtType == ISD::NON_EXTLOAD && VT.getSizeInBits() < 32) {
1129 assert(VT == MVT::i1 && "Only i1 non-extloads expected");
1130 // FIXME: Copied from PPC
1131 // First, load into 32 bits, then truncate to 1 bit.
1133 SDValue Chain = Load->getChain();
1134 SDValue BasePtr = Load->getBasePtr();
1135 MachineMemOperand *MMO = Load->getMemOperand();
1137 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
1138 BasePtr, MVT::i8, MMO);
1139 return DAG.getNode(ISD::TRUNCATE, DL, VT, NewLD);
1142 // Lower loads constant address space global variable loads
1143 if (Load->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS &&
1144 isa<GlobalVariable>(
1145 GetUnderlyingObject(Load->getMemOperand()->getValue()))) {
1147 SDValue Ptr = DAG.getZExtOrTrunc(Load->getBasePtr(), DL,
1148 getPointerTy(AMDGPUAS::PRIVATE_ADDRESS));
1149 Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
1150 DAG.getConstant(2, MVT::i32));
1151 return DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
1152 Load->getChain(), Ptr,
1153 DAG.getTargetConstant(0, MVT::i32), Op.getOperand(2));
1156 if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS ||
1157 ExtType == ISD::NON_EXTLOAD || Load->getMemoryVT().bitsGE(MVT::i32))
1161 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
1162 DAG.getConstant(2, MVT::i32));
1163 SDValue Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
1164 Load->getChain(), Ptr,
1165 DAG.getTargetConstant(0, MVT::i32),
1167 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
1169 DAG.getConstant(0x3, MVT::i32));
1170 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1171 DAG.getConstant(3, MVT::i32));
1173 Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Ret, ShiftAmt);
1175 EVT MemEltVT = MemVT.getScalarType();
1176 if (ExtType == ISD::SEXTLOAD) {
1177 SDValue MemEltVTNode = DAG.getValueType(MemEltVT);
1178 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode);
1181 return DAG.getZeroExtendInReg(Ret, DL, MemEltVT);
1184 SDValue AMDGPUTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1186 SDValue Result = AMDGPUTargetLowering::MergeVectorStore(Op, DAG);
1187 if (Result.getNode()) {
1191 StoreSDNode *Store = cast<StoreSDNode>(Op);
1192 SDValue Chain = Store->getChain();
1193 if ((Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1194 Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) &&
1195 Store->getValue().getValueType().isVector()) {
1196 return SplitVectorStore(Op, DAG);
1199 EVT MemVT = Store->getMemoryVT();
1200 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS &&
1201 MemVT.bitsLT(MVT::i32)) {
1203 if (Store->getMemoryVT() == MVT::i8) {
1205 } else if (Store->getMemoryVT() == MVT::i16) {
1208 SDValue BasePtr = Store->getBasePtr();
1209 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, BasePtr,
1210 DAG.getConstant(2, MVT::i32));
1211 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
1212 Chain, Ptr, DAG.getTargetConstant(0, MVT::i32));
1214 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, BasePtr,
1215 DAG.getConstant(0x3, MVT::i32));
1217 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1218 DAG.getConstant(3, MVT::i32));
1220 SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
1223 SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT);
1225 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1226 MaskedValue, ShiftAmt);
1228 SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32, DAG.getConstant(Mask, MVT::i32),
1230 DstMask = DAG.getNode(ISD::XOR, DL, MVT::i32, DstMask,
1231 DAG.getConstant(0xffffffff, MVT::i32));
1232 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1234 SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
1235 return DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1236 Chain, Value, Ptr, DAG.getTargetConstant(0, MVT::i32));
1241 SDValue AMDGPUTargetLowering::LowerSDIV24(SDValue Op, SelectionDAG &DAG) const {
1243 EVT OVT = Op.getValueType();
1244 SDValue LHS = Op.getOperand(0);
1245 SDValue RHS = Op.getOperand(1);
1248 if (!OVT.isVector()) {
1251 } else if (OVT.getVectorNumElements() == 2) {
1254 } else if (OVT.getVectorNumElements() == 4) {
1258 unsigned bitsize = OVT.getScalarType().getSizeInBits();
1259 // char|short jq = ia ^ ib;
1260 SDValue jq = DAG.getNode(ISD::XOR, DL, OVT, LHS, RHS);
1262 // jq = jq >> (bitsize - 2)
1263 jq = DAG.getNode(ISD::SRA, DL, OVT, jq, DAG.getConstant(bitsize - 2, OVT));
1266 jq = DAG.getNode(ISD::OR, DL, OVT, jq, DAG.getConstant(1, OVT));
1269 jq = DAG.getSExtOrTrunc(jq, DL, INTTY);
1271 // int ia = (int)LHS;
1272 SDValue ia = DAG.getSExtOrTrunc(LHS, DL, INTTY);
1274 // int ib, (int)RHS;
1275 SDValue ib = DAG.getSExtOrTrunc(RHS, DL, INTTY);
1277 // float fa = (float)ia;
1278 SDValue fa = DAG.getNode(ISD::SINT_TO_FP, DL, FLTTY, ia);
1280 // float fb = (float)ib;
1281 SDValue fb = DAG.getNode(ISD::SINT_TO_FP, DL, FLTTY, ib);
1283 // float fq = native_divide(fa, fb);
1284 SDValue fq = DAG.getNode(AMDGPUISD::DIV_INF, DL, FLTTY, fa, fb);
1287 fq = DAG.getNode(ISD::FTRUNC, DL, FLTTY, fq);
1289 // float fqneg = -fq;
1290 SDValue fqneg = DAG.getNode(ISD::FNEG, DL, FLTTY, fq);
1292 // float fr = mad(fqneg, fb, fa);
1293 SDValue fr = DAG.getNode(ISD::FADD, DL, FLTTY,
1294 DAG.getNode(ISD::MUL, DL, FLTTY, fqneg, fb), fa);
1296 // int iq = (int)fq;
1297 SDValue iq = DAG.getNode(ISD::FP_TO_SINT, DL, INTTY, fq);
1300 fr = DAG.getNode(ISD::FABS, DL, FLTTY, fr);
1303 fb = DAG.getNode(ISD::FABS, DL, FLTTY, fb);
1305 // int cv = fr >= fb;
1307 if (INTTY == MVT::i32) {
1308 cv = DAG.getSetCC(DL, INTTY, fr, fb, ISD::SETOGE);
1310 cv = DAG.getSetCC(DL, INTTY, fr, fb, ISD::SETOGE);
1312 // jq = (cv ? jq : 0);
1313 jq = DAG.getNode(ISD::SELECT, DL, OVT, cv, jq,
1314 DAG.getConstant(0, OVT));
1316 iq = DAG.getSExtOrTrunc(iq, DL, OVT);
1317 iq = DAG.getNode(ISD::ADD, DL, OVT, iq, jq);
1321 SDValue AMDGPUTargetLowering::LowerSDIV32(SDValue Op, SelectionDAG &DAG) const {
1323 EVT OVT = Op.getValueType();
1324 SDValue LHS = Op.getOperand(0);
1325 SDValue RHS = Op.getOperand(1);
1326 // The LowerSDIV32 function generates equivalent to the following IL.
1336 // ixor r10, r10, r11
1338 // ixor DST, r0, r10
1347 SDValue r10 = DAG.getSelectCC(DL,
1348 r0, DAG.getConstant(0, OVT),
1349 DAG.getConstant(-1, OVT),
1350 DAG.getConstant(0, OVT),
1354 SDValue r11 = DAG.getSelectCC(DL,
1355 r1, DAG.getConstant(0, OVT),
1356 DAG.getConstant(-1, OVT),
1357 DAG.getConstant(0, OVT),
1361 r0 = DAG.getNode(ISD::ADD, DL, OVT, r0, r10);
1364 r1 = DAG.getNode(ISD::ADD, DL, OVT, r1, r11);
1367 r0 = DAG.getNode(ISD::XOR, DL, OVT, r0, r10);
1370 r1 = DAG.getNode(ISD::XOR, DL, OVT, r1, r11);
1373 r0 = DAG.getNode(ISD::UDIV, DL, OVT, r0, r1);
1375 // ixor r10, r10, r11
1376 r10 = DAG.getNode(ISD::XOR, DL, OVT, r10, r11);
1379 r0 = DAG.getNode(ISD::ADD, DL, OVT, r0, r10);
1381 // ixor DST, r0, r10
1382 SDValue DST = DAG.getNode(ISD::XOR, DL, OVT, r0, r10);
1386 SDValue AMDGPUTargetLowering::LowerSDIV64(SDValue Op, SelectionDAG &DAG) const {
1387 return SDValue(Op.getNode(), 0);
1390 SDValue AMDGPUTargetLowering::LowerSDIV(SDValue Op, SelectionDAG &DAG) const {
1391 EVT OVT = Op.getValueType().getScalarType();
1393 if (OVT == MVT::i64)
1394 return LowerSDIV64(Op, DAG);
1396 if (OVT.getScalarType() == MVT::i32)
1397 return LowerSDIV32(Op, DAG);
1399 if (OVT == MVT::i16 || OVT == MVT::i8) {
1400 // FIXME: We should be checking for the masked bits. This isn't reached
1401 // because i8 and i16 are not legal types.
1402 return LowerSDIV24(Op, DAG);
1405 return SDValue(Op.getNode(), 0);
1408 SDValue AMDGPUTargetLowering::LowerSREM32(SDValue Op, SelectionDAG &DAG) const {
1410 EVT OVT = Op.getValueType();
1411 SDValue LHS = Op.getOperand(0);
1412 SDValue RHS = Op.getOperand(1);
1413 // The LowerSREM32 function generates equivalent to the following IL.
1423 // umul r20, r20, r1
1426 // ixor DST, r0, r10
1435 SDValue r10 = DAG.getSetCC(DL, OVT, r0, DAG.getConstant(0, OVT), ISD::SETLT);
1438 SDValue r11 = DAG.getSetCC(DL, OVT, r1, DAG.getConstant(0, OVT), ISD::SETLT);
1441 r0 = DAG.getNode(ISD::ADD, DL, OVT, r0, r10);
1444 r1 = DAG.getNode(ISD::ADD, DL, OVT, r1, r11);
1447 r0 = DAG.getNode(ISD::XOR, DL, OVT, r0, r10);
1450 r1 = DAG.getNode(ISD::XOR, DL, OVT, r1, r11);
1453 SDValue r20 = DAG.getNode(ISD::UREM, DL, OVT, r0, r1);
1455 // umul r20, r20, r1
1456 r20 = DAG.getNode(AMDGPUISD::UMUL, DL, OVT, r20, r1);
1459 r0 = DAG.getNode(ISD::SUB, DL, OVT, r0, r20);
1462 r0 = DAG.getNode(ISD::ADD, DL, OVT, r0, r10);
1464 // ixor DST, r0, r10
1465 SDValue DST = DAG.getNode(ISD::XOR, DL, OVT, r0, r10);
1469 SDValue AMDGPUTargetLowering::LowerSREM64(SDValue Op, SelectionDAG &DAG) const {
1470 return SDValue(Op.getNode(), 0);
1473 SDValue AMDGPUTargetLowering::LowerSREM(SDValue Op, SelectionDAG &DAG) const {
1474 EVT OVT = Op.getValueType();
1476 if (OVT.getScalarType() == MVT::i64)
1477 return LowerSREM64(Op, DAG);
1479 if (OVT.getScalarType() == MVT::i32)
1480 return LowerSREM32(Op, DAG);
1482 return SDValue(Op.getNode(), 0);
1485 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
1486 SelectionDAG &DAG) const {
1488 EVT VT = Op.getValueType();
1490 SDValue Num = Op.getOperand(0);
1491 SDValue Den = Op.getOperand(1);
1493 // RCP = URECIP(Den) = 2^32 / Den + e
1494 // e is rounding error.
1495 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
1497 // RCP_LO = umulo(RCP, Den) */
1498 SDValue RCP_LO = DAG.getNode(ISD::UMULO, DL, VT, RCP, Den);
1500 // RCP_HI = mulhu (RCP, Den) */
1501 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
1503 // NEG_RCP_LO = -RCP_LO
1504 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
1507 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
1508 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
1511 // Calculate the rounding error from the URECIP instruction
1512 // E = mulhu(ABS_RCP_LO, RCP)
1513 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
1515 // RCP_A_E = RCP + E
1516 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
1518 // RCP_S_E = RCP - E
1519 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
1521 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
1522 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
1525 // Quotient = mulhu(Tmp0, Num)
1526 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
1528 // Num_S_Remainder = Quotient * Den
1529 SDValue Num_S_Remainder = DAG.getNode(ISD::UMULO, DL, VT, Quotient, Den);
1531 // Remainder = Num - Num_S_Remainder
1532 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
1534 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
1535 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
1536 DAG.getConstant(-1, VT),
1537 DAG.getConstant(0, VT),
1539 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
1540 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
1542 DAG.getConstant(-1, VT),
1543 DAG.getConstant(0, VT),
1545 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
1546 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
1549 // Calculate Division result:
1551 // Quotient_A_One = Quotient + 1
1552 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
1553 DAG.getConstant(1, VT));
1555 // Quotient_S_One = Quotient - 1
1556 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
1557 DAG.getConstant(1, VT));
1559 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
1560 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
1561 Quotient, Quotient_A_One, ISD::SETEQ);
1563 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
1564 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
1565 Quotient_S_One, Div, ISD::SETEQ);
1567 // Calculate Rem result:
1569 // Remainder_S_Den = Remainder - Den
1570 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
1572 // Remainder_A_Den = Remainder + Den
1573 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
1575 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
1576 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
1577 Remainder, Remainder_S_Den, ISD::SETEQ);
1579 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
1580 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
1581 Remainder_A_Den, Rem, ISD::SETEQ);
1586 return DAG.getMergeValues(Ops, DL);
1589 SDValue AMDGPUTargetLowering::LowerFCEIL(SDValue Op, SelectionDAG &DAG) const {
1591 SDValue Src = Op.getOperand(0);
1593 // result = trunc(src)
1594 // if (src > 0.0 && src != result)
1597 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1599 const SDValue Zero = DAG.getConstantFP(0.0, MVT::f64);
1600 const SDValue One = DAG.getConstantFP(1.0, MVT::f64);
1602 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f64);
1604 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOGT);
1605 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1606 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1608 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, One, Zero);
1609 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1612 SDValue AMDGPUTargetLowering::LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const {
1614 SDValue Src = Op.getOperand(0);
1616 assert(Op.getValueType() == MVT::f64);
1618 const SDValue Zero = DAG.getConstant(0, MVT::i32);
1619 const SDValue One = DAG.getConstant(1, MVT::i32);
1621 SDValue VecSrc = DAG.getNode(ISD::BITCAST, SL, MVT::v2i32, Src);
1623 // Extract the upper half, since this is where we will find the sign and
1625 SDValue Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, MVT::i32, VecSrc, One);
1627 const unsigned FractBits = 52;
1628 const unsigned ExpBits = 11;
1630 // Extract the exponent.
1631 SDValue ExpPart = DAG.getNode(AMDGPUISD::BFE_I32, SL, MVT::i32,
1633 DAG.getConstant(FractBits - 32, MVT::i32),
1634 DAG.getConstant(ExpBits, MVT::i32));
1635 SDValue Exp = DAG.getNode(ISD::SUB, SL, MVT::i32, ExpPart,
1636 DAG.getConstant(1023, MVT::i32));
1638 // Extract the sign bit.
1639 const SDValue SignBitMask = DAG.getConstant(UINT32_C(1) << 31, MVT::i32);
1640 SDValue SignBit = DAG.getNode(ISD::AND, SL, MVT::i32, Hi, SignBitMask);
1642 // Extend back to to 64-bits.
1643 SDValue SignBit64 = DAG.getNode(ISD::BUILD_VECTOR, SL, MVT::v2i32,
1645 SignBit64 = DAG.getNode(ISD::BITCAST, SL, MVT::i64, SignBit64);
1647 SDValue BcInt = DAG.getNode(ISD::BITCAST, SL, MVT::i64, Src);
1648 const SDValue FractMask
1649 = DAG.getConstant((UINT64_C(1) << FractBits) - 1, MVT::i64);
1651 SDValue Shr = DAG.getNode(ISD::SRA, SL, MVT::i64, FractMask, Exp);
1652 SDValue Not = DAG.getNOT(SL, Shr, MVT::i64);
1653 SDValue Tmp0 = DAG.getNode(ISD::AND, SL, MVT::i64, BcInt, Not);
1655 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::i32);
1657 const SDValue FiftyOne = DAG.getConstant(FractBits - 1, MVT::i32);
1659 SDValue ExpLt0 = DAG.getSetCC(SL, SetCCVT, Exp, Zero, ISD::SETLT);
1660 SDValue ExpGt51 = DAG.getSetCC(SL, SetCCVT, Exp, FiftyOne, ISD::SETGT);
1662 SDValue Tmp1 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpLt0, SignBit64, Tmp0);
1663 SDValue Tmp2 = DAG.getNode(ISD::SELECT, SL, MVT::i64, ExpGt51, BcInt, Tmp1);
1665 return DAG.getNode(ISD::BITCAST, SL, MVT::f64, Tmp2);
1668 SDValue AMDGPUTargetLowering::LowerFRINT(SDValue Op, SelectionDAG &DAG) const {
1670 SDValue Src = Op.getOperand(0);
1672 assert(Op.getValueType() == MVT::f64);
1674 APFloat C1Val(APFloat::IEEEdouble, "0x1.0p+52");
1675 SDValue C1 = DAG.getConstantFP(C1Val, MVT::f64);
1676 SDValue CopySign = DAG.getNode(ISD::FCOPYSIGN, SL, MVT::f64, C1, Src);
1678 SDValue Tmp1 = DAG.getNode(ISD::FADD, SL, MVT::f64, Src, CopySign);
1679 SDValue Tmp2 = DAG.getNode(ISD::FSUB, SL, MVT::f64, Tmp1, CopySign);
1681 SDValue Fabs = DAG.getNode(ISD::FABS, SL, MVT::f64, Src);
1683 APFloat C2Val(APFloat::IEEEdouble, "0x1.fffffffffffffp+51");
1684 SDValue C2 = DAG.getConstantFP(C2Val, MVT::f64);
1686 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f64);
1687 SDValue Cond = DAG.getSetCC(SL, SetCCVT, Fabs, C2, ISD::SETOGT);
1689 return DAG.getSelect(SL, MVT::f64, Cond, Src, Tmp2);
1692 SDValue AMDGPUTargetLowering::LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const {
1693 // FNEARBYINT and FRINT are the same, except in their handling of FP
1694 // exceptions. Those aren't really meaningful for us, and OpenCL only has
1695 // rint, so just treat them as equivalent.
1696 return DAG.getNode(ISD::FRINT, SDLoc(Op), Op.getValueType(), Op.getOperand(0));
1699 SDValue AMDGPUTargetLowering::LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const {
1701 SDValue Src = Op.getOperand(0);
1703 // result = trunc(src);
1704 // if (src < 0.0 && src != result)
1707 SDValue Trunc = DAG.getNode(ISD::FTRUNC, SL, MVT::f64, Src);
1709 const SDValue Zero = DAG.getConstantFP(0.0, MVT::f64);
1710 const SDValue NegOne = DAG.getConstantFP(-1.0, MVT::f64);
1712 EVT SetCCVT = getSetCCResultType(*DAG.getContext(), MVT::f64);
1714 SDValue Lt0 = DAG.getSetCC(SL, SetCCVT, Src, Zero, ISD::SETOLT);
1715 SDValue NeTrunc = DAG.getSetCC(SL, SetCCVT, Src, Trunc, ISD::SETONE);
1716 SDValue And = DAG.getNode(ISD::AND, SL, SetCCVT, Lt0, NeTrunc);
1718 SDValue Add = DAG.getNode(ISD::SELECT, SL, MVT::f64, And, NegOne, Zero);
1719 return DAG.getNode(ISD::FADD, SL, MVT::f64, Trunc, Add);
1722 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
1723 SelectionDAG &DAG) const {
1724 SDValue S0 = Op.getOperand(0);
1726 if (Op.getValueType() != MVT::f32 || S0.getValueType() != MVT::i64)
1729 // f32 uint_to_fp i64
1730 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
1731 DAG.getConstant(0, MVT::i32));
1732 SDValue FloatLo = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Lo);
1733 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
1734 DAG.getConstant(1, MVT::i32));
1735 SDValue FloatHi = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Hi);
1736 FloatHi = DAG.getNode(ISD::FMUL, DL, MVT::f32, FloatHi,
1737 DAG.getConstantFP(4294967296.0f, MVT::f32)); // 2^32
1738 return DAG.getNode(ISD::FADD, DL, MVT::f32, FloatLo, FloatHi);
1741 SDValue AMDGPUTargetLowering::ExpandSIGN_EXTEND_INREG(SDValue Op,
1743 SelectionDAG &DAG) const {
1744 MVT VT = Op.getSimpleValueType();
1746 SDValue Shift = DAG.getConstant(BitsDiff, VT);
1747 // Shift left by 'Shift' bits.
1748 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Op.getOperand(0), Shift);
1749 // Signed shift Right by 'Shift' bits.
1750 return DAG.getNode(ISD::SRA, DL, VT, Shl, Shift);
1753 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
1754 SelectionDAG &DAG) const {
1755 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1756 MVT VT = Op.getSimpleValueType();
1757 MVT ScalarVT = VT.getScalarType();
1762 SDValue Src = Op.getOperand(0);
1765 // TODO: Don't scalarize on Evergreen?
1766 unsigned NElts = VT.getVectorNumElements();
1767 SmallVector<SDValue, 8> Args;
1768 DAG.ExtractVectorElements(Src, Args, 0, NElts);
1770 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType());
1771 for (unsigned I = 0; I < NElts; ++I)
1772 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp);
1774 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Args);
1777 //===----------------------------------------------------------------------===//
1778 // Custom DAG optimizations
1779 //===----------------------------------------------------------------------===//
1781 static bool isU24(SDValue Op, SelectionDAG &DAG) {
1782 APInt KnownZero, KnownOne;
1783 EVT VT = Op.getValueType();
1784 DAG.computeKnownBits(Op, KnownZero, KnownOne);
1786 return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24;
1789 static bool isI24(SDValue Op, SelectionDAG &DAG) {
1790 EVT VT = Op.getValueType();
1792 // In order for this to be a signed 24-bit value, bit 23, must
1794 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
1795 // as unsigned 24-bit values.
1796 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24;
1799 static void simplifyI24(SDValue Op, TargetLowering::DAGCombinerInfo &DCI) {
1801 SelectionDAG &DAG = DCI.DAG;
1802 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1803 EVT VT = Op.getValueType();
1805 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24);
1806 APInt KnownZero, KnownOne;
1807 TargetLowering::TargetLoweringOpt TLO(DAG, true, true);
1808 if (TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
1809 DCI.CommitTargetLoweringOpt(TLO);
1812 template <typename IntTy>
1813 static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0,
1814 uint32_t Offset, uint32_t Width) {
1815 if (Width + Offset < 32) {
1816 IntTy Result = (Src0 << (32 - Offset - Width)) >> (32 - Width);
1817 return DAG.getConstant(Result, MVT::i32);
1820 return DAG.getConstant(Src0 >> Offset, MVT::i32);
1823 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N,
1824 DAGCombinerInfo &DCI) const {
1825 SelectionDAG &DAG = DCI.DAG;
1828 switch(N->getOpcode()) {
1831 EVT VT = N->getValueType(0);
1832 SDValue N0 = N->getOperand(0);
1833 SDValue N1 = N->getOperand(1);
1836 // FIXME: Add support for 24-bit multiply with 64-bit output on SI.
1837 if (VT.isVector() || VT.getSizeInBits() > 32)
1840 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) {
1841 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
1842 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
1843 Mul = DAG.getNode(AMDGPUISD::MUL_U24, DL, MVT::i32, N0, N1);
1844 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) {
1845 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
1846 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
1847 Mul = DAG.getNode(AMDGPUISD::MUL_I24, DL, MVT::i32, N0, N1);
1852 // We need to use sext even for MUL_U24, because MUL_U24 is used
1853 // for signed multiply of 8 and 16-bit types.
1854 SDValue Reg = DAG.getSExtOrTrunc(Mul, DL, VT);
1858 case AMDGPUISD::MUL_I24:
1859 case AMDGPUISD::MUL_U24: {
1860 SDValue N0 = N->getOperand(0);
1861 SDValue N1 = N->getOperand(1);
1862 simplifyI24(N0, DCI);
1863 simplifyI24(N1, DCI);
1866 case ISD::SELECT_CC: {
1867 return CombineMinMax(N, DAG);
1869 case AMDGPUISD::BFE_I32:
1870 case AMDGPUISD::BFE_U32: {
1871 assert(!N->getValueType(0).isVector() &&
1872 "Vector handling of BFE not implemented");
1873 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
1877 uint32_t WidthVal = Width->getZExtValue() & 0x1f;
1879 return DAG.getConstant(0, MVT::i32);
1881 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
1885 SDValue BitsFrom = N->getOperand(0);
1886 uint32_t OffsetVal = Offset->getZExtValue() & 0x1f;
1888 bool Signed = N->getOpcode() == AMDGPUISD::BFE_I32;
1890 if (OffsetVal == 0) {
1891 // This is already sign / zero extended, so try to fold away extra BFEs.
1892 unsigned SignBits = Signed ? (32 - WidthVal + 1) : (32 - WidthVal);
1894 unsigned OpSignBits = DAG.ComputeNumSignBits(BitsFrom);
1895 if (OpSignBits >= SignBits)
1898 EVT SmallVT = EVT::getIntegerVT(*DAG.getContext(), WidthVal);
1900 // This is a sign_extend_inreg. Replace it to take advantage of existing
1901 // DAG Combines. If not eliminated, we will match back to BFE during
1904 // TODO: The sext_inreg of extended types ends, although we can could
1905 // handle them in a single BFE.
1906 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, BitsFrom,
1907 DAG.getValueType(SmallVT));
1910 return DAG.getZeroExtendInReg(BitsFrom, DL, SmallVT);
1913 if (ConstantSDNode *Val = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
1915 return constantFoldBFE<int32_t>(DAG,
1916 Val->getSExtValue(),
1921 return constantFoldBFE<uint32_t>(DAG,
1922 Val->getZExtValue(),
1927 APInt Demanded = APInt::getBitsSet(32,
1929 OffsetVal + WidthVal);
1931 if ((OffsetVal + WidthVal) >= 32) {
1932 SDValue ShiftVal = DAG.getConstant(OffsetVal, MVT::i32);
1933 return DAG.getNode(Signed ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1934 BitsFrom, ShiftVal);
1937 APInt KnownZero, KnownOne;
1938 TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
1939 !DCI.isBeforeLegalizeOps());
1940 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1941 if (TLO.ShrinkDemandedConstant(BitsFrom, Demanded) ||
1942 TLI.SimplifyDemandedBits(BitsFrom, Demanded, KnownZero, KnownOne, TLO)) {
1943 DCI.CommitTargetLoweringOpt(TLO);
1952 //===----------------------------------------------------------------------===//
1954 //===----------------------------------------------------------------------===//
1956 void AMDGPUTargetLowering::getOriginalFunctionArgs(
1959 const SmallVectorImpl<ISD::InputArg> &Ins,
1960 SmallVectorImpl<ISD::InputArg> &OrigIns) const {
1962 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1963 if (Ins[i].ArgVT == Ins[i].VT) {
1964 OrigIns.push_back(Ins[i]);
1969 if (Ins[i].ArgVT.isVector() && !Ins[i].VT.isVector()) {
1970 // Vector has been split into scalars.
1971 VT = Ins[i].ArgVT.getVectorElementType();
1972 } else if (Ins[i].VT.isVector() && Ins[i].ArgVT.isVector() &&
1973 Ins[i].ArgVT.getVectorElementType() !=
1974 Ins[i].VT.getVectorElementType()) {
1975 // Vector elements have been promoted
1978 // Vector has been spilt into smaller vectors.
1982 ISD::InputArg Arg(Ins[i].Flags, VT, VT, Ins[i].Used,
1983 Ins[i].OrigArgIndex, Ins[i].PartOffset);
1984 OrigIns.push_back(Arg);
1988 bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
1989 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
1990 return CFP->isExactlyValue(1.0);
1992 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
1993 return C->isAllOnesValue();
1998 bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
1999 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
2000 return CFP->getValueAPF().isZero();
2002 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
2003 return C->isNullValue();
2008 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
2009 const TargetRegisterClass *RC,
2010 unsigned Reg, EVT VT) const {
2011 MachineFunction &MF = DAG.getMachineFunction();
2012 MachineRegisterInfo &MRI = MF.getRegInfo();
2013 unsigned VirtualRegister;
2014 if (!MRI.isLiveIn(Reg)) {
2015 VirtualRegister = MRI.createVirtualRegister(RC);
2016 MRI.addLiveIn(Reg, VirtualRegister);
2018 VirtualRegister = MRI.getLiveInVirtReg(Reg);
2020 return DAG.getRegister(VirtualRegister, VT);
2023 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
2025 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
2027 default: return nullptr;
2029 NODE_NAME_CASE(CALL);
2030 NODE_NAME_CASE(UMUL);
2031 NODE_NAME_CASE(DIV_INF);
2032 NODE_NAME_CASE(RET_FLAG);
2033 NODE_NAME_CASE(BRANCH_COND);
2036 NODE_NAME_CASE(DWORDADDR)
2037 NODE_NAME_CASE(FRACT)
2038 NODE_NAME_CASE(CLAMP)
2039 NODE_NAME_CASE(FMAX)
2040 NODE_NAME_CASE(SMAX)
2041 NODE_NAME_CASE(UMAX)
2042 NODE_NAME_CASE(FMIN)
2043 NODE_NAME_CASE(SMIN)
2044 NODE_NAME_CASE(UMIN)
2045 NODE_NAME_CASE(BFE_U32)
2046 NODE_NAME_CASE(BFE_I32)
2049 NODE_NAME_CASE(BREV)
2050 NODE_NAME_CASE(MUL_U24)
2051 NODE_NAME_CASE(MUL_I24)
2052 NODE_NAME_CASE(MAD_U24)
2053 NODE_NAME_CASE(MAD_I24)
2054 NODE_NAME_CASE(URECIP)
2055 NODE_NAME_CASE(DOT4)
2056 NODE_NAME_CASE(EXPORT)
2057 NODE_NAME_CASE(CONST_ADDRESS)
2058 NODE_NAME_CASE(REGISTER_LOAD)
2059 NODE_NAME_CASE(REGISTER_STORE)
2060 NODE_NAME_CASE(LOAD_CONSTANT)
2061 NODE_NAME_CASE(LOAD_INPUT)
2062 NODE_NAME_CASE(SAMPLE)
2063 NODE_NAME_CASE(SAMPLEB)
2064 NODE_NAME_CASE(SAMPLED)
2065 NODE_NAME_CASE(SAMPLEL)
2066 NODE_NAME_CASE(CVT_F32_UBYTE0)
2067 NODE_NAME_CASE(CVT_F32_UBYTE1)
2068 NODE_NAME_CASE(CVT_F32_UBYTE2)
2069 NODE_NAME_CASE(CVT_F32_UBYTE3)
2070 NODE_NAME_CASE(BUILD_VERTICAL_VECTOR)
2071 NODE_NAME_CASE(STORE_MSKOR)
2072 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
2076 static void computeKnownBitsForMinMax(const SDValue Op0,
2080 const SelectionDAG &DAG,
2082 APInt Op0Zero, Op0One;
2083 APInt Op1Zero, Op1One;
2084 DAG.computeKnownBits(Op0, Op0Zero, Op0One, Depth);
2085 DAG.computeKnownBits(Op1, Op1Zero, Op1One, Depth);
2087 KnownZero = Op0Zero & Op1Zero;
2088 KnownOne = Op0One & Op1One;
2091 void AMDGPUTargetLowering::computeKnownBitsForTargetNode(
2095 const SelectionDAG &DAG,
2096 unsigned Depth) const {
2098 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything.
2102 unsigned Opc = Op.getOpcode();
2107 case ISD::INTRINSIC_WO_CHAIN: {
2108 // FIXME: The intrinsic should just use the node.
2109 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
2110 case AMDGPUIntrinsic::AMDGPU_imax:
2111 case AMDGPUIntrinsic::AMDGPU_umax:
2112 case AMDGPUIntrinsic::AMDGPU_imin:
2113 case AMDGPUIntrinsic::AMDGPU_umin:
2114 computeKnownBitsForMinMax(Op.getOperand(1), Op.getOperand(2),
2115 KnownZero, KnownOne, DAG, Depth);
2123 case AMDGPUISD::SMAX:
2124 case AMDGPUISD::UMAX:
2125 case AMDGPUISD::SMIN:
2126 case AMDGPUISD::UMIN:
2127 computeKnownBitsForMinMax(Op.getOperand(0), Op.getOperand(1),
2128 KnownZero, KnownOne, DAG, Depth);
2131 case AMDGPUISD::BFE_I32:
2132 case AMDGPUISD::BFE_U32: {
2133 ConstantSDNode *CWidth = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2137 unsigned BitWidth = 32;
2138 uint32_t Width = CWidth->getZExtValue() & 0x1f;
2140 KnownZero = APInt::getAllOnesValue(BitWidth);
2141 KnownOne = APInt::getNullValue(BitWidth);
2145 // FIXME: This could do a lot more. If offset is 0, should be the same as
2146 // sign_extend_inreg implementation, but that involves duplicating it.
2147 if (Opc == AMDGPUISD::BFE_I32)
2148 KnownOne = APInt::getHighBitsSet(BitWidth, BitWidth - Width);
2150 KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - Width);
2157 unsigned AMDGPUTargetLowering::ComputeNumSignBitsForTargetNode(
2159 const SelectionDAG &DAG,
2160 unsigned Depth) const {
2161 switch (Op.getOpcode()) {
2162 case AMDGPUISD::BFE_I32: {
2163 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2167 unsigned SignBits = 32 - Width->getZExtValue() + 1;
2168 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(Op.getOperand(1));
2169 if (!Offset || !Offset->isNullValue())
2172 // TODO: Could probably figure something out with non-0 offsets.
2173 unsigned Op0SignBits = DAG.ComputeNumSignBits(Op.getOperand(0), Depth + 1);
2174 return std::max(SignBits, Op0SignBits);
2177 case AMDGPUISD::BFE_U32: {
2178 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2179 return Width ? 32 - (Width->getZExtValue() & 0x1f) : 1;