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 AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM) :
88 TargetLowering(TM, new TargetLoweringObjectFileELF()) {
90 Subtarget = &TM.getSubtarget<AMDGPUSubtarget>();
92 // Initialize target lowering borrowed from AMDIL
95 // We need to custom lower some of the intrinsics
96 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
98 // Library functions. These default to Expand, but we have instructions
100 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
101 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
102 setOperationAction(ISD::FPOW, MVT::f32, Legal);
103 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
104 setOperationAction(ISD::FABS, MVT::f32, Legal);
105 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
106 setOperationAction(ISD::FRINT, MVT::f32, Legal);
107 setOperationAction(ISD::FROUND, MVT::f32, Legal);
108 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
110 // The hardware supports ROTR, but not ROTL
111 setOperationAction(ISD::ROTL, MVT::i32, Expand);
113 // Lower floating point store/load to integer store/load to reduce the number
114 // of patterns in tablegen.
115 setOperationAction(ISD::STORE, MVT::f32, Promote);
116 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
118 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
119 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
121 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
122 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
124 setOperationAction(ISD::STORE, MVT::v8f32, Promote);
125 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
127 setOperationAction(ISD::STORE, MVT::v16f32, Promote);
128 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
130 setOperationAction(ISD::STORE, MVT::f64, Promote);
131 AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64);
133 setOperationAction(ISD::STORE, MVT::v2f64, Promote);
134 AddPromotedToType(ISD::STORE, MVT::v2f64, MVT::v2i64);
136 // Custom lowering of vector stores is required for local address space
138 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
139 // XXX: Native v2i32 local address space stores are possible, but not
140 // currently implemented.
141 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
143 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
144 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
145 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
147 // XXX: This can be change to Custom, once ExpandVectorStores can
148 // handle 64-bit stores.
149 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
151 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
152 setTruncStoreAction(MVT::i64, MVT::i8, Expand);
153 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
154 setTruncStoreAction(MVT::v2i64, MVT::v2i1, Expand);
155 setTruncStoreAction(MVT::v4i64, MVT::v4i1, Expand);
158 setOperationAction(ISD::LOAD, MVT::f32, Promote);
159 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
161 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
162 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
164 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
165 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
167 setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
168 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
170 setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
171 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
173 setOperationAction(ISD::LOAD, MVT::f64, Promote);
174 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64);
176 setOperationAction(ISD::LOAD, MVT::v2f64, Promote);
177 AddPromotedToType(ISD::LOAD, MVT::v2f64, MVT::v2i64);
179 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
180 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
181 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
182 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
183 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
184 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
185 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f32, Custom);
186 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
187 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f32, Custom);
188 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i32, Custom);
190 setLoadExtAction(ISD::EXTLOAD, MVT::v2i8, Expand);
191 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i8, Expand);
192 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i8, Expand);
193 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
194 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i8, Expand);
195 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i8, Expand);
196 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, Expand);
197 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, Expand);
198 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, Expand);
199 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, Expand);
200 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, Expand);
201 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, Expand);
203 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
205 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
207 setOperationAction(ISD::FNEG, MVT::v2f32, Expand);
208 setOperationAction(ISD::FNEG, MVT::v4f32, Expand);
210 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
212 setOperationAction(ISD::MUL, MVT::i64, Expand);
213 setOperationAction(ISD::SUB, MVT::i64, Expand);
215 setOperationAction(ISD::UDIV, MVT::i32, Expand);
216 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
217 setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
218 setOperationAction(ISD::UREM, MVT::i32, Expand);
219 setOperationAction(ISD::VSELECT, MVT::v2f32, Expand);
220 setOperationAction(ISD::VSELECT, MVT::v4f32, Expand);
222 static const MVT::SimpleValueType IntTypes[] = {
223 MVT::v2i32, MVT::v4i32
226 for (MVT VT : IntTypes) {
227 //Expand the following operations for the current type by default
228 setOperationAction(ISD::ADD, VT, Expand);
229 setOperationAction(ISD::AND, VT, Expand);
230 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
231 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
232 setOperationAction(ISD::MUL, VT, Expand);
233 setOperationAction(ISD::OR, VT, Expand);
234 setOperationAction(ISD::SHL, VT, Expand);
235 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
236 setOperationAction(ISD::SRL, VT, Expand);
237 setOperationAction(ISD::SRA, VT, Expand);
238 setOperationAction(ISD::SUB, VT, Expand);
239 setOperationAction(ISD::UDIV, VT, Expand);
240 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
241 setOperationAction(ISD::UREM, VT, Expand);
242 setOperationAction(ISD::SELECT, VT, Expand);
243 setOperationAction(ISD::VSELECT, VT, Expand);
244 setOperationAction(ISD::XOR, VT, Expand);
247 static const MVT::SimpleValueType FloatTypes[] = {
248 MVT::v2f32, MVT::v4f32
251 for (MVT VT : FloatTypes) {
252 setOperationAction(ISD::FABS, VT, Expand);
253 setOperationAction(ISD::FADD, VT, Expand);
254 setOperationAction(ISD::FCOS, VT, Expand);
255 setOperationAction(ISD::FDIV, VT, Expand);
256 setOperationAction(ISD::FPOW, VT, Expand);
257 setOperationAction(ISD::FFLOOR, VT, Expand);
258 setOperationAction(ISD::FTRUNC, VT, Expand);
259 setOperationAction(ISD::FMUL, VT, Expand);
260 setOperationAction(ISD::FRINT, VT, Expand);
261 setOperationAction(ISD::FSQRT, VT, Expand);
262 setOperationAction(ISD::FSIN, VT, Expand);
263 setOperationAction(ISD::FSUB, VT, Expand);
264 setOperationAction(ISD::SELECT, VT, Expand);
267 setTargetDAGCombine(ISD::MUL);
268 setTargetDAGCombine(ISD::SELECT_CC);
271 //===----------------------------------------------------------------------===//
272 // Target Information
273 //===----------------------------------------------------------------------===//
275 MVT AMDGPUTargetLowering::getVectorIdxTy() const {
279 bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy,
281 if (LoadTy.getSizeInBits() != CastTy.getSizeInBits())
284 unsigned LScalarSize = LoadTy.getScalarType().getSizeInBits();
285 unsigned CastScalarSize = CastTy.getScalarType().getSizeInBits();
287 return ((LScalarSize <= CastScalarSize) ||
288 (CastScalarSize >= 32) ||
292 //===---------------------------------------------------------------------===//
294 //===---------------------------------------------------------------------===//
296 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
297 assert(VT.isFloatingPoint());
298 return VT == MVT::f32;
301 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
302 assert(VT.isFloatingPoint());
303 return VT == MVT::f32;
306 bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const {
307 // Truncate is just accessing a subregister.
308 return Dest.bitsLT(Source) && (Dest.getSizeInBits() % 32 == 0);
311 bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const {
312 // Truncate is just accessing a subregister.
313 return Dest->getPrimitiveSizeInBits() < Source->getPrimitiveSizeInBits() &&
314 (Dest->getPrimitiveSizeInBits() % 32 == 0);
317 bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const {
318 const DataLayout *DL = getDataLayout();
319 unsigned SrcSize = DL->getTypeSizeInBits(Src->getScalarType());
320 unsigned DestSize = DL->getTypeSizeInBits(Dest->getScalarType());
322 return SrcSize == 32 && DestSize == 64;
325 bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const {
326 // Any register load of a 64-bit value really requires 2 32-bit moves. For all
327 // practical purposes, the extra mov 0 to load a 64-bit is free. As used,
328 // this will enable reducing 64-bit operations the 32-bit, which is always
330 return Src == MVT::i32 && Dest == MVT::i64;
333 bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const {
334 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a
335 // limited number of native 64-bit operations. Shrinking an operation to fit
336 // in a single 32-bit register should always be helpful. As currently used,
337 // this is much less general than the name suggests, and is only used in
338 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is
339 // not profitable, and may actually be harmful.
340 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32;
343 //===---------------------------------------------------------------------===//
344 // TargetLowering Callbacks
345 //===---------------------------------------------------------------------===//
347 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
348 const SmallVectorImpl<ISD::InputArg> &Ins) const {
350 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
353 SDValue AMDGPUTargetLowering::LowerReturn(
355 CallingConv::ID CallConv,
357 const SmallVectorImpl<ISD::OutputArg> &Outs,
358 const SmallVectorImpl<SDValue> &OutVals,
359 SDLoc DL, SelectionDAG &DAG) const {
360 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
363 //===---------------------------------------------------------------------===//
364 // Target specific lowering
365 //===---------------------------------------------------------------------===//
367 SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI,
368 SmallVectorImpl<SDValue> &InVals) const {
369 SDValue Callee = CLI.Callee;
370 SelectionDAG &DAG = CLI.DAG;
372 const Function &Fn = *DAG.getMachineFunction().getFunction();
374 StringRef FuncName("<unknown>");
376 if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee))
377 FuncName = G->getSymbol();
378 else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
379 FuncName = G->getGlobal()->getName();
381 DiagnosticInfoUnsupported NoCalls(Fn, "call to function " + FuncName);
382 DAG.getContext()->diagnose(NoCalls);
386 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
388 switch (Op.getOpcode()) {
390 Op.getNode()->dump();
391 llvm_unreachable("Custom lowering code for this"
392 "instruction is not implemented yet!");
394 // AMDIL DAG lowering
395 case ISD::SDIV: return LowerSDIV(Op, DAG);
396 case ISD::SREM: return LowerSREM(Op, DAG);
397 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
398 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
399 // AMDGPU DAG lowering
400 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
401 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
402 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
403 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
404 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
405 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
410 void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N,
411 SmallVectorImpl<SDValue> &Results,
412 SelectionDAG &DAG) const {
413 switch (N->getOpcode()) {
414 case ISD::SIGN_EXTEND_INREG:
415 // Different parts of legalization seem to interpret which type of
416 // sign_extend_inreg is the one to check for custom lowering. The extended
417 // from type is what really matters, but some places check for custom
418 // lowering of the result type. This results in trying to use
419 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do
420 // nothing here and let the illegal result integer be handled normally.
423 SDValue Op = SDValue(N, 0);
425 EVT VT = Op.getValueType();
426 SDValue UDIVREM = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT),
427 N->getOperand(0), N->getOperand(1));
428 Results.push_back(UDIVREM);
432 SDValue Op = SDValue(N, 0);
434 EVT VT = Op.getValueType();
435 SDValue UDIVREM = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT),
436 N->getOperand(0), N->getOperand(1));
437 Results.push_back(UDIVREM.getValue(1));
441 SDValue Op = SDValue(N, 0);
443 EVT VT = Op.getValueType();
444 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
446 SDValue one = DAG.getConstant(1, HalfVT);
447 SDValue zero = DAG.getConstant(0, HalfVT);
450 SDValue LHS = N->getOperand(0);
451 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero);
452 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one);
454 SDValue RHS = N->getOperand(1);
455 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero);
456 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one);
458 // Get Speculative values
459 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo);
460 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo);
462 SDValue REM_Hi = zero;
463 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ);
465 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ);
466 SDValue DIV_Lo = zero;
468 const unsigned halfBitWidth = HalfVT.getSizeInBits();
470 for (unsigned i = 0; i < halfBitWidth; ++i) {
471 SDValue POS = DAG.getConstant(halfBitWidth - i - 1, HalfVT);
472 // Get Value of high bit
474 if (halfBitWidth == 32 && Subtarget->hasBFE()) {
475 HBit = DAG.getNode(AMDGPUISD::BFE_U32, DL, HalfVT, LHS_Lo, POS, one);
477 HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS);
478 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one);
481 SDValue Carry = DAG.getNode(ISD::SRL, DL, HalfVT, REM_Lo,
482 DAG.getConstant(halfBitWidth - 1, HalfVT));
483 REM_Hi = DAG.getNode(ISD::SHL, DL, HalfVT, REM_Hi, one);
484 REM_Hi = DAG.getNode(ISD::OR, DL, HalfVT, REM_Hi, Carry);
486 REM_Lo = DAG.getNode(ISD::SHL, DL, HalfVT, REM_Lo, one);
487 REM_Lo = DAG.getNode(ISD::OR, DL, HalfVT, REM_Lo, HBit);
490 SDValue REM = DAG.getNode(ISD::BUILD_PAIR, DL, VT, REM_Lo, REM_Hi);
492 SDValue BIT = DAG.getConstant(1 << (halfBitWidth - i - 1), HalfVT);
493 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETGE);
495 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT);
499 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS);
501 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETGE);
502 REM_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, REM, zero);
503 REM_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, REM, one);
506 SDValue REM = DAG.getNode(ISD::BUILD_PAIR, DL, VT, REM_Lo, REM_Hi);
507 SDValue DIV = DAG.getNode(ISD::BUILD_PAIR, DL, VT, DIV_Lo, DIV_Hi);
508 Results.push_back(DIV);
509 Results.push_back(REM);
517 SDValue AMDGPUTargetLowering::LowerConstantInitializer(const Constant* Init,
518 const GlobalValue *GV,
519 const SDValue &InitPtr,
521 SelectionDAG &DAG) const {
522 const DataLayout *TD = getTargetMachine().getDataLayout();
524 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Init)) {
525 EVT VT = EVT::getEVT(CI->getType());
526 PointerType *PtrTy = PointerType::get(CI->getType(), 0);
527 return DAG.getStore(Chain, DL, DAG.getConstant(*CI, VT), InitPtr,
528 MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
529 TD->getPrefTypeAlignment(CI->getType()));
532 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Init)) {
533 EVT VT = EVT::getEVT(CFP->getType());
534 PointerType *PtrTy = PointerType::get(CFP->getType(), 0);
535 return DAG.getStore(Chain, DL, DAG.getConstantFP(*CFP, VT), InitPtr,
536 MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
537 TD->getPrefTypeAlignment(CFP->getType()));
540 Type *InitTy = Init->getType();
541 if (StructType *ST = dyn_cast<StructType>(InitTy)) {
542 const StructLayout *SL = TD->getStructLayout(ST);
544 EVT PtrVT = InitPtr.getValueType();
545 SmallVector<SDValue, 8> Chains;
547 for (unsigned I = 0, N = ST->getNumElements(); I != N; ++I) {
548 SDValue Offset = DAG.getConstant(SL->getElementOffset(I), PtrVT);
549 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, InitPtr, Offset);
551 Constant *Elt = Init->getAggregateElement(I);
552 Chains.push_back(LowerConstantInitializer(Elt, GV, Ptr, Chain, DAG));
555 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
558 if (SequentialType *SeqTy = dyn_cast<SequentialType>(InitTy)) {
559 EVT PtrVT = InitPtr.getValueType();
561 unsigned NumElements;
562 if (ArrayType *AT = dyn_cast<ArrayType>(SeqTy))
563 NumElements = AT->getNumElements();
564 else if (VectorType *VT = dyn_cast<VectorType>(SeqTy))
565 NumElements = VT->getNumElements();
567 llvm_unreachable("Unexpected type");
569 unsigned EltSize = TD->getTypeAllocSize(SeqTy->getElementType());
570 SmallVector<SDValue, 8> Chains;
571 for (unsigned i = 0; i < NumElements; ++i) {
572 SDValue Offset = DAG.getConstant(i * EltSize, PtrVT);
573 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, InitPtr, Offset);
575 Constant *Elt = Init->getAggregateElement(i);
576 Chains.push_back(LowerConstantInitializer(Elt, GV, Ptr, Chain, DAG));
579 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
583 llvm_unreachable("Unhandled constant initializer");
586 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
588 SelectionDAG &DAG) const {
590 const DataLayout *TD = getTargetMachine().getDataLayout();
591 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
592 const GlobalValue *GV = G->getGlobal();
594 switch (G->getAddressSpace()) {
595 default: llvm_unreachable("Global Address lowering not implemented for this "
597 case AMDGPUAS::LOCAL_ADDRESS: {
598 // XXX: What does the value of G->getOffset() mean?
599 assert(G->getOffset() == 0 &&
600 "Do not know what to do with an non-zero offset");
603 if (MFI->LocalMemoryObjects.count(GV) == 0) {
604 uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
605 Offset = MFI->LDSSize;
606 MFI->LocalMemoryObjects[GV] = Offset;
607 // XXX: Account for alignment?
608 MFI->LDSSize += Size;
610 Offset = MFI->LocalMemoryObjects[GV];
613 return DAG.getConstant(Offset, getPointerTy(G->getAddressSpace()));
615 case AMDGPUAS::CONSTANT_ADDRESS: {
616 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
617 Type *EltType = GV->getType()->getElementType();
618 unsigned Size = TD->getTypeAllocSize(EltType);
619 unsigned Alignment = TD->getPrefTypeAlignment(EltType);
621 const GlobalVariable *Var = cast<GlobalVariable>(GV);
622 const Constant *Init = Var->getInitializer();
623 int FI = FrameInfo->CreateStackObject(Size, Alignment, false);
624 SDValue InitPtr = DAG.getFrameIndex(FI,
625 getPointerTy(AMDGPUAS::PRIVATE_ADDRESS));
626 SmallVector<SDNode*, 8> WorkList;
628 for (SDNode::use_iterator I = DAG.getEntryNode()->use_begin(),
629 E = DAG.getEntryNode()->use_end(); I != E; ++I) {
630 if (I->getOpcode() != AMDGPUISD::REGISTER_LOAD && I->getOpcode() != ISD::LOAD)
632 WorkList.push_back(*I);
634 SDValue Chain = LowerConstantInitializer(Init, GV, InitPtr, DAG.getEntryNode(), DAG);
635 for (SmallVector<SDNode*, 8>::iterator I = WorkList.begin(),
636 E = WorkList.end(); I != E; ++I) {
637 SmallVector<SDValue, 8> Ops;
638 Ops.push_back(Chain);
639 for (unsigned i = 1; i < (*I)->getNumOperands(); ++i) {
640 Ops.push_back((*I)->getOperand(i));
642 DAG.UpdateNodeOperands(*I, Ops);
644 return DAG.getZExtOrTrunc(InitPtr, SDLoc(Op),
645 getPointerTy(AMDGPUAS::CONSTANT_ADDRESS));
650 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
651 SelectionDAG &DAG) const {
652 SmallVector<SDValue, 8> Args;
653 SDValue A = Op.getOperand(0);
654 SDValue B = Op.getOperand(1);
656 DAG.ExtractVectorElements(A, Args);
657 DAG.ExtractVectorElements(B, Args);
659 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(), Args);
662 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
663 SelectionDAG &DAG) const {
665 SmallVector<SDValue, 8> Args;
666 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
667 EVT VT = Op.getValueType();
668 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start,
669 VT.getVectorNumElements());
671 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(), Args);
674 SDValue AMDGPUTargetLowering::LowerFrameIndex(SDValue Op,
675 SelectionDAG &DAG) const {
677 MachineFunction &MF = DAG.getMachineFunction();
678 const AMDGPUFrameLowering *TFL =
679 static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
681 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
684 unsigned FrameIndex = FIN->getIndex();
685 unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
686 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF),
690 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
691 SelectionDAG &DAG) const {
692 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
694 EVT VT = Op.getValueType();
696 switch (IntrinsicID) {
698 case AMDGPUIntrinsic::AMDIL_abs:
699 return LowerIntrinsicIABS(Op, DAG);
700 case AMDGPUIntrinsic::AMDIL_exp:
701 return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
702 case AMDGPUIntrinsic::AMDGPU_lrp:
703 return LowerIntrinsicLRP(Op, DAG);
704 case AMDGPUIntrinsic::AMDIL_fraction:
705 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
706 case AMDGPUIntrinsic::AMDIL_max:
707 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, Op.getOperand(1),
709 case AMDGPUIntrinsic::AMDGPU_imax:
710 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
712 case AMDGPUIntrinsic::AMDGPU_umax:
713 return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
715 case AMDGPUIntrinsic::AMDIL_min:
716 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, Op.getOperand(1),
718 case AMDGPUIntrinsic::AMDGPU_imin:
719 return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
721 case AMDGPUIntrinsic::AMDGPU_umin:
722 return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
725 case AMDGPUIntrinsic::AMDGPU_umul24:
726 return DAG.getNode(AMDGPUISD::MUL_U24, DL, VT,
727 Op.getOperand(1), Op.getOperand(2));
729 case AMDGPUIntrinsic::AMDGPU_imul24:
730 return DAG.getNode(AMDGPUISD::MUL_I24, DL, VT,
731 Op.getOperand(1), Op.getOperand(2));
733 case AMDGPUIntrinsic::AMDGPU_bfe_i32:
734 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT,
739 case AMDGPUIntrinsic::AMDGPU_bfe_u32:
740 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT,
745 case AMDGPUIntrinsic::AMDGPU_bfi:
746 return DAG.getNode(AMDGPUISD::BFI, DL, VT,
751 case AMDGPUIntrinsic::AMDGPU_bfm:
752 return DAG.getNode(AMDGPUISD::BFM, DL, VT,
756 case AMDGPUIntrinsic::AMDIL_round_nearest:
757 return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
761 ///IABS(a) = SMAX(sub(0, a), a)
762 SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
763 SelectionDAG &DAG) const {
765 EVT VT = Op.getValueType();
766 SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
769 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
772 /// Linear Interpolation
773 /// LRP(a, b, c) = muladd(a, b, (1 - a) * c)
774 SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
775 SelectionDAG &DAG) const {
777 EVT VT = Op.getValueType();
778 SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
779 DAG.getConstantFP(1.0f, MVT::f32),
781 SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
783 return DAG.getNode(ISD::FADD, DL, VT,
784 DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
788 /// \brief Generate Min/Max node
789 SDValue AMDGPUTargetLowering::CombineMinMax(SDNode *N,
790 SelectionDAG &DAG) const {
792 EVT VT = N->getValueType(0);
794 SDValue LHS = N->getOperand(0);
795 SDValue RHS = N->getOperand(1);
796 SDValue True = N->getOperand(2);
797 SDValue False = N->getOperand(3);
798 SDValue CC = N->getOperand(4);
800 if (VT != MVT::f32 ||
801 !((LHS == True && RHS == False) || (LHS == False && RHS == True))) {
805 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
819 llvm_unreachable("Operation should already be optimised!");
826 unsigned Opc = (LHS == True) ? AMDGPUISD::FMIN : AMDGPUISD::FMAX;
827 return DAG.getNode(Opc, DL, VT, LHS, RHS);
835 unsigned Opc = (LHS == True) ? AMDGPUISD::FMAX : AMDGPUISD::FMIN;
836 return DAG.getNode(Opc, DL, VT, LHS, RHS);
838 case ISD::SETCC_INVALID:
839 llvm_unreachable("Invalid setcc condcode!");
844 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue &Op,
845 SelectionDAG &DAG) const {
846 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
847 EVT MemEltVT = Load->getMemoryVT().getVectorElementType();
848 EVT EltVT = Op.getValueType().getVectorElementType();
849 EVT PtrVT = Load->getBasePtr().getValueType();
850 unsigned NumElts = Load->getMemoryVT().getVectorNumElements();
851 SmallVector<SDValue, 8> Loads;
854 for (unsigned i = 0, e = NumElts; i != e; ++i) {
855 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, Load->getBasePtr(),
856 DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8), PtrVT));
857 Loads.push_back(DAG.getExtLoad(Load->getExtensionType(), SL, EltVT,
858 Load->getChain(), Ptr,
859 MachinePointerInfo(Load->getMemOperand()->getValue()),
860 MemEltVT, Load->isVolatile(), Load->isNonTemporal(),
861 Load->getAlignment()));
863 return DAG.getNode(ISD::BUILD_VECTOR, SL, Op.getValueType(), Loads);
866 SDValue AMDGPUTargetLowering::MergeVectorStore(const SDValue &Op,
867 SelectionDAG &DAG) const {
868 StoreSDNode *Store = dyn_cast<StoreSDNode>(Op);
869 EVT MemVT = Store->getMemoryVT();
870 unsigned MemBits = MemVT.getSizeInBits();
872 // Byte stores are really expensive, so if possible, try to pack 32-bit vector
873 // truncating store into an i32 store.
874 // XXX: We could also handle optimize other vector bitwidths.
875 if (!MemVT.isVector() || MemBits > 32) {
880 SDValue Value = Store->getValue();
881 EVT VT = Value.getValueType();
882 EVT ElemVT = VT.getVectorElementType();
883 SDValue Ptr = Store->getBasePtr();
884 EVT MemEltVT = MemVT.getVectorElementType();
885 unsigned MemEltBits = MemEltVT.getSizeInBits();
886 unsigned MemNumElements = MemVT.getVectorNumElements();
887 unsigned PackedSize = MemVT.getStoreSizeInBits();
888 SDValue Mask = DAG.getConstant((1 << MemEltBits) - 1, MVT::i32);
890 assert(Value.getValueType().getScalarSizeInBits() >= 32);
893 for (unsigned i = 0; i < MemNumElements; ++i) {
894 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT, Value,
895 DAG.getConstant(i, MVT::i32));
896 Elt = DAG.getZExtOrTrunc(Elt, DL, MVT::i32);
897 Elt = DAG.getNode(ISD::AND, DL, MVT::i32, Elt, Mask); // getZeroExtendInReg
899 SDValue Shift = DAG.getConstant(MemEltBits * i, MVT::i32);
900 Elt = DAG.getNode(ISD::SHL, DL, MVT::i32, Elt, Shift);
905 PackedValue = DAG.getNode(ISD::OR, DL, MVT::i32, PackedValue, Elt);
909 if (PackedSize < 32) {
910 EVT PackedVT = EVT::getIntegerVT(*DAG.getContext(), PackedSize);
911 return DAG.getTruncStore(Store->getChain(), DL, PackedValue, Ptr,
912 Store->getMemOperand()->getPointerInfo(),
914 Store->isNonTemporal(), Store->isVolatile(),
915 Store->getAlignment());
918 return DAG.getStore(Store->getChain(), DL, PackedValue, Ptr,
919 Store->getMemOperand()->getPointerInfo(),
920 Store->isVolatile(), Store->isNonTemporal(),
921 Store->getAlignment());
924 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
925 SelectionDAG &DAG) const {
926 StoreSDNode *Store = cast<StoreSDNode>(Op);
927 EVT MemEltVT = Store->getMemoryVT().getVectorElementType();
928 EVT EltVT = Store->getValue().getValueType().getVectorElementType();
929 EVT PtrVT = Store->getBasePtr().getValueType();
930 unsigned NumElts = Store->getMemoryVT().getVectorNumElements();
933 SmallVector<SDValue, 8> Chains;
935 for (unsigned i = 0, e = NumElts; i != e; ++i) {
936 SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
937 Store->getValue(), DAG.getConstant(i, MVT::i32));
938 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT,
940 DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8),
942 Chains.push_back(DAG.getTruncStore(Store->getChain(), SL, Val, Ptr,
943 MachinePointerInfo(Store->getMemOperand()->getValue()),
944 MemEltVT, Store->isVolatile(), Store->isNonTemporal(),
945 Store->getAlignment()));
947 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, Chains);
950 SDValue AMDGPUTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
952 LoadSDNode *Load = cast<LoadSDNode>(Op);
953 ISD::LoadExtType ExtType = Load->getExtensionType();
954 EVT VT = Op.getValueType();
955 EVT MemVT = Load->getMemoryVT();
957 if (ExtType != ISD::NON_EXTLOAD && !VT.isVector() && VT.getSizeInBits() > 32) {
958 // We can do the extload to 32-bits, and then need to separately extend to
961 SDValue ExtLoad32 = DAG.getExtLoad(ExtType, DL, MVT::i32,
965 Load->getMemOperand());
966 return DAG.getNode(ISD::getExtForLoadExtType(ExtType), DL, VT, ExtLoad32);
969 if (ExtType == ISD::NON_EXTLOAD && VT.getSizeInBits() < 32) {
970 assert(VT == MVT::i1 && "Only i1 non-extloads expected");
971 // FIXME: Copied from PPC
972 // First, load into 32 bits, then truncate to 1 bit.
974 SDValue Chain = Load->getChain();
975 SDValue BasePtr = Load->getBasePtr();
976 MachineMemOperand *MMO = Load->getMemOperand();
978 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
979 BasePtr, MVT::i8, MMO);
980 return DAG.getNode(ISD::TRUNCATE, DL, VT, NewLD);
983 // Lower loads constant address space global variable loads
984 if (Load->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS &&
986 GetUnderlyingObject(Load->getMemOperand()->getValue()))) {
988 SDValue Ptr = DAG.getZExtOrTrunc(Load->getBasePtr(), DL,
989 getPointerTy(AMDGPUAS::PRIVATE_ADDRESS));
990 Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
991 DAG.getConstant(2, MVT::i32));
992 return DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
993 Load->getChain(), Ptr,
994 DAG.getTargetConstant(0, MVT::i32), Op.getOperand(2));
997 if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS ||
998 ExtType == ISD::NON_EXTLOAD || Load->getMemoryVT().bitsGE(MVT::i32))
1002 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
1003 DAG.getConstant(2, MVT::i32));
1004 SDValue Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
1005 Load->getChain(), Ptr,
1006 DAG.getTargetConstant(0, MVT::i32),
1008 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
1010 DAG.getConstant(0x3, MVT::i32));
1011 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1012 DAG.getConstant(3, MVT::i32));
1014 Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Ret, ShiftAmt);
1016 EVT MemEltVT = MemVT.getScalarType();
1017 if (ExtType == ISD::SEXTLOAD) {
1018 SDValue MemEltVTNode = DAG.getValueType(MemEltVT);
1019 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode);
1022 return DAG.getZeroExtendInReg(Ret, DL, MemEltVT);
1025 SDValue AMDGPUTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1027 SDValue Result = AMDGPUTargetLowering::MergeVectorStore(Op, DAG);
1028 if (Result.getNode()) {
1032 StoreSDNode *Store = cast<StoreSDNode>(Op);
1033 SDValue Chain = Store->getChain();
1034 if ((Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1035 Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) &&
1036 Store->getValue().getValueType().isVector()) {
1037 return SplitVectorStore(Op, DAG);
1040 EVT MemVT = Store->getMemoryVT();
1041 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS &&
1042 MemVT.bitsLT(MVT::i32)) {
1044 if (Store->getMemoryVT() == MVT::i8) {
1046 } else if (Store->getMemoryVT() == MVT::i16) {
1049 SDValue BasePtr = Store->getBasePtr();
1050 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, BasePtr,
1051 DAG.getConstant(2, MVT::i32));
1052 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
1053 Chain, Ptr, DAG.getTargetConstant(0, MVT::i32));
1055 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, BasePtr,
1056 DAG.getConstant(0x3, MVT::i32));
1058 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1059 DAG.getConstant(3, MVT::i32));
1061 SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
1064 SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT);
1066 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1067 MaskedValue, ShiftAmt);
1069 SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32, DAG.getConstant(Mask, MVT::i32),
1071 DstMask = DAG.getNode(ISD::XOR, DL, MVT::i32, DstMask,
1072 DAG.getConstant(0xffffffff, MVT::i32));
1073 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1075 SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
1076 return DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1077 Chain, Value, Ptr, DAG.getTargetConstant(0, MVT::i32));
1082 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
1083 SelectionDAG &DAG) const {
1085 EVT VT = Op.getValueType();
1087 SDValue Num = Op.getOperand(0);
1088 SDValue Den = Op.getOperand(1);
1090 // RCP = URECIP(Den) = 2^32 / Den + e
1091 // e is rounding error.
1092 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
1094 // RCP_LO = umulo(RCP, Den) */
1095 SDValue RCP_LO = DAG.getNode(ISD::UMULO, DL, VT, RCP, Den);
1097 // RCP_HI = mulhu (RCP, Den) */
1098 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
1100 // NEG_RCP_LO = -RCP_LO
1101 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
1104 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
1105 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
1108 // Calculate the rounding error from the URECIP instruction
1109 // E = mulhu(ABS_RCP_LO, RCP)
1110 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
1112 // RCP_A_E = RCP + E
1113 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
1115 // RCP_S_E = RCP - E
1116 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
1118 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
1119 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
1122 // Quotient = mulhu(Tmp0, Num)
1123 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
1125 // Num_S_Remainder = Quotient * Den
1126 SDValue Num_S_Remainder = DAG.getNode(ISD::UMULO, DL, VT, Quotient, Den);
1128 // Remainder = Num - Num_S_Remainder
1129 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
1131 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
1132 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
1133 DAG.getConstant(-1, VT),
1134 DAG.getConstant(0, VT),
1136 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
1137 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
1139 DAG.getConstant(-1, VT),
1140 DAG.getConstant(0, VT),
1142 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
1143 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
1146 // Calculate Division result:
1148 // Quotient_A_One = Quotient + 1
1149 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
1150 DAG.getConstant(1, VT));
1152 // Quotient_S_One = Quotient - 1
1153 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
1154 DAG.getConstant(1, VT));
1156 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
1157 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
1158 Quotient, Quotient_A_One, ISD::SETEQ);
1160 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
1161 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
1162 Quotient_S_One, Div, ISD::SETEQ);
1164 // Calculate Rem result:
1166 // Remainder_S_Den = Remainder - Den
1167 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
1169 // Remainder_A_Den = Remainder + Den
1170 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
1172 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
1173 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
1174 Remainder, Remainder_S_Den, ISD::SETEQ);
1176 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
1177 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
1178 Remainder_A_Den, Rem, ISD::SETEQ);
1183 return DAG.getMergeValues(Ops, DL);
1186 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
1187 SelectionDAG &DAG) const {
1188 SDValue S0 = Op.getOperand(0);
1190 if (Op.getValueType() != MVT::f32 || S0.getValueType() != MVT::i64)
1193 // f32 uint_to_fp i64
1194 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
1195 DAG.getConstant(0, MVT::i32));
1196 SDValue FloatLo = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Lo);
1197 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
1198 DAG.getConstant(1, MVT::i32));
1199 SDValue FloatHi = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Hi);
1200 FloatHi = DAG.getNode(ISD::FMUL, DL, MVT::f32, FloatHi,
1201 DAG.getConstantFP(4294967296.0f, MVT::f32)); // 2^32
1202 return DAG.getNode(ISD::FADD, DL, MVT::f32, FloatLo, FloatHi);
1206 SDValue AMDGPUTargetLowering::ExpandSIGN_EXTEND_INREG(SDValue Op,
1208 SelectionDAG &DAG) const {
1209 MVT VT = Op.getSimpleValueType();
1211 SDValue Shift = DAG.getConstant(BitsDiff, VT);
1212 // Shift left by 'Shift' bits.
1213 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Op.getOperand(0), Shift);
1214 // Signed shift Right by 'Shift' bits.
1215 return DAG.getNode(ISD::SRA, DL, VT, Shl, Shift);
1218 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
1219 SelectionDAG &DAG) const {
1220 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1221 MVT VT = Op.getSimpleValueType();
1222 MVT ScalarVT = VT.getScalarType();
1227 SDValue Src = Op.getOperand(0);
1230 // TODO: Don't scalarize on Evergreen?
1231 unsigned NElts = VT.getVectorNumElements();
1232 SmallVector<SDValue, 8> Args;
1233 DAG.ExtractVectorElements(Src, Args, 0, NElts);
1235 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType());
1236 for (unsigned I = 0; I < NElts; ++I)
1237 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp);
1239 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Args);
1242 //===----------------------------------------------------------------------===//
1243 // Custom DAG optimizations
1244 //===----------------------------------------------------------------------===//
1246 static bool isU24(SDValue Op, SelectionDAG &DAG) {
1247 APInt KnownZero, KnownOne;
1248 EVT VT = Op.getValueType();
1249 DAG.computeKnownBits(Op, KnownZero, KnownOne);
1251 return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24;
1254 static bool isI24(SDValue Op, SelectionDAG &DAG) {
1255 EVT VT = Op.getValueType();
1257 // In order for this to be a signed 24-bit value, bit 23, must
1259 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
1260 // as unsigned 24-bit values.
1261 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24;
1264 static void simplifyI24(SDValue Op, TargetLowering::DAGCombinerInfo &DCI) {
1266 SelectionDAG &DAG = DCI.DAG;
1267 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1268 EVT VT = Op.getValueType();
1270 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24);
1271 APInt KnownZero, KnownOne;
1272 TargetLowering::TargetLoweringOpt TLO(DAG, true, true);
1273 if (TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
1274 DCI.CommitTargetLoweringOpt(TLO);
1277 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N,
1278 DAGCombinerInfo &DCI) const {
1279 SelectionDAG &DAG = DCI.DAG;
1282 switch(N->getOpcode()) {
1285 EVT VT = N->getValueType(0);
1286 SDValue N0 = N->getOperand(0);
1287 SDValue N1 = N->getOperand(1);
1290 // FIXME: Add support for 24-bit multiply with 64-bit output on SI.
1291 if (VT.isVector() || VT.getSizeInBits() > 32)
1294 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) {
1295 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
1296 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
1297 Mul = DAG.getNode(AMDGPUISD::MUL_U24, DL, MVT::i32, N0, N1);
1298 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) {
1299 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
1300 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
1301 Mul = DAG.getNode(AMDGPUISD::MUL_I24, DL, MVT::i32, N0, N1);
1306 // We need to use sext even for MUL_U24, because MUL_U24 is used
1307 // for signed multiply of 8 and 16-bit types.
1308 SDValue Reg = DAG.getSExtOrTrunc(Mul, DL, VT);
1312 case AMDGPUISD::MUL_I24:
1313 case AMDGPUISD::MUL_U24: {
1314 SDValue N0 = N->getOperand(0);
1315 SDValue N1 = N->getOperand(1);
1316 simplifyI24(N0, DCI);
1317 simplifyI24(N1, DCI);
1320 case ISD::SELECT_CC: {
1321 return CombineMinMax(N, DAG);
1327 //===----------------------------------------------------------------------===//
1329 //===----------------------------------------------------------------------===//
1331 void AMDGPUTargetLowering::getOriginalFunctionArgs(
1334 const SmallVectorImpl<ISD::InputArg> &Ins,
1335 SmallVectorImpl<ISD::InputArg> &OrigIns) const {
1337 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1338 if (Ins[i].ArgVT == Ins[i].VT) {
1339 OrigIns.push_back(Ins[i]);
1344 if (Ins[i].ArgVT.isVector() && !Ins[i].VT.isVector()) {
1345 // Vector has been split into scalars.
1346 VT = Ins[i].ArgVT.getVectorElementType();
1347 } else if (Ins[i].VT.isVector() && Ins[i].ArgVT.isVector() &&
1348 Ins[i].ArgVT.getVectorElementType() !=
1349 Ins[i].VT.getVectorElementType()) {
1350 // Vector elements have been promoted
1353 // Vector has been spilt into smaller vectors.
1357 ISD::InputArg Arg(Ins[i].Flags, VT, VT, Ins[i].Used,
1358 Ins[i].OrigArgIndex, Ins[i].PartOffset);
1359 OrigIns.push_back(Arg);
1363 bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
1364 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
1365 return CFP->isExactlyValue(1.0);
1367 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
1368 return C->isAllOnesValue();
1373 bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
1374 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
1375 return CFP->getValueAPF().isZero();
1377 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
1378 return C->isNullValue();
1383 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1384 const TargetRegisterClass *RC,
1385 unsigned Reg, EVT VT) const {
1386 MachineFunction &MF = DAG.getMachineFunction();
1387 MachineRegisterInfo &MRI = MF.getRegInfo();
1388 unsigned VirtualRegister;
1389 if (!MRI.isLiveIn(Reg)) {
1390 VirtualRegister = MRI.createVirtualRegister(RC);
1391 MRI.addLiveIn(Reg, VirtualRegister);
1393 VirtualRegister = MRI.getLiveInVirtReg(Reg);
1395 return DAG.getRegister(VirtualRegister, VT);
1398 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
1400 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
1402 default: return nullptr;
1404 NODE_NAME_CASE(CALL);
1405 NODE_NAME_CASE(UMUL);
1406 NODE_NAME_CASE(DIV_INF);
1407 NODE_NAME_CASE(RET_FLAG);
1408 NODE_NAME_CASE(BRANCH_COND);
1411 NODE_NAME_CASE(DWORDADDR)
1412 NODE_NAME_CASE(FRACT)
1413 NODE_NAME_CASE(FMAX)
1414 NODE_NAME_CASE(SMAX)
1415 NODE_NAME_CASE(UMAX)
1416 NODE_NAME_CASE(FMIN)
1417 NODE_NAME_CASE(SMIN)
1418 NODE_NAME_CASE(UMIN)
1419 NODE_NAME_CASE(BFE_U32)
1420 NODE_NAME_CASE(BFE_I32)
1423 NODE_NAME_CASE(MUL_U24)
1424 NODE_NAME_CASE(MUL_I24)
1425 NODE_NAME_CASE(URECIP)
1426 NODE_NAME_CASE(DOT4)
1427 NODE_NAME_CASE(EXPORT)
1428 NODE_NAME_CASE(CONST_ADDRESS)
1429 NODE_NAME_CASE(REGISTER_LOAD)
1430 NODE_NAME_CASE(REGISTER_STORE)
1431 NODE_NAME_CASE(LOAD_CONSTANT)
1432 NODE_NAME_CASE(LOAD_INPUT)
1433 NODE_NAME_CASE(SAMPLE)
1434 NODE_NAME_CASE(SAMPLEB)
1435 NODE_NAME_CASE(SAMPLED)
1436 NODE_NAME_CASE(SAMPLEL)
1437 NODE_NAME_CASE(STORE_MSKOR)
1438 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
1442 static void computeKnownBitsForMinMax(const SDValue Op0,
1446 const SelectionDAG &DAG,
1448 APInt Op0Zero, Op0One;
1449 APInt Op1Zero, Op1One;
1450 DAG.computeKnownBits(Op0, Op0Zero, Op0One, Depth);
1451 DAG.computeKnownBits(Op1, Op1Zero, Op1One, Depth);
1453 KnownZero = Op0Zero & Op1Zero;
1454 KnownOne = Op0One & Op1One;
1457 void AMDGPUTargetLowering::computeKnownBitsForTargetNode(
1461 const SelectionDAG &DAG,
1462 unsigned Depth) const {
1464 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything.
1465 unsigned Opc = Op.getOpcode();
1467 case ISD::INTRINSIC_WO_CHAIN: {
1468 // FIXME: The intrinsic should just use the node.
1469 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
1470 case AMDGPUIntrinsic::AMDGPU_imax:
1471 case AMDGPUIntrinsic::AMDGPU_umax:
1472 case AMDGPUIntrinsic::AMDGPU_imin:
1473 case AMDGPUIntrinsic::AMDGPU_umin:
1474 computeKnownBitsForMinMax(Op.getOperand(1), Op.getOperand(2),
1475 KnownZero, KnownOne, DAG, Depth);
1483 case AMDGPUISD::SMAX:
1484 case AMDGPUISD::UMAX:
1485 case AMDGPUISD::SMIN:
1486 case AMDGPUISD::UMIN:
1487 computeKnownBitsForMinMax(Op.getOperand(0), Op.getOperand(1),
1488 KnownZero, KnownOne, DAG, Depth);