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"
32 #include "AMDGPUGenCallingConv.inc"
34 AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM) :
35 TargetLowering(TM, new TargetLoweringObjectFileELF()) {
37 // Initialize target lowering borrowed from AMDIL
40 // We need to custom lower some of the intrinsics
41 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
43 // Library functions. These default to Expand, but we have instructions
45 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
46 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
47 setOperationAction(ISD::FPOW, MVT::f32, Legal);
48 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
49 setOperationAction(ISD::FABS, MVT::f32, Legal);
50 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
51 setOperationAction(ISD::FRINT, MVT::f32, Legal);
53 // The hardware supports ROTR, but not ROTL
54 setOperationAction(ISD::ROTL, MVT::i32, Expand);
56 // Lower floating point store/load to integer store/load to reduce the number
57 // of patterns in tablegen.
58 setOperationAction(ISD::STORE, MVT::f32, Promote);
59 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
61 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
62 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
64 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
65 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
67 setOperationAction(ISD::STORE, MVT::f64, Promote);
68 AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64);
70 setOperationAction(ISD::LOAD, MVT::f32, Promote);
71 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
73 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
74 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
76 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
77 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
79 setOperationAction(ISD::LOAD, MVT::f64, Promote);
80 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64);
82 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Expand);
83 setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Expand);
85 setOperationAction(ISD::FNEG, MVT::v2f32, Expand);
86 setOperationAction(ISD::FNEG, MVT::v4f32, Expand);
88 setOperationAction(ISD::MUL, MVT::i64, Expand);
90 setOperationAction(ISD::UDIV, MVT::i32, Expand);
91 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
92 setOperationAction(ISD::UREM, MVT::i32, Expand);
93 setOperationAction(ISD::VSELECT, MVT::v2f32, Expand);
94 setOperationAction(ISD::VSELECT, MVT::v4f32, Expand);
96 static const int types[] = {
100 const size_t NumTypes = array_lengthof(types);
102 for (unsigned int x = 0; x < NumTypes; ++x) {
103 MVT::SimpleValueType VT = (MVT::SimpleValueType)types[x];
104 //Expand the following operations for the current type by default
105 setOperationAction(ISD::ADD, VT, Expand);
106 setOperationAction(ISD::AND, VT, Expand);
107 setOperationAction(ISD::FP_TO_SINT, VT, Expand);
108 setOperationAction(ISD::FP_TO_UINT, VT, Expand);
109 setOperationAction(ISD::MUL, VT, Expand);
110 setOperationAction(ISD::OR, VT, Expand);
111 setOperationAction(ISD::SHL, VT, Expand);
112 setOperationAction(ISD::SINT_TO_FP, VT, Expand);
113 setOperationAction(ISD::SRL, VT, Expand);
114 setOperationAction(ISD::SRA, VT, Expand);
115 setOperationAction(ISD::SUB, VT, Expand);
116 setOperationAction(ISD::UDIV, VT, Expand);
117 setOperationAction(ISD::UINT_TO_FP, VT, Expand);
118 setOperationAction(ISD::UREM, VT, Expand);
119 setOperationAction(ISD::VSELECT, VT, Expand);
120 setOperationAction(ISD::XOR, VT, Expand);
124 //===---------------------------------------------------------------------===//
126 //===---------------------------------------------------------------------===//
128 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
129 assert(VT.isFloatingPoint());
130 return VT == MVT::f32;
133 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
134 assert(VT.isFloatingPoint());
135 return VT == MVT::f32;
138 //===---------------------------------------------------------------------===//
139 // TargetLowering Callbacks
140 //===---------------------------------------------------------------------===//
142 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
143 const SmallVectorImpl<ISD::InputArg> &Ins) const {
145 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
148 SDValue AMDGPUTargetLowering::LowerReturn(
150 CallingConv::ID CallConv,
152 const SmallVectorImpl<ISD::OutputArg> &Outs,
153 const SmallVectorImpl<SDValue> &OutVals,
154 SDLoc DL, SelectionDAG &DAG) const {
155 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
158 //===---------------------------------------------------------------------===//
159 // Target specific lowering
160 //===---------------------------------------------------------------------===//
162 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
164 switch (Op.getOpcode()) {
166 Op.getNode()->dump();
167 assert(0 && "Custom lowering code for this"
168 "instruction is not implemented yet!");
170 // AMDIL DAG lowering
171 case ISD::SDIV: return LowerSDIV(Op, DAG);
172 case ISD::SREM: return LowerSREM(Op, DAG);
173 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
174 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
175 // AMDGPU DAG lowering
176 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
177 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
182 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
184 SelectionDAG &DAG) const {
186 const DataLayout *TD = getTargetMachine().getDataLayout();
187 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
188 // XXX: What does the value of G->getOffset() mean?
189 assert(G->getOffset() == 0 &&
190 "Do not know what to do with an non-zero offset");
192 unsigned Offset = MFI->LDSSize;
193 const GlobalValue *GV = G->getGlobal();
194 uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
196 // XXX: Account for alignment?
197 MFI->LDSSize += Size;
199 return DAG.getConstant(Offset, TD->getPointerSize() == 8 ? MVT::i64 : MVT::i32);
202 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
203 SelectionDAG &DAG) const {
204 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
206 EVT VT = Op.getValueType();
208 switch (IntrinsicID) {
210 case AMDGPUIntrinsic::AMDIL_abs:
211 return LowerIntrinsicIABS(Op, DAG);
212 case AMDGPUIntrinsic::AMDIL_exp:
213 return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
214 case AMDGPUIntrinsic::AMDGPU_lrp:
215 return LowerIntrinsicLRP(Op, DAG);
216 case AMDGPUIntrinsic::AMDIL_fraction:
217 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
218 case AMDGPUIntrinsic::AMDIL_max:
219 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, Op.getOperand(1),
221 case AMDGPUIntrinsic::AMDGPU_imax:
222 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
224 case AMDGPUIntrinsic::AMDGPU_umax:
225 return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
227 case AMDGPUIntrinsic::AMDIL_min:
228 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, Op.getOperand(1),
230 case AMDGPUIntrinsic::AMDGPU_imin:
231 return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
233 case AMDGPUIntrinsic::AMDGPU_umin:
234 return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
236 case AMDGPUIntrinsic::AMDIL_round_nearest:
237 return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
241 ///IABS(a) = SMAX(sub(0, a), a)
242 SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
243 SelectionDAG &DAG) const {
246 EVT VT = Op.getValueType();
247 SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
250 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
253 /// Linear Interpolation
254 /// LRP(a, b, c) = muladd(a, b, (1 - a) * c)
255 SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
256 SelectionDAG &DAG) const {
258 EVT VT = Op.getValueType();
259 SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
260 DAG.getConstantFP(1.0f, MVT::f32),
262 SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
264 return DAG.getNode(ISD::FADD, DL, VT,
265 DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
269 /// \brief Generate Min/Max node
270 SDValue AMDGPUTargetLowering::LowerMinMax(SDValue Op,
271 SelectionDAG &DAG) const {
273 EVT VT = Op.getValueType();
275 SDValue LHS = Op.getOperand(0);
276 SDValue RHS = Op.getOperand(1);
277 SDValue True = Op.getOperand(2);
278 SDValue False = Op.getOperand(3);
279 SDValue CC = Op.getOperand(4);
281 if (VT != MVT::f32 ||
282 !((LHS == True && RHS == False) || (LHS == False && RHS == True))) {
286 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
300 assert(0 && "Operation should already be optimised !");
308 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
310 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
319 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
321 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
323 case ISD::SETCC_INVALID:
324 assert(0 && "Invalid setcc condcode !");
331 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
332 SelectionDAG &DAG) const {
334 EVT VT = Op.getValueType();
336 SDValue Num = Op.getOperand(0);
337 SDValue Den = Op.getOperand(1);
339 SmallVector<SDValue, 8> Results;
341 // RCP = URECIP(Den) = 2^32 / Den + e
342 // e is rounding error.
343 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
345 // RCP_LO = umulo(RCP, Den) */
346 SDValue RCP_LO = DAG.getNode(ISD::UMULO, DL, VT, RCP, Den);
348 // RCP_HI = mulhu (RCP, Den) */
349 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
351 // NEG_RCP_LO = -RCP_LO
352 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
355 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
356 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
359 // Calculate the rounding error from the URECIP instruction
360 // E = mulhu(ABS_RCP_LO, RCP)
361 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
364 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
367 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
369 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
370 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
373 // Quotient = mulhu(Tmp0, Num)
374 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
376 // Num_S_Remainder = Quotient * Den
377 SDValue Num_S_Remainder = DAG.getNode(ISD::UMULO, DL, VT, Quotient, Den);
379 // Remainder = Num - Num_S_Remainder
380 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
382 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
383 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
384 DAG.getConstant(-1, VT),
385 DAG.getConstant(0, VT),
387 // Remainder_GE_Zero = (Remainder >= 0 ? -1 : 0)
388 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Remainder,
389 DAG.getConstant(0, VT),
390 DAG.getConstant(-1, VT),
391 DAG.getConstant(0, VT),
393 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
394 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
397 // Calculate Division result:
399 // Quotient_A_One = Quotient + 1
400 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
401 DAG.getConstant(1, VT));
403 // Quotient_S_One = Quotient - 1
404 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
405 DAG.getConstant(1, VT));
407 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
408 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
409 Quotient, Quotient_A_One, ISD::SETEQ);
411 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
412 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
413 Quotient_S_One, Div, ISD::SETEQ);
415 // Calculate Rem result:
417 // Remainder_S_Den = Remainder - Den
418 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
420 // Remainder_A_Den = Remainder + Den
421 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
423 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
424 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
425 Remainder, Remainder_S_Den, ISD::SETEQ);
427 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
428 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
429 Remainder_A_Den, Rem, ISD::SETEQ);
433 return DAG.getMergeValues(Ops, 2, DL);
436 //===----------------------------------------------------------------------===//
438 //===----------------------------------------------------------------------===//
440 bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
441 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
442 return CFP->isExactlyValue(1.0);
444 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
445 return C->isAllOnesValue();
450 bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
451 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
452 return CFP->getValueAPF().isZero();
454 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
455 return C->isNullValue();
460 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
461 const TargetRegisterClass *RC,
462 unsigned Reg, EVT VT) const {
463 MachineFunction &MF = DAG.getMachineFunction();
464 MachineRegisterInfo &MRI = MF.getRegInfo();
465 unsigned VirtualRegister;
466 if (!MRI.isLiveIn(Reg)) {
467 VirtualRegister = MRI.createVirtualRegister(RC);
468 MRI.addLiveIn(Reg, VirtualRegister);
470 VirtualRegister = MRI.getLiveInVirtReg(Reg);
472 return DAG.getRegister(VirtualRegister, VT);
475 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
477 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
481 NODE_NAME_CASE(CALL);
482 NODE_NAME_CASE(UMUL);
483 NODE_NAME_CASE(DIV_INF);
484 NODE_NAME_CASE(RET_FLAG);
485 NODE_NAME_CASE(BRANCH_COND);
488 NODE_NAME_CASE(DWORDADDR)
489 NODE_NAME_CASE(FRACT)
496 NODE_NAME_CASE(URECIP)
497 NODE_NAME_CASE(EXPORT)
498 NODE_NAME_CASE(CONST_ADDRESS)
499 NODE_NAME_CASE(REGISTER_LOAD)
500 NODE_NAME_CASE(REGISTER_STORE)