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"
33 static bool allocateStack(unsigned ValNo, MVT ValVT, MVT LocVT,
34 CCValAssign::LocInfo LocInfo,
35 ISD::ArgFlagsTy ArgFlags, CCState &State) {
36 unsigned Offset = State.AllocateStack(ValVT.getStoreSize(),
37 ArgFlags.getOrigAlign());
38 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
43 #include "AMDGPUGenCallingConv.inc"
45 AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM) :
46 TargetLowering(TM, new TargetLoweringObjectFileELF()) {
48 Subtarget = &TM.getSubtarget<AMDGPUSubtarget>();
50 // Initialize target lowering borrowed from AMDIL
53 // We need to custom lower some of the intrinsics
54 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
56 // Library functions. These default to Expand, but we have instructions
58 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
59 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
60 setOperationAction(ISD::FPOW, MVT::f32, Legal);
61 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
62 setOperationAction(ISD::FABS, MVT::f32, Legal);
63 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
64 setOperationAction(ISD::FRINT, MVT::f32, Legal);
65 setOperationAction(ISD::FROUND, MVT::f32, Legal);
66 setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
68 // The hardware supports ROTR, but not ROTL
69 setOperationAction(ISD::ROTL, MVT::i32, Expand);
71 // Lower floating point store/load to integer store/load to reduce the number
72 // of patterns in tablegen.
73 setOperationAction(ISD::STORE, MVT::f32, Promote);
74 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
76 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
77 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
79 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
80 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
82 setOperationAction(ISD::STORE, MVT::v8f32, Promote);
83 AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
85 setOperationAction(ISD::STORE, MVT::v16f32, Promote);
86 AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
88 setOperationAction(ISD::STORE, MVT::f64, Promote);
89 AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64);
91 // Custom lowering of vector stores is required for local address space
93 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
94 // XXX: Native v2i32 local address space stores are possible, but not
95 // currently implemented.
96 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
98 setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
99 setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
100 setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
102 // XXX: This can be change to Custom, once ExpandVectorStores can
103 // handle 64-bit stores.
104 setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
106 setTruncStoreAction(MVT::i64, MVT::i1, Expand);
107 setTruncStoreAction(MVT::v2i64, MVT::v2i1, Expand);
108 setTruncStoreAction(MVT::v4i64, MVT::v4i1, Expand);
111 setOperationAction(ISD::LOAD, MVT::f32, Promote);
112 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
114 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
115 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
117 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
118 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
120 setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
121 AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
123 setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
124 AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
126 setOperationAction(ISD::LOAD, MVT::f64, Promote);
127 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64);
129 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
130 setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
131 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
132 setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
133 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
134 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
135 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f32, Custom);
136 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
137 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f32, Custom);
138 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i32, Custom);
140 setLoadExtAction(ISD::EXTLOAD, MVT::v2i8, Expand);
141 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i8, Expand);
142 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i8, Expand);
143 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
144 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i8, Expand);
145 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i8, Expand);
146 setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, Expand);
147 setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, Expand);
148 setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, Expand);
149 setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, Expand);
150 setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, Expand);
151 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, Expand);
153 setOperationAction(ISD::BR_CC, MVT::i1, Expand);
155 setOperationAction(ISD::FNEG, MVT::v2f32, Expand);
156 setOperationAction(ISD::FNEG, MVT::v4f32, Expand);
158 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
160 setOperationAction(ISD::MUL, MVT::i64, Expand);
162 setOperationAction(ISD::UDIV, MVT::i32, Expand);
163 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
164 setOperationAction(ISD::UREM, MVT::i32, Expand);
165 setOperationAction(ISD::VSELECT, MVT::v2f32, Expand);
166 setOperationAction(ISD::VSELECT, MVT::v4f32, Expand);
168 static const MVT::SimpleValueType IntTypes[] = {
169 MVT::v2i32, MVT::v4i32
171 const size_t NumIntTypes = array_lengthof(IntTypes);
173 for (unsigned int x = 0; x < NumIntTypes; ++x) {
174 MVT::SimpleValueType VT = IntTypes[x];
175 //Expand the following operations for the current type by default
176 setOperationAction(ISD::ADD, VT, Expand);
177 setOperationAction(ISD::AND, VT, Expand);
178 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
179 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
180 setOperationAction(ISD::MUL, VT, Expand);
181 setOperationAction(ISD::OR, VT, Expand);
182 setOperationAction(ISD::SHL, VT, Expand);
183 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
184 setOperationAction(ISD::SRL, VT, Expand);
185 setOperationAction(ISD::SRA, VT, Expand);
186 setOperationAction(ISD::SUB, VT, Expand);
187 setOperationAction(ISD::UDIV, VT, Expand);
188 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
189 setOperationAction(ISD::UREM, VT, Expand);
190 setOperationAction(ISD::SELECT, VT, Expand);
191 setOperationAction(ISD::VSELECT, VT, Expand);
192 setOperationAction(ISD::XOR, VT, Expand);
195 static const MVT::SimpleValueType FloatTypes[] = {
196 MVT::v2f32, MVT::v4f32
198 const size_t NumFloatTypes = array_lengthof(FloatTypes);
200 for (unsigned int x = 0; x < NumFloatTypes; ++x) {
201 MVT::SimpleValueType VT = FloatTypes[x];
202 setOperationAction(ISD::FABS, VT, Expand);
203 setOperationAction(ISD::FADD, VT, Expand);
204 setOperationAction(ISD::FDIV, VT, Expand);
205 setOperationAction(ISD::FPOW, VT, Expand);
206 setOperationAction(ISD::FFLOOR, VT, Expand);
207 setOperationAction(ISD::FTRUNC, VT, Expand);
208 setOperationAction(ISD::FMUL, VT, Expand);
209 setOperationAction(ISD::FRINT, VT, Expand);
210 setOperationAction(ISD::FSQRT, VT, Expand);
211 setOperationAction(ISD::FSUB, VT, Expand);
212 setOperationAction(ISD::SELECT, VT, Expand);
215 setTargetDAGCombine(ISD::MUL);
218 //===----------------------------------------------------------------------===//
219 // Target Information
220 //===----------------------------------------------------------------------===//
222 MVT AMDGPUTargetLowering::getVectorIdxTy() const {
226 bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy,
228 if (LoadTy.getSizeInBits() != CastTy.getSizeInBits())
231 unsigned LScalarSize = LoadTy.getScalarType().getSizeInBits();
232 unsigned CastScalarSize = CastTy.getScalarType().getSizeInBits();
234 return ((LScalarSize <= CastScalarSize) ||
235 (CastScalarSize >= 32) ||
239 //===---------------------------------------------------------------------===//
241 //===---------------------------------------------------------------------===//
243 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
244 assert(VT.isFloatingPoint());
245 return VT == MVT::f32;
248 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
249 assert(VT.isFloatingPoint());
250 return VT == MVT::f32;
253 bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const {
254 // Truncate is just accessing a subregister.
255 return Dest.bitsLT(Source) && (Dest.getSizeInBits() % 32 == 0);
258 bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const {
259 // Truncate is just accessing a subregister.
260 return Dest->getPrimitiveSizeInBits() < Source->getPrimitiveSizeInBits() &&
261 (Dest->getPrimitiveSizeInBits() % 32 == 0);
264 bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const {
265 const DataLayout *DL = getDataLayout();
266 unsigned SrcSize = DL->getTypeSizeInBits(Src->getScalarType());
267 unsigned DestSize = DL->getTypeSizeInBits(Dest->getScalarType());
269 return SrcSize == 32 && DestSize == 64;
272 bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const {
273 // Any register load of a 64-bit value really requires 2 32-bit moves. For all
274 // practical purposes, the extra mov 0 to load a 64-bit is free. As used,
275 // this will enable reducing 64-bit operations the 32-bit, which is always
277 return Src == MVT::i32 && Dest == MVT::i64;
280 bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const {
281 // There aren't really 64-bit registers, but pairs of 32-bit ones and only a
282 // limited number of native 64-bit operations. Shrinking an operation to fit
283 // in a single 32-bit register should always be helpful. As currently used,
284 // this is much less general than the name suggests, and is only used in
285 // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is
286 // not profitable, and may actually be harmful.
287 return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32;
290 //===---------------------------------------------------------------------===//
291 // TargetLowering Callbacks
292 //===---------------------------------------------------------------------===//
294 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
295 const SmallVectorImpl<ISD::InputArg> &Ins) const {
297 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
300 SDValue AMDGPUTargetLowering::LowerReturn(
302 CallingConv::ID CallConv,
304 const SmallVectorImpl<ISD::OutputArg> &Outs,
305 const SmallVectorImpl<SDValue> &OutVals,
306 SDLoc DL, SelectionDAG &DAG) const {
307 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
310 //===---------------------------------------------------------------------===//
311 // Target specific lowering
312 //===---------------------------------------------------------------------===//
314 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
316 switch (Op.getOpcode()) {
318 Op.getNode()->dump();
319 llvm_unreachable("Custom lowering code for this"
320 "instruction is not implemented yet!");
322 // AMDIL DAG lowering
323 case ISD::SDIV: return LowerSDIV(Op, DAG);
324 case ISD::SREM: return LowerSREM(Op, DAG);
325 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
326 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
327 // AMDGPU DAG lowering
328 case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
329 case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
330 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
331 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
332 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
333 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
338 void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N,
339 SmallVectorImpl<SDValue> &Results,
340 SelectionDAG &DAG) const {
341 switch (N->getOpcode()) {
342 case ISD::SIGN_EXTEND_INREG:
343 // Different parts of legalization seem to interpret which type of
344 // sign_extend_inreg is the one to check for custom lowering. The extended
345 // from type is what really matters, but some places check for custom
346 // lowering of the result type. This results in trying to use
347 // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do
348 // nothing here and let the illegal result integer be handled normally.
356 SDValue AMDGPUTargetLowering::LowerConstantInitializer(const Constant* Init,
357 const GlobalValue *GV,
358 const SDValue &InitPtr,
360 SelectionDAG &DAG) const {
361 const DataLayout *TD = getTargetMachine().getDataLayout();
363 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Init)) {
364 EVT VT = EVT::getEVT(CI->getType());
365 PointerType *PtrTy = PointerType::get(CI->getType(), 0);
366 return DAG.getStore(Chain, DL, DAG.getConstant(*CI, VT), InitPtr,
367 MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
368 TD->getPrefTypeAlignment(CI->getType()));
369 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Init)) {
370 EVT VT = EVT::getEVT(CFP->getType());
371 PointerType *PtrTy = PointerType::get(CFP->getType(), 0);
372 return DAG.getStore(Chain, DL, DAG.getConstantFP(*CFP, VT), InitPtr,
373 MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
374 TD->getPrefTypeAlignment(CFP->getType()));
375 } else if (Init->getType()->isAggregateType()) {
376 EVT PtrVT = InitPtr.getValueType();
377 unsigned NumElements = Init->getType()->getArrayNumElements();
378 SmallVector<SDValue, 8> Chains;
379 for (unsigned i = 0; i < NumElements; ++i) {
380 SDValue Offset = DAG.getConstant(i * TD->getTypeAllocSize(
381 Init->getType()->getArrayElementType()), PtrVT);
382 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, InitPtr, Offset);
383 Chains.push_back(LowerConstantInitializer(Init->getAggregateElement(i),
384 GV, Ptr, Chain, DAG));
386 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
387 Chains.data(), Chains.size());
390 llvm_unreachable("Unhandled constant initializer");
394 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
396 SelectionDAG &DAG) const {
398 const DataLayout *TD = getTargetMachine().getDataLayout();
399 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
400 const GlobalValue *GV = G->getGlobal();
402 switch (G->getAddressSpace()) {
403 default: llvm_unreachable("Global Address lowering not implemented for this "
405 case AMDGPUAS::LOCAL_ADDRESS: {
406 // XXX: What does the value of G->getOffset() mean?
407 assert(G->getOffset() == 0 &&
408 "Do not know what to do with an non-zero offset");
411 if (MFI->LocalMemoryObjects.count(GV) == 0) {
412 uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
413 Offset = MFI->LDSSize;
414 MFI->LocalMemoryObjects[GV] = Offset;
415 // XXX: Account for alignment?
416 MFI->LDSSize += Size;
418 Offset = MFI->LocalMemoryObjects[GV];
421 return DAG.getConstant(Offset, getPointerTy(G->getAddressSpace()));
423 case AMDGPUAS::CONSTANT_ADDRESS: {
424 MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
425 Type *EltType = GV->getType()->getElementType();
426 unsigned Size = TD->getTypeAllocSize(EltType);
427 unsigned Alignment = TD->getPrefTypeAlignment(EltType);
429 const GlobalVariable *Var = dyn_cast<GlobalVariable>(GV);
430 const Constant *Init = Var->getInitializer();
431 int FI = FrameInfo->CreateStackObject(Size, Alignment, false);
432 SDValue InitPtr = DAG.getFrameIndex(FI,
433 getPointerTy(AMDGPUAS::PRIVATE_ADDRESS));
434 SmallVector<SDNode*, 8> WorkList;
436 for (SDNode::use_iterator I = DAG.getEntryNode()->use_begin(),
437 E = DAG.getEntryNode()->use_end(); I != E; ++I) {
438 if (I->getOpcode() != AMDGPUISD::REGISTER_LOAD && I->getOpcode() != ISD::LOAD)
440 WorkList.push_back(*I);
442 SDValue Chain = LowerConstantInitializer(Init, GV, InitPtr, DAG.getEntryNode(), DAG);
443 for (SmallVector<SDNode*, 8>::iterator I = WorkList.begin(),
444 E = WorkList.end(); I != E; ++I) {
445 SmallVector<SDValue, 8> Ops;
446 Ops.push_back(Chain);
447 for (unsigned i = 1; i < (*I)->getNumOperands(); ++i) {
448 Ops.push_back((*I)->getOperand(i));
450 DAG.UpdateNodeOperands(*I, Ops.data(), Ops.size());
452 return DAG.getZExtOrTrunc(InitPtr, SDLoc(Op),
453 getPointerTy(AMDGPUAS::CONSTANT_ADDRESS));
458 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
459 SelectionDAG &DAG) const {
460 SmallVector<SDValue, 8> Args;
461 SDValue A = Op.getOperand(0);
462 SDValue B = Op.getOperand(1);
464 DAG.ExtractVectorElements(A, Args);
465 DAG.ExtractVectorElements(B, Args);
467 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(),
468 Args.data(), Args.size());
471 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
472 SelectionDAG &DAG) const {
474 SmallVector<SDValue, 8> Args;
475 unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
476 EVT VT = Op.getValueType();
477 DAG.ExtractVectorElements(Op.getOperand(0), Args, Start,
478 VT.getVectorNumElements());
480 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(),
481 Args.data(), Args.size());
484 SDValue AMDGPUTargetLowering::LowerFrameIndex(SDValue Op,
485 SelectionDAG &DAG) const {
487 MachineFunction &MF = DAG.getMachineFunction();
488 const AMDGPUFrameLowering *TFL =
489 static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
491 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
494 unsigned FrameIndex = FIN->getIndex();
495 unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
496 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF),
500 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
501 SelectionDAG &DAG) const {
502 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
504 EVT VT = Op.getValueType();
506 switch (IntrinsicID) {
508 case AMDGPUIntrinsic::AMDIL_abs:
509 return LowerIntrinsicIABS(Op, DAG);
510 case AMDGPUIntrinsic::AMDIL_exp:
511 return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
512 case AMDGPUIntrinsic::AMDGPU_lrp:
513 return LowerIntrinsicLRP(Op, DAG);
514 case AMDGPUIntrinsic::AMDIL_fraction:
515 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
516 case AMDGPUIntrinsic::AMDIL_max:
517 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, Op.getOperand(1),
519 case AMDGPUIntrinsic::AMDGPU_imax:
520 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
522 case AMDGPUIntrinsic::AMDGPU_umax:
523 return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
525 case AMDGPUIntrinsic::AMDIL_min:
526 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, Op.getOperand(1),
528 case AMDGPUIntrinsic::AMDGPU_imin:
529 return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
531 case AMDGPUIntrinsic::AMDGPU_umin:
532 return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
535 case AMDGPUIntrinsic::AMDGPU_bfe_i32:
536 return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT,
541 case AMDGPUIntrinsic::AMDGPU_bfe_u32:
542 return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT,
547 case AMDGPUIntrinsic::AMDGPU_bfi:
548 return DAG.getNode(AMDGPUISD::BFI, DL, VT,
553 case AMDGPUIntrinsic::AMDGPU_bfm:
554 return DAG.getNode(AMDGPUISD::BFM, DL, VT,
558 case AMDGPUIntrinsic::AMDIL_round_nearest:
559 return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
563 ///IABS(a) = SMAX(sub(0, a), a)
564 SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
565 SelectionDAG &DAG) const {
568 EVT VT = Op.getValueType();
569 SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
572 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
575 /// Linear Interpolation
576 /// LRP(a, b, c) = muladd(a, b, (1 - a) * c)
577 SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
578 SelectionDAG &DAG) const {
580 EVT VT = Op.getValueType();
581 SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
582 DAG.getConstantFP(1.0f, MVT::f32),
584 SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
586 return DAG.getNode(ISD::FADD, DL, VT,
587 DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
591 /// \brief Generate Min/Max node
592 SDValue AMDGPUTargetLowering::LowerMinMax(SDValue Op,
593 SelectionDAG &DAG) const {
595 EVT VT = Op.getValueType();
597 SDValue LHS = Op.getOperand(0);
598 SDValue RHS = Op.getOperand(1);
599 SDValue True = Op.getOperand(2);
600 SDValue False = Op.getOperand(3);
601 SDValue CC = Op.getOperand(4);
603 if (VT != MVT::f32 ||
604 !((LHS == True && RHS == False) || (LHS == False && RHS == True))) {
608 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
622 llvm_unreachable("Operation should already be optimised!");
630 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
632 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
641 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
643 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
645 case ISD::SETCC_INVALID:
646 llvm_unreachable("Invalid setcc condcode!");
651 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue &Op,
652 SelectionDAG &DAG) const {
653 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
654 EVT MemEltVT = Load->getMemoryVT().getVectorElementType();
655 EVT EltVT = Op.getValueType().getVectorElementType();
656 EVT PtrVT = Load->getBasePtr().getValueType();
657 unsigned NumElts = Load->getMemoryVT().getVectorNumElements();
658 SmallVector<SDValue, 8> Loads;
661 for (unsigned i = 0, e = NumElts; i != e; ++i) {
662 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, Load->getBasePtr(),
663 DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8), PtrVT));
664 Loads.push_back(DAG.getExtLoad(Load->getExtensionType(), SL, EltVT,
665 Load->getChain(), Ptr,
666 MachinePointerInfo(Load->getMemOperand()->getValue()),
667 MemEltVT, Load->isVolatile(), Load->isNonTemporal(),
668 Load->getAlignment()));
670 return DAG.getNode(ISD::BUILD_VECTOR, SL, Op.getValueType(),
671 Loads.data(), Loads.size());
674 SDValue AMDGPUTargetLowering::MergeVectorStore(const SDValue &Op,
675 SelectionDAG &DAG) const {
676 StoreSDNode *Store = dyn_cast<StoreSDNode>(Op);
677 EVT MemVT = Store->getMemoryVT();
678 unsigned MemBits = MemVT.getSizeInBits();
680 // Byte stores are really expensive, so if possible, try to pack 32-bit vector
681 // truncating store into an i32 store.
682 // XXX: We could also handle optimize other vector bitwidths.
683 if (!MemVT.isVector() || MemBits > 32) {
688 SDValue Value = Store->getValue();
689 EVT VT = Value.getValueType();
690 EVT ElemVT = VT.getVectorElementType();
691 SDValue Ptr = Store->getBasePtr();
692 EVT MemEltVT = MemVT.getVectorElementType();
693 unsigned MemEltBits = MemEltVT.getSizeInBits();
694 unsigned MemNumElements = MemVT.getVectorNumElements();
695 unsigned PackedSize = MemVT.getStoreSizeInBits();
696 SDValue Mask = DAG.getConstant((1 << MemEltBits) - 1, MVT::i32);
698 assert(Value.getValueType().getScalarSizeInBits() >= 32);
701 for (unsigned i = 0; i < MemNumElements; ++i) {
702 SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT, Value,
703 DAG.getConstant(i, MVT::i32));
704 Elt = DAG.getZExtOrTrunc(Elt, DL, MVT::i32);
705 Elt = DAG.getNode(ISD::AND, DL, MVT::i32, Elt, Mask); // getZeroExtendInReg
707 SDValue Shift = DAG.getConstant(MemEltBits * i, MVT::i32);
708 Elt = DAG.getNode(ISD::SHL, DL, MVT::i32, Elt, Shift);
713 PackedValue = DAG.getNode(ISD::OR, DL, MVT::i32, PackedValue, Elt);
717 if (PackedSize < 32) {
718 EVT PackedVT = EVT::getIntegerVT(*DAG.getContext(), PackedSize);
719 return DAG.getTruncStore(Store->getChain(), DL, PackedValue, Ptr,
720 Store->getMemOperand()->getPointerInfo(),
722 Store->isNonTemporal(), Store->isVolatile(),
723 Store->getAlignment());
726 return DAG.getStore(Store->getChain(), DL, PackedValue, Ptr,
727 Store->getMemOperand()->getPointerInfo(),
728 Store->isVolatile(), Store->isNonTemporal(),
729 Store->getAlignment());
732 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
733 SelectionDAG &DAG) const {
734 StoreSDNode *Store = cast<StoreSDNode>(Op);
735 EVT MemEltVT = Store->getMemoryVT().getVectorElementType();
736 EVT EltVT = Store->getValue().getValueType().getVectorElementType();
737 EVT PtrVT = Store->getBasePtr().getValueType();
738 unsigned NumElts = Store->getMemoryVT().getVectorNumElements();
741 SmallVector<SDValue, 8> Chains;
743 for (unsigned i = 0, e = NumElts; i != e; ++i) {
744 SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
745 Store->getValue(), DAG.getConstant(i, MVT::i32));
746 SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT,
748 DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8),
750 Chains.push_back(DAG.getTruncStore(Store->getChain(), SL, Val, Ptr,
751 MachinePointerInfo(Store->getMemOperand()->getValue()),
752 MemEltVT, Store->isVolatile(), Store->isNonTemporal(),
753 Store->getAlignment()));
755 return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, Chains.data(), NumElts);
758 SDValue AMDGPUTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
760 LoadSDNode *Load = cast<LoadSDNode>(Op);
761 ISD::LoadExtType ExtType = Load->getExtensionType();
762 EVT VT = Op.getValueType();
763 EVT MemVT = Load->getMemoryVT();
765 if (ExtType != ISD::NON_EXTLOAD && !VT.isVector() && VT.getSizeInBits() > 32) {
766 // We can do the extload to 32-bits, and then need to separately extend to
769 SDValue ExtLoad32 = DAG.getExtLoad(ExtType, DL, MVT::i32,
773 Load->getMemOperand());
774 return DAG.getNode(ISD::getExtForLoadExtType(ExtType), DL, VT, ExtLoad32);
777 if (ExtType == ISD::NON_EXTLOAD && VT.getSizeInBits() < 32) {
778 assert(VT == MVT::i1 && "Only i1 non-extloads expected");
779 // FIXME: Copied from PPC
780 // First, load into 32 bits, then truncate to 1 bit.
782 SDValue Chain = Load->getChain();
783 SDValue BasePtr = Load->getBasePtr();
784 MachineMemOperand *MMO = Load->getMemOperand();
786 SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
787 BasePtr, MVT::i8, MMO);
788 return DAG.getNode(ISD::TRUNCATE, DL, VT, NewLD);
791 // Lower loads constant address space global variable loads
792 if (Load->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS &&
794 GetUnderlyingObject(Load->getMemOperand()->getValue()))) {
796 SDValue Ptr = DAG.getZExtOrTrunc(Load->getBasePtr(), DL,
797 getPointerTy(AMDGPUAS::PRIVATE_ADDRESS));
798 Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
799 DAG.getConstant(2, MVT::i32));
800 return DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
801 Load->getChain(), Ptr,
802 DAG.getTargetConstant(0, MVT::i32), Op.getOperand(2));
805 if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS ||
806 ExtType == ISD::NON_EXTLOAD || Load->getMemoryVT().bitsGE(MVT::i32))
810 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
811 DAG.getConstant(2, MVT::i32));
812 SDValue Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
813 Load->getChain(), Ptr,
814 DAG.getTargetConstant(0, MVT::i32),
816 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
818 DAG.getConstant(0x3, MVT::i32));
819 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
820 DAG.getConstant(3, MVT::i32));
822 Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Ret, ShiftAmt);
824 EVT MemEltVT = MemVT.getScalarType();
825 if (ExtType == ISD::SEXTLOAD) {
826 SDValue MemEltVTNode = DAG.getValueType(MemEltVT);
827 return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode);
830 return DAG.getZeroExtendInReg(Ret, DL, MemEltVT);
833 SDValue AMDGPUTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
835 SDValue Result = AMDGPUTargetLowering::MergeVectorStore(Op, DAG);
836 if (Result.getNode()) {
840 StoreSDNode *Store = cast<StoreSDNode>(Op);
841 SDValue Chain = Store->getChain();
842 if ((Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
843 Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) &&
844 Store->getValue().getValueType().isVector()) {
845 return SplitVectorStore(Op, DAG);
848 EVT MemVT = Store->getMemoryVT();
849 if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS &&
850 MemVT.bitsLT(MVT::i32)) {
852 if (Store->getMemoryVT() == MVT::i8) {
854 } else if (Store->getMemoryVT() == MVT::i16) {
857 SDValue BasePtr = Store->getBasePtr();
858 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, BasePtr,
859 DAG.getConstant(2, MVT::i32));
860 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
861 Chain, Ptr, DAG.getTargetConstant(0, MVT::i32));
863 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, BasePtr,
864 DAG.getConstant(0x3, MVT::i32));
866 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
867 DAG.getConstant(3, MVT::i32));
869 SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
872 SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT);
874 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
875 MaskedValue, ShiftAmt);
877 SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32, DAG.getConstant(Mask, MVT::i32),
879 DstMask = DAG.getNode(ISD::XOR, DL, MVT::i32, DstMask,
880 DAG.getConstant(0xffffffff, MVT::i32));
881 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
883 SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
884 return DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
885 Chain, Value, Ptr, DAG.getTargetConstant(0, MVT::i32));
890 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
891 SelectionDAG &DAG) const {
893 EVT VT = Op.getValueType();
895 SDValue Num = Op.getOperand(0);
896 SDValue Den = Op.getOperand(1);
898 SmallVector<SDValue, 8> Results;
900 // RCP = URECIP(Den) = 2^32 / Den + e
901 // e is rounding error.
902 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
904 // RCP_LO = umulo(RCP, Den) */
905 SDValue RCP_LO = DAG.getNode(ISD::UMULO, DL, VT, RCP, Den);
907 // RCP_HI = mulhu (RCP, Den) */
908 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
910 // NEG_RCP_LO = -RCP_LO
911 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
914 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
915 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
918 // Calculate the rounding error from the URECIP instruction
919 // E = mulhu(ABS_RCP_LO, RCP)
920 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
923 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
926 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
928 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
929 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
932 // Quotient = mulhu(Tmp0, Num)
933 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
935 // Num_S_Remainder = Quotient * Den
936 SDValue Num_S_Remainder = DAG.getNode(ISD::UMULO, DL, VT, Quotient, Den);
938 // Remainder = Num - Num_S_Remainder
939 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
941 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
942 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
943 DAG.getConstant(-1, VT),
944 DAG.getConstant(0, VT),
946 // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
947 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
949 DAG.getConstant(-1, VT),
950 DAG.getConstant(0, VT),
952 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
953 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
956 // Calculate Division result:
958 // Quotient_A_One = Quotient + 1
959 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
960 DAG.getConstant(1, VT));
962 // Quotient_S_One = Quotient - 1
963 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
964 DAG.getConstant(1, VT));
966 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
967 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
968 Quotient, Quotient_A_One, ISD::SETEQ);
970 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
971 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
972 Quotient_S_One, Div, ISD::SETEQ);
974 // Calculate Rem result:
976 // Remainder_S_Den = Remainder - Den
977 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
979 // Remainder_A_Den = Remainder + Den
980 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
982 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
983 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
984 Remainder, Remainder_S_Den, ISD::SETEQ);
986 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
987 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
988 Remainder_A_Den, Rem, ISD::SETEQ);
993 return DAG.getMergeValues(Ops, 2, DL);
996 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
997 SelectionDAG &DAG) const {
998 SDValue S0 = Op.getOperand(0);
1000 if (Op.getValueType() != MVT::f32 || S0.getValueType() != MVT::i64)
1003 // f32 uint_to_fp i64
1004 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
1005 DAG.getConstant(0, MVT::i32));
1006 SDValue FloatLo = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Lo);
1007 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
1008 DAG.getConstant(1, MVT::i32));
1009 SDValue FloatHi = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Hi);
1010 FloatHi = DAG.getNode(ISD::FMUL, DL, MVT::f32, FloatHi,
1011 DAG.getConstantFP(4294967296.0f, MVT::f32)); // 2^32
1012 return DAG.getNode(ISD::FADD, DL, MVT::f32, FloatLo, FloatHi);
1016 SDValue AMDGPUTargetLowering::ExpandSIGN_EXTEND_INREG(SDValue Op,
1018 SelectionDAG &DAG) const {
1019 MVT VT = Op.getSimpleValueType();
1021 SDValue Shift = DAG.getConstant(BitsDiff, VT);
1022 // Shift left by 'Shift' bits.
1023 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Op.getOperand(0), Shift);
1024 // Signed shift Right by 'Shift' bits.
1025 return DAG.getNode(ISD::SRA, DL, VT, Shl, Shift);
1028 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
1029 SelectionDAG &DAG) const {
1030 EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1031 MVT VT = Op.getSimpleValueType();
1032 MVT ScalarVT = VT.getScalarType();
1037 SDValue Src = Op.getOperand(0);
1040 // TODO: Don't scalarize on Evergreen?
1041 unsigned NElts = VT.getVectorNumElements();
1042 SmallVector<SDValue, 8> Args;
1043 DAG.ExtractVectorElements(Src, Args, 0, NElts);
1045 SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType());
1046 for (unsigned I = 0; I < NElts; ++I)
1047 Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp);
1049 return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Args.data(), Args.size());
1052 //===----------------------------------------------------------------------===//
1053 // Custom DAG optimizations
1054 //===----------------------------------------------------------------------===//
1056 static bool isU24(SDValue Op, SelectionDAG &DAG) {
1057 APInt KnownZero, KnownOne;
1058 EVT VT = Op.getValueType();
1059 DAG.ComputeMaskedBits(Op, KnownZero, KnownOne);
1061 return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24;
1064 static bool isI24(SDValue Op, SelectionDAG &DAG) {
1065 EVT VT = Op.getValueType();
1067 // In order for this to be a signed 24-bit value, bit 23, must
1069 return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
1070 // as unsigned 24-bit values.
1071 (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24;
1074 static void simplifyI24(SDValue Op, TargetLowering::DAGCombinerInfo &DCI) {
1076 SelectionDAG &DAG = DCI.DAG;
1077 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1078 EVT VT = Op.getValueType();
1080 APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24);
1081 APInt KnownZero, KnownOne;
1082 TargetLowering::TargetLoweringOpt TLO(DAG, true, true);
1083 if (TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
1084 DCI.CommitTargetLoweringOpt(TLO);
1087 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N,
1088 DAGCombinerInfo &DCI) const {
1089 SelectionDAG &DAG = DCI.DAG;
1092 switch(N->getOpcode()) {
1095 EVT VT = N->getValueType(0);
1096 SDValue N0 = N->getOperand(0);
1097 SDValue N1 = N->getOperand(1);
1100 // FIXME: Add support for 24-bit multiply with 64-bit output on SI.
1101 if (VT.isVector() || VT.getSizeInBits() > 32)
1104 if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) {
1105 N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
1106 N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
1107 Mul = DAG.getNode(AMDGPUISD::MUL_U24, DL, MVT::i32, N0, N1);
1108 } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) {
1109 N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
1110 N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
1111 Mul = DAG.getNode(AMDGPUISD::MUL_I24, DL, MVT::i32, N0, N1);
1116 // We need to use sext even for MUL_U24, because MUL_U24 is used
1117 // for signed multiply of 8 and 16-bit types.
1118 SDValue Reg = DAG.getSExtOrTrunc(Mul, DL, VT);
1122 case AMDGPUISD::MUL_I24:
1123 case AMDGPUISD::MUL_U24: {
1124 SDValue N0 = N->getOperand(0);
1125 SDValue N1 = N->getOperand(1);
1126 simplifyI24(N0, DCI);
1127 simplifyI24(N1, DCI);
1134 //===----------------------------------------------------------------------===//
1136 //===----------------------------------------------------------------------===//
1138 void AMDGPUTargetLowering::getOriginalFunctionArgs(
1141 const SmallVectorImpl<ISD::InputArg> &Ins,
1142 SmallVectorImpl<ISD::InputArg> &OrigIns) const {
1144 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1145 if (Ins[i].ArgVT == Ins[i].VT) {
1146 OrigIns.push_back(Ins[i]);
1151 if (Ins[i].ArgVT.isVector() && !Ins[i].VT.isVector()) {
1152 // Vector has been split into scalars.
1153 VT = Ins[i].ArgVT.getVectorElementType();
1154 } else if (Ins[i].VT.isVector() && Ins[i].ArgVT.isVector() &&
1155 Ins[i].ArgVT.getVectorElementType() !=
1156 Ins[i].VT.getVectorElementType()) {
1157 // Vector elements have been promoted
1160 // Vector has been spilt into smaller vectors.
1164 ISD::InputArg Arg(Ins[i].Flags, VT, VT, Ins[i].Used,
1165 Ins[i].OrigArgIndex, Ins[i].PartOffset);
1166 OrigIns.push_back(Arg);
1170 bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
1171 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
1172 return CFP->isExactlyValue(1.0);
1174 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
1175 return C->isAllOnesValue();
1180 bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
1181 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
1182 return CFP->getValueAPF().isZero();
1184 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
1185 return C->isNullValue();
1190 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1191 const TargetRegisterClass *RC,
1192 unsigned Reg, EVT VT) const {
1193 MachineFunction &MF = DAG.getMachineFunction();
1194 MachineRegisterInfo &MRI = MF.getRegInfo();
1195 unsigned VirtualRegister;
1196 if (!MRI.isLiveIn(Reg)) {
1197 VirtualRegister = MRI.createVirtualRegister(RC);
1198 MRI.addLiveIn(Reg, VirtualRegister);
1200 VirtualRegister = MRI.getLiveInVirtReg(Reg);
1202 return DAG.getRegister(VirtualRegister, VT);
1205 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
1207 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
1211 NODE_NAME_CASE(CALL);
1212 NODE_NAME_CASE(UMUL);
1213 NODE_NAME_CASE(DIV_INF);
1214 NODE_NAME_CASE(RET_FLAG);
1215 NODE_NAME_CASE(BRANCH_COND);
1218 NODE_NAME_CASE(DWORDADDR)
1219 NODE_NAME_CASE(FRACT)
1220 NODE_NAME_CASE(FMAX)
1221 NODE_NAME_CASE(SMAX)
1222 NODE_NAME_CASE(UMAX)
1223 NODE_NAME_CASE(FMIN)
1224 NODE_NAME_CASE(SMIN)
1225 NODE_NAME_CASE(UMIN)
1226 NODE_NAME_CASE(BFE_U32)
1227 NODE_NAME_CASE(BFE_I32)
1230 NODE_NAME_CASE(MUL_U24)
1231 NODE_NAME_CASE(MUL_I24)
1232 NODE_NAME_CASE(URECIP)
1233 NODE_NAME_CASE(DOT4)
1234 NODE_NAME_CASE(EXPORT)
1235 NODE_NAME_CASE(CONST_ADDRESS)
1236 NODE_NAME_CASE(REGISTER_LOAD)
1237 NODE_NAME_CASE(REGISTER_STORE)
1238 NODE_NAME_CASE(LOAD_CONSTANT)
1239 NODE_NAME_CASE(LOAD_INPUT)
1240 NODE_NAME_CASE(SAMPLE)
1241 NODE_NAME_CASE(SAMPLEB)
1242 NODE_NAME_CASE(SAMPLED)
1243 NODE_NAME_CASE(SAMPLEL)
1244 NODE_NAME_CASE(STORE_MSKOR)
1245 NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
1249 static void computeMaskedBitsForMinMax(const SDValue Op0,
1253 const SelectionDAG &DAG,
1255 APInt Op0Zero, Op0One;
1256 APInt Op1Zero, Op1One;
1257 DAG.ComputeMaskedBits(Op0, Op0Zero, Op0One, Depth);
1258 DAG.ComputeMaskedBits(Op1, Op1Zero, Op1One, Depth);
1260 KnownZero = Op0Zero & Op1Zero;
1261 KnownOne = Op0One & Op1One;
1264 void AMDGPUTargetLowering::computeMaskedBitsForTargetNode(
1268 const SelectionDAG &DAG,
1269 unsigned Depth) const {
1271 KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything.
1272 unsigned Opc = Op.getOpcode();
1274 case ISD::INTRINSIC_WO_CHAIN: {
1275 // FIXME: The intrinsic should just use the node.
1276 switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
1277 case AMDGPUIntrinsic::AMDGPU_imax:
1278 case AMDGPUIntrinsic::AMDGPU_umax:
1279 case AMDGPUIntrinsic::AMDGPU_imin:
1280 case AMDGPUIntrinsic::AMDGPU_umin:
1281 computeMaskedBitsForMinMax(Op.getOperand(1), Op.getOperand(2),
1282 KnownZero, KnownOne, DAG, Depth);
1290 case AMDGPUISD::SMAX:
1291 case AMDGPUISD::UMAX:
1292 case AMDGPUISD::SMIN:
1293 case AMDGPUISD::UMIN:
1294 computeMaskedBitsForMinMax(Op.getOperand(0), Op.getOperand(1),
1295 KnownZero, KnownOne, DAG, Depth);