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 "AMDGPURegisterInfo.h"
19 #include "AMDGPUSubtarget.h"
20 #include "AMDILIntrinsicInfo.h"
21 #include "R600MachineFunctionInfo.h"
22 #include "SIMachineFunctionInfo.h"
23 #include "llvm/CodeGen/CallingConvLower.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/SelectionDAG.h"
27 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
28 #include "llvm/IR/DataLayout.h"
31 static bool allocateStack(unsigned ValNo, MVT ValVT, MVT LocVT,
32 CCValAssign::LocInfo LocInfo,
33 ISD::ArgFlagsTy ArgFlags, CCState &State) {
34 unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() / 8, ArgFlags.getOrigAlign());
35 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
40 #include "AMDGPUGenCallingConv.inc"
42 AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM) :
43 TargetLowering(TM, new TargetLoweringObjectFileELF()) {
45 // Initialize target lowering borrowed from AMDIL
48 // We need to custom lower some of the intrinsics
49 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
51 // Library functions. These default to Expand, but we have instructions
53 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
54 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
55 setOperationAction(ISD::FPOW, MVT::f32, Legal);
56 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
57 setOperationAction(ISD::FABS, MVT::f32, Legal);
58 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
59 setOperationAction(ISD::FRINT, MVT::f32, Legal);
61 // The hardware supports ROTR, but not ROTL
62 setOperationAction(ISD::ROTL, MVT::i32, Expand);
64 // Lower floating point store/load to integer store/load to reduce the number
65 // of patterns in tablegen.
66 setOperationAction(ISD::STORE, MVT::f32, Promote);
67 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
69 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
70 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
72 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
73 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
75 setOperationAction(ISD::STORE, MVT::v8f32, Promote);
76 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
78 setOperationAction(ISD::STORE, MVT::v16f32, Promote);
79 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
81 setOperationAction(ISD::STORE, MVT::f64, Promote);
82 AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64);
84 // Custom lowering of vector stores is required for local address space
86 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
87 // XXX: Native v2i32 local address space stores are possible, but not
88 // currently implemented.
89 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
91 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
92 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
93 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
94 // XXX: This can be change to Custom, once ExpandVectorStores can
95 // handle 64-bit stores.
96 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
98 setOperationAction(ISD::LOAD, MVT::f32, Promote);
99 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
101 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
102 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
104 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
105 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
107 setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
108 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
110 setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
111 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
113 setOperationAction(ISD::LOAD, MVT::f64, Promote);
114 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64);
116 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
117 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
118 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
119 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
121 setLoadExtAction(ISD::EXTLOAD, MVT::v2i8, Expand);
122 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i8, Expand);
123 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i8, Expand);
124 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
125 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i8, Expand);
126 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i8, Expand);
127 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, Expand);
128 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, Expand);
129 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, Expand);
130 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, Expand);
131 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, Expand);
132 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, Expand);
134 setOperationAction(ISD::FNEG, MVT::v2f32, Expand);
135 setOperationAction(ISD::FNEG, MVT::v4f32, Expand);
137 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
139 setOperationAction(ISD::MUL, MVT::i64, Expand);
141 setOperationAction(ISD::UDIV, MVT::i32, Expand);
142 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
143 setOperationAction(ISD::UREM, MVT::i32, Expand);
144 setOperationAction(ISD::VSELECT, MVT::v2f32, Expand);
145 setOperationAction(ISD::VSELECT, MVT::v4f32, Expand);
147 static const MVT::SimpleValueType IntTypes[] = {
148 MVT::v2i32, MVT::v4i32
150 const size_t NumIntTypes = array_lengthof(IntTypes);
152 for (unsigned int x = 0; x < NumIntTypes; ++x) {
153 MVT::SimpleValueType VT = IntTypes[x];
154 //Expand the following operations for the current type by default
155 setOperationAction(ISD::ADD, VT, Expand);
156 setOperationAction(ISD::AND, VT, Expand);
157 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
158 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
159 setOperationAction(ISD::MUL, VT, Expand);
160 setOperationAction(ISD::OR, VT, Expand);
161 setOperationAction(ISD::SHL, VT, Expand);
162 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
163 setOperationAction(ISD::SRL, VT, Expand);
164 setOperationAction(ISD::SRA, VT, Expand);
165 setOperationAction(ISD::SUB, VT, Expand);
166 setOperationAction(ISD::UDIV, VT, Expand);
167 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
168 setOperationAction(ISD::UREM, VT, Expand);
169 setOperationAction(ISD::VSELECT, VT, Expand);
170 setOperationAction(ISD::XOR, VT, Expand);
173 static const MVT::SimpleValueType FloatTypes[] = {
174 MVT::v2f32, MVT::v4f32
176 const size_t NumFloatTypes = array_lengthof(FloatTypes);
178 for (unsigned int x = 0; x < NumFloatTypes; ++x) {
179 MVT::SimpleValueType VT = FloatTypes[x];
180 setOperationAction(ISD::FADD, VT, Expand);
181 setOperationAction(ISD::FDIV, VT, Expand);
182 setOperationAction(ISD::FFLOOR, VT, Expand);
183 setOperationAction(ISD::FMUL, VT, Expand);
184 setOperationAction(ISD::FRINT, VT, Expand);
185 setOperationAction(ISD::FSQRT, VT, Expand);
186 setOperationAction(ISD::FSUB, VT, Expand);
190 //===----------------------------------------------------------------------===//
191 // Target Information
192 //===----------------------------------------------------------------------===//
194 MVT AMDGPUTargetLowering::getVectorIdxTy() const {
199 //===---------------------------------------------------------------------===//
201 //===---------------------------------------------------------------------===//
203 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
204 assert(VT.isFloatingPoint());
205 return VT == MVT::f32;
208 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
209 assert(VT.isFloatingPoint());
210 return VT == MVT::f32;
213 //===---------------------------------------------------------------------===//
214 // TargetLowering Callbacks
215 //===---------------------------------------------------------------------===//
217 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
218 const SmallVectorImpl<ISD::InputArg> &Ins) const {
220 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
223 SDValue AMDGPUTargetLowering::LowerReturn(
225 CallingConv::ID CallConv,
227 const SmallVectorImpl<ISD::OutputArg> &Outs,
228 const SmallVectorImpl<SDValue> &OutVals,
229 SDLoc DL, SelectionDAG &DAG) const {
230 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
233 //===---------------------------------------------------------------------===//
234 // Target specific lowering
235 //===---------------------------------------------------------------------===//
237 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
239 switch (Op.getOpcode()) {
241 Op.getNode()->dump();
242 assert(0 && "Custom lowering code for this"
243 "instruction is not implemented yet!");
245 // AMDIL DAG lowering
246 case ISD::SDIV: return LowerSDIV(Op, DAG);
247 case ISD::SREM: return LowerSREM(Op, DAG);
248 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
249 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
250 // AMDGPU DAG lowering
251 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
252 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
253 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
254 case ISD::STORE: return LowerSTORE(Op, DAG);
255 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
256 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
261 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
263 SelectionDAG &DAG) const {
265 const DataLayout *TD = getTargetMachine().getDataLayout();
266 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
268 assert(G->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS);
269 // XXX: What does the value of G->getOffset() mean?
270 assert(G->getOffset() == 0 &&
271 "Do not know what to do with an non-zero offset");
273 const GlobalValue *GV = G->getGlobal();
276 if (MFI->LocalMemoryObjects.count(GV) == 0) {
277 uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
278 Offset = MFI->LDSSize;
279 MFI->LocalMemoryObjects[GV] = Offset;
280 // XXX: Account for alignment?
281 MFI->LDSSize += Size;
283 Offset = MFI->LocalMemoryObjects[GV];
286 return DAG.getConstant(Offset, getPointerTy(G->getAddressSpace()));
289 void AMDGPUTargetLowering::ExtractVectorElements(SDValue Op, SelectionDAG &DAG,
290 SmallVectorImpl<SDValue> &Args,
292 unsigned Count) const {
293 EVT VT = Op.getValueType();
294 for (unsigned i = Start, e = Start + Count; i != e; ++i) {
295 Args.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op),
296 VT.getVectorElementType(),
297 Op, DAG.getConstant(i, MVT::i32)));
301 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
302 SelectionDAG &DAG) const {
303 SmallVector<SDValue, 8> Args;
304 SDValue A = Op.getOperand(0);
305 SDValue B = Op.getOperand(1);
307 ExtractVectorElements(A, DAG, Args, 0,
308 A.getValueType().getVectorNumElements());
309 ExtractVectorElements(B, DAG, Args, 0,
310 B.getValueType().getVectorNumElements());
312 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(),
313 &Args[0], Args.size());
316 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
317 SelectionDAG &DAG) const {
319 SmallVector<SDValue, 8> Args;
320 EVT VT = Op.getValueType();
321 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
322 ExtractVectorElements(Op.getOperand(0), DAG, Args, Start,
323 VT.getVectorNumElements());
325 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(),
326 &Args[0], Args.size());
330 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
331 SelectionDAG &DAG) const {
332 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
334 EVT VT = Op.getValueType();
336 switch (IntrinsicID) {
338 case AMDGPUIntrinsic::AMDIL_abs:
339 return LowerIntrinsicIABS(Op, DAG);
340 case AMDGPUIntrinsic::AMDIL_exp:
341 return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
342 case AMDGPUIntrinsic::AMDGPU_lrp:
343 return LowerIntrinsicLRP(Op, DAG);
344 case AMDGPUIntrinsic::AMDIL_fraction:
345 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
346 case AMDGPUIntrinsic::AMDIL_max:
347 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, Op.getOperand(1),
349 case AMDGPUIntrinsic::AMDGPU_imax:
350 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
352 case AMDGPUIntrinsic::AMDGPU_umax:
353 return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
355 case AMDGPUIntrinsic::AMDIL_min:
356 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, Op.getOperand(1),
358 case AMDGPUIntrinsic::AMDGPU_imin:
359 return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
361 case AMDGPUIntrinsic::AMDGPU_umin:
362 return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
364 case AMDGPUIntrinsic::AMDIL_round_nearest:
365 return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
369 ///IABS(a) = SMAX(sub(0, a), a)
370 SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
371 SelectionDAG &DAG) const {
374 EVT VT = Op.getValueType();
375 SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
378 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
381 /// Linear Interpolation
382 /// LRP(a, b, c) = muladd(a, b, (1 - a) * c)
383 SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
384 SelectionDAG &DAG) const {
386 EVT VT = Op.getValueType();
387 SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
388 DAG.getConstantFP(1.0f, MVT::f32),
390 SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
392 return DAG.getNode(ISD::FADD, DL, VT,
393 DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
397 /// \brief Generate Min/Max node
398 SDValue AMDGPUTargetLowering::LowerMinMax(SDValue Op,
399 SelectionDAG &DAG) const {
401 EVT VT = Op.getValueType();
403 SDValue LHS = Op.getOperand(0);
404 SDValue RHS = Op.getOperand(1);
405 SDValue True = Op.getOperand(2);
406 SDValue False = Op.getOperand(3);
407 SDValue CC = Op.getOperand(4);
409 if (VT != MVT::f32 ||
410 !((LHS == True && RHS == False) || (LHS == False && RHS == True))) {
414 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
428 assert(0 && "Operation should already be optimised !");
436 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
438 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
447 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
449 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
451 case ISD::SETCC_INVALID:
452 assert(0 && "Invalid setcc condcode !");
457 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue &Op,
458 SelectionDAG &DAG) const {
459 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
460 EVT MemEltVT = Load->getMemoryVT().getVectorElementType();
461 EVT EltVT = Op.getValueType().getVectorElementType();
462 EVT PtrVT = Load->getBasePtr().getValueType();
463 unsigned NumElts = Load->getMemoryVT().getVectorNumElements();
464 SmallVector<SDValue, 8> Loads;
467 for (unsigned i = 0, e = NumElts; i != e; ++i) {
468 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, Load->getBasePtr(),
469 DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8), PtrVT));
470 Loads.push_back(DAG.getExtLoad(Load->getExtensionType(), SL, EltVT,
471 Load->getChain(), Ptr,
472 MachinePointerInfo(Load->getMemOperand()->getValue()),
473 MemEltVT, Load->isVolatile(), Load->isNonTemporal(),
474 Load->getAlignment()));
476 return DAG.getNode(ISD::BUILD_VECTOR, SL, Op.getValueType(), &Loads[0],
480 SDValue AMDGPUTargetLowering::MergeVectorStore(const SDValue &Op,
481 SelectionDAG &DAG) const {
482 StoreSDNode *Store = dyn_cast<StoreSDNode>(Op);
483 EVT MemVT = Store->getMemoryVT();
484 unsigned MemBits = MemVT.getSizeInBits();
486 // Byte stores are really expensive, so if possible, try to pack
487 // 32-bit vector truncatating store into an i32 store.
488 // XXX: We could also handle optimize other vector bitwidths
489 if (!MemVT.isVector() || MemBits > 32) {
494 const SDValue &Value = Store->getValue();
495 EVT VT = Value.getValueType();
496 const SDValue &Ptr = Store->getBasePtr();
497 EVT MemEltVT = MemVT.getVectorElementType();
498 unsigned MemEltBits = MemEltVT.getSizeInBits();
499 unsigned MemNumElements = MemVT.getVectorNumElements();
500 EVT PackedVT = EVT::getIntegerVT(*DAG.getContext(), MemVT.getSizeInBits());
504 Mask = DAG.getConstant(0xFF, PackedVT);
507 Mask = DAG.getConstant(0xFFFF, PackedVT);
510 llvm_unreachable("Cannot lower this vector store");
513 for (unsigned i = 0; i < MemNumElements; ++i) {
514 EVT ElemVT = VT.getVectorElementType();
515 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT, Value,
516 DAG.getConstant(i, MVT::i32));
517 Elt = DAG.getZExtOrTrunc(Elt, DL, PackedVT);
518 Elt = DAG.getNode(ISD::AND, DL, PackedVT, Elt, Mask);
519 SDValue Shift = DAG.getConstant(MemEltBits * i, PackedVT);
520 Elt = DAG.getNode(ISD::SHL, DL, PackedVT, Elt, Shift);
524 PackedValue = DAG.getNode(ISD::OR, DL, PackedVT, PackedValue, Elt);
527 return DAG.getStore(Store->getChain(), DL, PackedValue, Ptr,
528 MachinePointerInfo(Store->getMemOperand()->getValue()),
529 Store->isVolatile(), Store->isNonTemporal(),
530 Store->getAlignment());
533 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
534 SelectionDAG &DAG) const {
535 StoreSDNode *Store = cast<StoreSDNode>(Op);
536 EVT MemEltVT = Store->getMemoryVT().getVectorElementType();
537 EVT EltVT = Store->getValue().getValueType().getVectorElementType();
538 EVT PtrVT = Store->getBasePtr().getValueType();
539 unsigned NumElts = Store->getMemoryVT().getVectorNumElements();
542 SmallVector<SDValue, 8> Chains;
544 for (unsigned i = 0, e = NumElts; i != e; ++i) {
545 SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
546 Store->getValue(), DAG.getConstant(i, MVT::i32));
547 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT,
549 DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8),
551 Chains.push_back(DAG.getTruncStore(Store->getChain(), SL, Val, Ptr,
552 MachinePointerInfo(Store->getMemOperand()->getValue()),
553 MemEltVT, Store->isVolatile(), Store->isNonTemporal(),
554 Store->getAlignment()));
556 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, &Chains[0], NumElts);
559 SDValue AMDGPUTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
560 SDValue Result = AMDGPUTargetLowering::MergeVectorStore(Op, DAG);
561 if (Result.getNode()) {
565 StoreSDNode *Store = cast<StoreSDNode>(Op);
566 if (Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS &&
567 Store->getValue().getValueType().isVector()) {
568 return SplitVectorStore(Op, DAG);
573 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
574 SelectionDAG &DAG) const {
576 EVT VT = Op.getValueType();
578 SDValue Num = Op.getOperand(0);
579 SDValue Den = Op.getOperand(1);
581 SmallVector<SDValue, 8> Results;
583 // RCP = URECIP(Den) = 2^32 / Den + e
584 // e is rounding error.
585 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
587 // RCP_LO = umulo(RCP, Den) */
588 SDValue RCP_LO = DAG.getNode(ISD::UMULO, DL, VT, RCP, Den);
590 // RCP_HI = mulhu (RCP, Den) */
591 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
593 // NEG_RCP_LO = -RCP_LO
594 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
597 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
598 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
601 // Calculate the rounding error from the URECIP instruction
602 // E = mulhu(ABS_RCP_LO, RCP)
603 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
606 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
609 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
611 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
612 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
615 // Quotient = mulhu(Tmp0, Num)
616 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
618 // Num_S_Remainder = Quotient * Den
619 SDValue Num_S_Remainder = DAG.getNode(ISD::UMULO, DL, VT, Quotient, Den);
621 // Remainder = Num - Num_S_Remainder
622 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
624 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
625 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
626 DAG.getConstant(-1, VT),
627 DAG.getConstant(0, VT),
629 // Remainder_GE_Zero = (Remainder >= 0 ? -1 : 0)
630 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Remainder,
631 DAG.getConstant(0, VT),
632 DAG.getConstant(-1, VT),
633 DAG.getConstant(0, VT),
635 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
636 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
639 // Calculate Division result:
641 // Quotient_A_One = Quotient + 1
642 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
643 DAG.getConstant(1, VT));
645 // Quotient_S_One = Quotient - 1
646 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
647 DAG.getConstant(1, VT));
649 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
650 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
651 Quotient, Quotient_A_One, ISD::SETEQ);
653 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
654 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
655 Quotient_S_One, Div, ISD::SETEQ);
657 // Calculate Rem result:
659 // Remainder_S_Den = Remainder - Den
660 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
662 // Remainder_A_Den = Remainder + Den
663 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
665 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
666 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
667 Remainder, Remainder_S_Den, ISD::SETEQ);
669 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
670 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
671 Remainder_A_Den, Rem, ISD::SETEQ);
675 return DAG.getMergeValues(Ops, 2, DL);
678 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
679 SelectionDAG &DAG) const {
680 SDValue S0 = Op.getOperand(0);
682 if (Op.getValueType() != MVT::f32 || S0.getValueType() != MVT::i64)
685 // f32 uint_to_fp i64
686 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
687 DAG.getConstant(0, MVT::i32));
688 SDValue FloatLo = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Lo);
689 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
690 DAG.getConstant(1, MVT::i32));
691 SDValue FloatHi = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Hi);
692 FloatHi = DAG.getNode(ISD::FMUL, DL, MVT::f32, FloatHi,
693 DAG.getConstantFP(4294967296.0f, MVT::f32)); // 2^32
694 return DAG.getNode(ISD::FADD, DL, MVT::f32, FloatLo, FloatHi);
698 //===----------------------------------------------------------------------===//
700 //===----------------------------------------------------------------------===//
702 void AMDGPUTargetLowering::getOriginalFunctionArgs(
705 const SmallVectorImpl<ISD::InputArg> &Ins,
706 SmallVectorImpl<ISD::InputArg> &OrigIns) const {
708 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
709 if (Ins[i].ArgVT == Ins[i].VT) {
710 OrigIns.push_back(Ins[i]);
715 if (Ins[i].ArgVT.isVector() && !Ins[i].VT.isVector()) {
716 // Vector has been split into scalars.
717 VT = Ins[i].ArgVT.getVectorElementType();
718 } else if (Ins[i].VT.isVector() && Ins[i].ArgVT.isVector() &&
719 Ins[i].ArgVT.getVectorElementType() !=
720 Ins[i].VT.getVectorElementType()) {
721 // Vector elements have been promoted
724 // Vector has been spilt into smaller vectors.
728 ISD::InputArg Arg(Ins[i].Flags, VT, VT, Ins[i].Used,
729 Ins[i].OrigArgIndex, Ins[i].PartOffset);
730 OrigIns.push_back(Arg);
734 bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
735 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
736 return CFP->isExactlyValue(1.0);
738 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
739 return C->isAllOnesValue();
744 bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
745 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
746 return CFP->getValueAPF().isZero();
748 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
749 return C->isNullValue();
754 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
755 const TargetRegisterClass *RC,
756 unsigned Reg, EVT VT) const {
757 MachineFunction &MF = DAG.getMachineFunction();
758 MachineRegisterInfo &MRI = MF.getRegInfo();
759 unsigned VirtualRegister;
760 if (!MRI.isLiveIn(Reg)) {
761 VirtualRegister = MRI.createVirtualRegister(RC);
762 MRI.addLiveIn(Reg, VirtualRegister);
764 VirtualRegister = MRI.getLiveInVirtReg(Reg);
766 return DAG.getRegister(VirtualRegister, VT);
769 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
771 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
775 NODE_NAME_CASE(CALL);
776 NODE_NAME_CASE(UMUL);
777 NODE_NAME_CASE(DIV_INF);
778 NODE_NAME_CASE(RET_FLAG);
779 NODE_NAME_CASE(BRANCH_COND);
782 NODE_NAME_CASE(DWORDADDR)
783 NODE_NAME_CASE(FRACT)
790 NODE_NAME_CASE(URECIP)
791 NODE_NAME_CASE(EXPORT)
792 NODE_NAME_CASE(CONST_ADDRESS)
793 NODE_NAME_CASE(REGISTER_LOAD)
794 NODE_NAME_CASE(REGISTER_STORE)
795 NODE_NAME_CASE(LOAD_CONSTANT)
796 NODE_NAME_CASE(LOAD_INPUT)
797 NODE_NAME_CASE(SAMPLE)
798 NODE_NAME_CASE(SAMPLEB)
799 NODE_NAME_CASE(SAMPLED)
800 NODE_NAME_CASE(SAMPLEL)
801 NODE_NAME_CASE(STORE_MSKOR)
802 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)