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/CodeGen/CallingConvLower.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/SelectionDAG.h"
28 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
29 #include "llvm/IR/DataLayout.h"
32 static bool allocateStack(unsigned ValNo, MVT ValVT, MVT LocVT,
33 CCValAssign::LocInfo LocInfo,
34 ISD::ArgFlagsTy ArgFlags, CCState &State) {
35 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() / 8, ArgFlags.getOrigAlign());
36 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
41 #include "AMDGPUGenCallingConv.inc"
43 AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM) :
44 TargetLowering(TM, new TargetLoweringObjectFileELF()) {
46 // Initialize target lowering borrowed from AMDIL
49 // We need to custom lower some of the intrinsics
50 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
52 // Library functions. These default to Expand, but we have instructions
54 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
55 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
56 setOperationAction(ISD::FPOW, MVT::f32, Legal);
57 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
58 setOperationAction(ISD::FABS, MVT::f32, Legal);
59 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
60 setOperationAction(ISD::FRINT, MVT::f32, Legal);
61 setOperationAction(ISD::FROUND, MVT::f32, Legal);
63 // The hardware supports ROTR, but not ROTL
64 setOperationAction(ISD::ROTL, MVT::i32, Expand);
66 // Lower floating point store/load to integer store/load to reduce the number
67 // of patterns in tablegen.
68 setOperationAction(ISD::STORE, MVT::f32, Promote);
69 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
71 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
72 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
74 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
75 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
77 setOperationAction(ISD::STORE, MVT::v8f32, Promote);
78 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
80 setOperationAction(ISD::STORE, MVT::v16f32, Promote);
81 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
83 setOperationAction(ISD::STORE, MVT::f64, Promote);
84 AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64);
86 // Custom lowering of vector stores is required for local address space
88 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
89 // XXX: Native v2i32 local address space stores are possible, but not
90 // currently implemented.
91 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
93 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
94 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
95 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
96 // XXX: This can be change to Custom, once ExpandVectorStores can
97 // handle 64-bit stores.
98 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
100 setOperationAction(ISD::LOAD, MVT::f32, Promote);
101 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
103 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
104 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
106 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
107 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
109 setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
110 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
112 setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
113 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
115 setOperationAction(ISD::LOAD, MVT::f64, Promote);
116 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64);
118 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
119 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
120 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
121 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
123 setLoadExtAction(ISD::EXTLOAD, MVT::v2i8, Expand);
124 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i8, Expand);
125 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i8, Expand);
126 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
127 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i8, Expand);
128 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i8, Expand);
129 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, Expand);
130 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, Expand);
131 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, Expand);
132 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, Expand);
133 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, Expand);
134 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, Expand);
136 setOperationAction(ISD::FNEG, MVT::v2f32, Expand);
137 setOperationAction(ISD::FNEG, MVT::v4f32, Expand);
139 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
141 setOperationAction(ISD::MUL, MVT::i64, Expand);
143 setOperationAction(ISD::UDIV, MVT::i32, Expand);
144 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
145 setOperationAction(ISD::UREM, MVT::i32, Expand);
146 setOperationAction(ISD::VSELECT, MVT::v2f32, Expand);
147 setOperationAction(ISD::VSELECT, MVT::v4f32, Expand);
149 static const MVT::SimpleValueType IntTypes[] = {
150 MVT::v2i32, MVT::v4i32
152 const size_t NumIntTypes = array_lengthof(IntTypes);
154 for (unsigned int x = 0; x < NumIntTypes; ++x) {
155 MVT::SimpleValueType VT = IntTypes[x];
156 //Expand the following operations for the current type by default
157 setOperationAction(ISD::ADD, VT, Expand);
158 setOperationAction(ISD::AND, VT, Expand);
159 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
160 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
161 setOperationAction(ISD::MUL, VT, Expand);
162 setOperationAction(ISD::OR, VT, Expand);
163 setOperationAction(ISD::SHL, VT, Expand);
164 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
165 setOperationAction(ISD::SRL, VT, Expand);
166 setOperationAction(ISD::SRA, VT, Expand);
167 setOperationAction(ISD::SUB, VT, Expand);
168 setOperationAction(ISD::UDIV, VT, Expand);
169 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
170 setOperationAction(ISD::UREM, VT, Expand);
171 setOperationAction(ISD::VSELECT, VT, Expand);
172 setOperationAction(ISD::XOR, VT, Expand);
175 static const MVT::SimpleValueType FloatTypes[] = {
176 MVT::v2f32, MVT::v4f32
178 const size_t NumFloatTypes = array_lengthof(FloatTypes);
180 for (unsigned int x = 0; x < NumFloatTypes; ++x) {
181 MVT::SimpleValueType VT = FloatTypes[x];
182 setOperationAction(ISD::FABS, VT, Expand);
183 setOperationAction(ISD::FADD, VT, Expand);
184 setOperationAction(ISD::FDIV, VT, Expand);
185 setOperationAction(ISD::FFLOOR, VT, Expand);
186 setOperationAction(ISD::FMUL, VT, Expand);
187 setOperationAction(ISD::FRINT, VT, Expand);
188 setOperationAction(ISD::FSQRT, VT, Expand);
189 setOperationAction(ISD::FSUB, VT, Expand);
193 //===----------------------------------------------------------------------===//
194 // Target Information
195 //===----------------------------------------------------------------------===//
197 MVT AMDGPUTargetLowering::getVectorIdxTy() const {
201 bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy,
203 if (LoadTy.getSizeInBits() != CastTy.getSizeInBits())
206 unsigned LScalarSize = LoadTy.getScalarType().getSizeInBits();
207 unsigned CastScalarSize = CastTy.getScalarType().getSizeInBits();
209 return ((LScalarSize <= CastScalarSize) ||
210 (CastScalarSize >= 32) ||
214 //===---------------------------------------------------------------------===//
216 //===---------------------------------------------------------------------===//
218 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
219 assert(VT.isFloatingPoint());
220 return VT == MVT::f32;
223 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
224 assert(VT.isFloatingPoint());
225 return VT == MVT::f32;
228 //===---------------------------------------------------------------------===//
229 // TargetLowering Callbacks
230 //===---------------------------------------------------------------------===//
232 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
233 const SmallVectorImpl<ISD::InputArg> &Ins) const {
235 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
238 SDValue AMDGPUTargetLowering::LowerReturn(
240 CallingConv::ID CallConv,
242 const SmallVectorImpl<ISD::OutputArg> &Outs,
243 const SmallVectorImpl<SDValue> &OutVals,
244 SDLoc DL, SelectionDAG &DAG) const {
245 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
248 //===---------------------------------------------------------------------===//
249 // Target specific lowering
250 //===---------------------------------------------------------------------===//
252 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
254 switch (Op.getOpcode()) {
256 Op.getNode()->dump();
257 assert(0 && "Custom lowering code for this"
258 "instruction is not implemented yet!");
260 // AMDIL DAG lowering
261 case ISD::SDIV: return LowerSDIV(Op, DAG);
262 case ISD::SREM: return LowerSREM(Op, DAG);
263 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
264 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
265 // AMDGPU DAG lowering
266 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
267 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
268 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
269 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
270 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
271 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
276 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
278 SelectionDAG &DAG) const {
280 const DataLayout *TD = getTargetMachine().getDataLayout();
281 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
283 assert(G->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS);
284 // XXX: What does the value of G->getOffset() mean?
285 assert(G->getOffset() == 0 &&
286 "Do not know what to do with an non-zero offset");
288 const GlobalValue *GV = G->getGlobal();
291 if (MFI->LocalMemoryObjects.count(GV) == 0) {
292 uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
293 Offset = MFI->LDSSize;
294 MFI->LocalMemoryObjects[GV] = Offset;
295 // XXX: Account for alignment?
296 MFI->LDSSize += Size;
298 Offset = MFI->LocalMemoryObjects[GV];
301 return DAG.getConstant(Offset, getPointerTy(G->getAddressSpace()));
304 void AMDGPUTargetLowering::ExtractVectorElements(SDValue Op, SelectionDAG &DAG,
305 SmallVectorImpl<SDValue> &Args,
307 unsigned Count) const {
308 EVT VT = Op.getValueType();
309 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
310 Args.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
311 VT.getVectorElementType(),
312 Op, DAG.getConstant(i, MVT::i32)));
316 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
317 SelectionDAG &DAG) const {
318 SmallVector<SDValue, 8> Args;
319 SDValue A = Op.getOperand(0);
320 SDValue B = Op.getOperand(1);
322 ExtractVectorElements(A, DAG, Args, 0,
323 A.getValueType().getVectorNumElements());
324 ExtractVectorElements(B, DAG, Args, 0,
325 B.getValueType().getVectorNumElements());
327 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(),
328 &Args[0], Args.size());
331 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
332 SelectionDAG &DAG) const {
334 SmallVector<SDValue, 8> Args;
335 EVT VT = Op.getValueType();
336 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
337 ExtractVectorElements(Op.getOperand(0), DAG, Args, Start,
338 VT.getVectorNumElements());
340 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(),
341 &Args[0], Args.size());
344 SDValue AMDGPUTargetLowering::LowerFrameIndex(SDValue Op,
345 SelectionDAG &DAG) const {
347 MachineFunction &MF = DAG.getMachineFunction();
348 const AMDGPUFrameLowering *TFL =
349 static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
351 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
354 unsigned FrameIndex = FIN->getIndex();
355 unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
356 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF),
360 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
361 SelectionDAG &DAG) const {
362 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
364 EVT VT = Op.getValueType();
366 switch (IntrinsicID) {
368 case AMDGPUIntrinsic::AMDIL_abs:
369 return LowerIntrinsicIABS(Op, DAG);
370 case AMDGPUIntrinsic::AMDIL_exp:
371 return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
372 case AMDGPUIntrinsic::AMDGPU_lrp:
373 return LowerIntrinsicLRP(Op, DAG);
374 case AMDGPUIntrinsic::AMDIL_fraction:
375 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
376 case AMDGPUIntrinsic::AMDIL_max:
377 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, Op.getOperand(1),
379 case AMDGPUIntrinsic::AMDGPU_imax:
380 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
382 case AMDGPUIntrinsic::AMDGPU_umax:
383 return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
385 case AMDGPUIntrinsic::AMDIL_min:
386 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, Op.getOperand(1),
388 case AMDGPUIntrinsic::AMDGPU_imin:
389 return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
391 case AMDGPUIntrinsic::AMDGPU_umin:
392 return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
394 case AMDGPUIntrinsic::AMDIL_round_nearest:
395 return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
399 ///IABS(a) = SMAX(sub(0, a), a)
400 SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
401 SelectionDAG &DAG) const {
404 EVT VT = Op.getValueType();
405 SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
408 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
411 /// Linear Interpolation
412 /// LRP(a, b, c) = muladd(a, b, (1 - a) * c)
413 SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
414 SelectionDAG &DAG) const {
416 EVT VT = Op.getValueType();
417 SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
418 DAG.getConstantFP(1.0f, MVT::f32),
420 SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
422 return DAG.getNode(ISD::FADD, DL, VT,
423 DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
427 /// \brief Generate Min/Max node
428 SDValue AMDGPUTargetLowering::LowerMinMax(SDValue Op,
429 SelectionDAG &DAG) const {
431 EVT VT = Op.getValueType();
433 SDValue LHS = Op.getOperand(0);
434 SDValue RHS = Op.getOperand(1);
435 SDValue True = Op.getOperand(2);
436 SDValue False = Op.getOperand(3);
437 SDValue CC = Op.getOperand(4);
439 if (VT != MVT::f32 ||
440 !((LHS == True && RHS == False) || (LHS == False && RHS == True))) {
444 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
458 assert(0 && "Operation should already be optimised !");
466 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
468 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
477 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
479 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
481 case ISD::SETCC_INVALID:
482 assert(0 && "Invalid setcc condcode !");
487 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue &Op,
488 SelectionDAG &DAG) const {
489 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
490 EVT MemEltVT = Load->getMemoryVT().getVectorElementType();
491 EVT EltVT = Op.getValueType().getVectorElementType();
492 EVT PtrVT = Load->getBasePtr().getValueType();
493 unsigned NumElts = Load->getMemoryVT().getVectorNumElements();
494 SmallVector<SDValue, 8> Loads;
497 for (unsigned i = 0, e = NumElts; i != e; ++i) {
498 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, Load->getBasePtr(),
499 DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8), PtrVT));
500 Loads.push_back(DAG.getExtLoad(Load->getExtensionType(), SL, EltVT,
501 Load->getChain(), Ptr,
502 MachinePointerInfo(Load->getMemOperand()->getValue()),
503 MemEltVT, Load->isVolatile(), Load->isNonTemporal(),
504 Load->getAlignment()));
506 return DAG.getNode(ISD::BUILD_VECTOR, SL, Op.getValueType(), &Loads[0],
510 SDValue AMDGPUTargetLowering::MergeVectorStore(const SDValue &Op,
511 SelectionDAG &DAG) const {
512 StoreSDNode *Store = dyn_cast<StoreSDNode>(Op);
513 EVT MemVT = Store->getMemoryVT();
514 unsigned MemBits = MemVT.getSizeInBits();
516 // Byte stores are really expensive, so if possible, try to pack
517 // 32-bit vector truncatating store into an i32 store.
518 // XXX: We could also handle optimize other vector bitwidths
519 if (!MemVT.isVector() || MemBits > 32) {
524 const SDValue &Value = Store->getValue();
525 EVT VT = Value.getValueType();
526 const SDValue &Ptr = Store->getBasePtr();
527 EVT MemEltVT = MemVT.getVectorElementType();
528 unsigned MemEltBits = MemEltVT.getSizeInBits();
529 unsigned MemNumElements = MemVT.getVectorNumElements();
530 EVT PackedVT = EVT::getIntegerVT(*DAG.getContext(), MemVT.getSizeInBits());
534 Mask = DAG.getConstant(0xFF, PackedVT);
537 Mask = DAG.getConstant(0xFFFF, PackedVT);
540 llvm_unreachable("Cannot lower this vector store");
543 for (unsigned i = 0; i < MemNumElements; ++i) {
544 EVT ElemVT = VT.getVectorElementType();
545 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT, Value,
546 DAG.getConstant(i, MVT::i32));
547 Elt = DAG.getZExtOrTrunc(Elt, DL, PackedVT);
548 Elt = DAG.getNode(ISD::AND, DL, PackedVT, Elt, Mask);
549 SDValue Shift = DAG.getConstant(MemEltBits * i, PackedVT);
550 Elt = DAG.getNode(ISD::SHL, DL, PackedVT, Elt, Shift);
554 PackedValue = DAG.getNode(ISD::OR, DL, PackedVT, PackedValue, Elt);
557 return DAG.getStore(Store->getChain(), DL, PackedValue, Ptr,
558 MachinePointerInfo(Store->getMemOperand()->getValue()),
559 Store->isVolatile(), Store->isNonTemporal(),
560 Store->getAlignment());
563 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
564 SelectionDAG &DAG) const {
565 StoreSDNode *Store = cast<StoreSDNode>(Op);
566 EVT MemEltVT = Store->getMemoryVT().getVectorElementType();
567 EVT EltVT = Store->getValue().getValueType().getVectorElementType();
568 EVT PtrVT = Store->getBasePtr().getValueType();
569 unsigned NumElts = Store->getMemoryVT().getVectorNumElements();
572 SmallVector<SDValue, 8> Chains;
574 for (unsigned i = 0, e = NumElts; i != e; ++i) {
575 SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
576 Store->getValue(), DAG.getConstant(i, MVT::i32));
577 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT,
579 DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8),
581 Chains.push_back(DAG.getTruncStore(Store->getChain(), SL, Val, Ptr,
582 MachinePointerInfo(Store->getMemOperand()->getValue()),
583 MemEltVT, Store->isVolatile(), Store->isNonTemporal(),
584 Store->getAlignment()));
586 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, &Chains[0], NumElts);
589 SDValue AMDGPUTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
590 SDValue Result = AMDGPUTargetLowering::MergeVectorStore(Op, DAG);
591 if (Result.getNode()) {
595 StoreSDNode *Store = cast<StoreSDNode>(Op);
596 if ((Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
597 Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) &&
598 Store->getValue().getValueType().isVector()) {
599 return SplitVectorStore(Op, DAG);
604 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
605 SelectionDAG &DAG) const {
607 EVT VT = Op.getValueType();
609 SDValue Num = Op.getOperand(0);
610 SDValue Den = Op.getOperand(1);
612 SmallVector<SDValue, 8> Results;
614 // RCP = URECIP(Den) = 2^32 / Den + e
615 // e is rounding error.
616 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
618 // RCP_LO = umulo(RCP, Den) */
619 SDValue RCP_LO = DAG.getNode(ISD::UMULO, DL, VT, RCP, Den);
621 // RCP_HI = mulhu (RCP, Den) */
622 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
624 // NEG_RCP_LO = -RCP_LO
625 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
628 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
629 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
632 // Calculate the rounding error from the URECIP instruction
633 // E = mulhu(ABS_RCP_LO, RCP)
634 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
637 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
640 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
642 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
643 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
646 // Quotient = mulhu(Tmp0, Num)
647 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
649 // Num_S_Remainder = Quotient * Den
650 SDValue Num_S_Remainder = DAG.getNode(ISD::UMULO, DL, VT, Quotient, Den);
652 // Remainder = Num - Num_S_Remainder
653 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
655 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
656 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
657 DAG.getConstant(-1, VT),
658 DAG.getConstant(0, VT),
660 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
661 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
663 DAG.getConstant(-1, VT),
664 DAG.getConstant(0, VT),
666 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
667 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
670 // Calculate Division result:
672 // Quotient_A_One = Quotient + 1
673 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
674 DAG.getConstant(1, VT));
676 // Quotient_S_One = Quotient - 1
677 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
678 DAG.getConstant(1, VT));
680 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
681 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
682 Quotient, Quotient_A_One, ISD::SETEQ);
684 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
685 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
686 Quotient_S_One, Div, ISD::SETEQ);
688 // Calculate Rem result:
690 // Remainder_S_Den = Remainder - Den
691 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
693 // Remainder_A_Den = Remainder + Den
694 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
696 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
697 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
698 Remainder, Remainder_S_Den, ISD::SETEQ);
700 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
701 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
702 Remainder_A_Den, Rem, ISD::SETEQ);
706 return DAG.getMergeValues(Ops, 2, DL);
709 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
710 SelectionDAG &DAG) const {
711 SDValue S0 = Op.getOperand(0);
713 if (Op.getValueType() != MVT::f32 || S0.getValueType() != MVT::i64)
716 // f32 uint_to_fp i64
717 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
718 DAG.getConstant(0, MVT::i32));
719 SDValue FloatLo = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Lo);
720 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
721 DAG.getConstant(1, MVT::i32));
722 SDValue FloatHi = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Hi);
723 FloatHi = DAG.getNode(ISD::FMUL, DL, MVT::f32, FloatHi,
724 DAG.getConstantFP(4294967296.0f, MVT::f32)); // 2^32
725 return DAG.getNode(ISD::FADD, DL, MVT::f32, FloatLo, FloatHi);
729 //===----------------------------------------------------------------------===//
731 //===----------------------------------------------------------------------===//
733 void AMDGPUTargetLowering::getOriginalFunctionArgs(
736 const SmallVectorImpl<ISD::InputArg> &Ins,
737 SmallVectorImpl<ISD::InputArg> &OrigIns) const {
739 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
740 if (Ins[i].ArgVT == Ins[i].VT) {
741 OrigIns.push_back(Ins[i]);
746 if (Ins[i].ArgVT.isVector() && !Ins[i].VT.isVector()) {
747 // Vector has been split into scalars.
748 VT = Ins[i].ArgVT.getVectorElementType();
749 } else if (Ins[i].VT.isVector() && Ins[i].ArgVT.isVector() &&
750 Ins[i].ArgVT.getVectorElementType() !=
751 Ins[i].VT.getVectorElementType()) {
752 // Vector elements have been promoted
755 // Vector has been spilt into smaller vectors.
759 ISD::InputArg Arg(Ins[i].Flags, VT, VT, Ins[i].Used,
760 Ins[i].OrigArgIndex, Ins[i].PartOffset);
761 OrigIns.push_back(Arg);
765 bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
766 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
767 return CFP->isExactlyValue(1.0);
769 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
770 return C->isAllOnesValue();
775 bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
776 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
777 return CFP->getValueAPF().isZero();
779 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
780 return C->isNullValue();
785 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
786 const TargetRegisterClass *RC,
787 unsigned Reg, EVT VT) const {
788 MachineFunction &MF = DAG.getMachineFunction();
789 MachineRegisterInfo &MRI = MF.getRegInfo();
790 unsigned VirtualRegister;
791 if (!MRI.isLiveIn(Reg)) {
792 VirtualRegister = MRI.createVirtualRegister(RC);
793 MRI.addLiveIn(Reg, VirtualRegister);
795 VirtualRegister = MRI.getLiveInVirtReg(Reg);
797 return DAG.getRegister(VirtualRegister, VT);
800 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
802 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
806 NODE_NAME_CASE(CALL);
807 NODE_NAME_CASE(UMUL);
808 NODE_NAME_CASE(DIV_INF);
809 NODE_NAME_CASE(RET_FLAG);
810 NODE_NAME_CASE(BRANCH_COND);
813 NODE_NAME_CASE(DWORDADDR)
814 NODE_NAME_CASE(FRACT)
821 NODE_NAME_CASE(URECIP)
822 NODE_NAME_CASE(EXPORT)
823 NODE_NAME_CASE(CONST_ADDRESS)
824 NODE_NAME_CASE(REGISTER_LOAD)
825 NODE_NAME_CASE(REGISTER_STORE)
826 NODE_NAME_CASE(LOAD_CONSTANT)
827 NODE_NAME_CASE(LOAD_INPUT)
828 NODE_NAME_CASE(SAMPLE)
829 NODE_NAME_CASE(SAMPLEB)
830 NODE_NAME_CASE(SAMPLED)
831 NODE_NAME_CASE(SAMPLEL)
832 NODE_NAME_CASE(STORE_MSKOR)
833 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)