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 "SIMachineFunctionInfo.h"
22 #include "llvm/CodeGen/CallingConvLower.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/SelectionDAG.h"
26 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
27 #include "llvm/IR/DataLayout.h"
31 #include "AMDGPUGenCallingConv.inc"
33 AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM) :
34 TargetLowering(TM, new TargetLoweringObjectFileELF()) {
36 // Initialize target lowering borrowed from AMDIL
39 // We need to custom lower some of the intrinsics
40 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
42 // Library functions. These default to Expand, but we have instructions
44 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
45 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
46 setOperationAction(ISD::FPOW, MVT::f32, Legal);
47 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
48 setOperationAction(ISD::FABS, MVT::f32, Legal);
49 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
50 setOperationAction(ISD::FRINT, MVT::f32, Legal);
52 // The hardware supports ROTR, but not ROTL
53 setOperationAction(ISD::ROTL, MVT::i32, Expand);
55 // Lower floating point store/load to integer store/load to reduce the number
56 // of patterns in tablegen.
57 setOperationAction(ISD::STORE, MVT::f32, Promote);
58 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
60 setOperationAction(ISD::STORE, MVT::v2f32, Promote);
61 AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
63 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
64 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
66 setOperationAction(ISD::STORE, MVT::f64, Promote);
67 AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64);
69 setOperationAction(ISD::LOAD, MVT::f32, Promote);
70 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
72 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
73 AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
75 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
76 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
78 setOperationAction(ISD::LOAD, MVT::f64, Promote);
79 AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64);
81 setOperationAction(ISD::FNEG, MVT::v2f32, Expand);
82 setOperationAction(ISD::FNEG, MVT::v4f32, Expand);
84 setOperationAction(ISD::MUL, MVT::i64, Expand);
86 setOperationAction(ISD::UDIV, MVT::i32, Expand);
87 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
88 setOperationAction(ISD::UREM, MVT::i32, Expand);
89 setOperationAction(ISD::VSELECT, MVT::v2f32, Expand);
90 setOperationAction(ISD::VSELECT, MVT::v4f32, Expand);
92 static const int types[] = {
96 const size_t NumTypes = array_lengthof(types);
98 for (unsigned int x = 0; x < NumTypes; ++x) {
99 MVT::SimpleValueType VT = (MVT::SimpleValueType)types[x];
100 //Expand the following operations for the current type by default
101 setOperationAction(ISD::ADD, VT, Expand);
102 setOperationAction(ISD::AND, VT, Expand);
103 setOperationAction(ISD::MUL, VT, Expand);
104 setOperationAction(ISD::OR, VT, Expand);
105 setOperationAction(ISD::SHL, VT, Expand);
106 setOperationAction(ISD::SRL, VT, Expand);
107 setOperationAction(ISD::SRA, VT, Expand);
108 setOperationAction(ISD::SUB, VT, Expand);
109 setOperationAction(ISD::UDIV, VT, Expand);
110 setOperationAction(ISD::UREM, VT, Expand);
111 setOperationAction(ISD::VSELECT, VT, Expand);
112 setOperationAction(ISD::XOR, VT, Expand);
116 //===---------------------------------------------------------------------===//
117 // TargetLowering Callbacks
118 //===---------------------------------------------------------------------===//
120 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
121 const SmallVectorImpl<ISD::InputArg> &Ins) const {
123 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
126 SDValue AMDGPUTargetLowering::LowerReturn(
128 CallingConv::ID CallConv,
130 const SmallVectorImpl<ISD::OutputArg> &Outs,
131 const SmallVectorImpl<SDValue> &OutVals,
132 SDLoc DL, SelectionDAG &DAG) const {
133 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
136 //===---------------------------------------------------------------------===//
137 // Target specific lowering
138 //===---------------------------------------------------------------------===//
140 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
142 switch (Op.getOpcode()) {
144 Op.getNode()->dump();
145 assert(0 && "Custom lowering code for this"
146 "instruction is not implemented yet!");
148 // AMDIL DAG lowering
149 case ISD::SDIV: return LowerSDIV(Op, DAG);
150 case ISD::SREM: return LowerSREM(Op, DAG);
151 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
152 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
153 // AMDGPU DAG lowering
154 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
155 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
160 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
162 SelectionDAG &DAG) const {
164 const DataLayout *TD = getTargetMachine().getDataLayout();
165 GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
166 // XXX: What does the value of G->getOffset() mean?
167 assert(G->getOffset() == 0 &&
168 "Do not know what to do with an non-zero offset");
170 unsigned Offset = MFI->LDSSize;
171 const GlobalValue *GV = G->getGlobal();
172 uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
174 // XXX: Account for alignment?
175 MFI->LDSSize += Size;
177 return DAG.getConstant(Offset, TD->getPointerSize() == 8 ? MVT::i64 : MVT::i32);
180 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
181 SelectionDAG &DAG) const {
182 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
184 EVT VT = Op.getValueType();
186 switch (IntrinsicID) {
188 case AMDGPUIntrinsic::AMDIL_abs:
189 return LowerIntrinsicIABS(Op, DAG);
190 case AMDGPUIntrinsic::AMDIL_exp:
191 return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
192 case AMDGPUIntrinsic::AMDGPU_lrp:
193 return LowerIntrinsicLRP(Op, DAG);
194 case AMDGPUIntrinsic::AMDIL_fraction:
195 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
196 case AMDGPUIntrinsic::AMDIL_max:
197 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, Op.getOperand(1),
199 case AMDGPUIntrinsic::AMDGPU_imax:
200 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
202 case AMDGPUIntrinsic::AMDGPU_umax:
203 return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
205 case AMDGPUIntrinsic::AMDIL_min:
206 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, Op.getOperand(1),
208 case AMDGPUIntrinsic::AMDGPU_imin:
209 return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
211 case AMDGPUIntrinsic::AMDGPU_umin:
212 return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
214 case AMDGPUIntrinsic::AMDIL_round_nearest:
215 return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
219 ///IABS(a) = SMAX(sub(0, a), a)
220 SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
221 SelectionDAG &DAG) const {
224 EVT VT = Op.getValueType();
225 SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
228 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
231 /// Linear Interpolation
232 /// LRP(a, b, c) = muladd(a, b, (1 - a) * c)
233 SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
234 SelectionDAG &DAG) const {
236 EVT VT = Op.getValueType();
237 SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
238 DAG.getConstantFP(1.0f, MVT::f32),
240 SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
242 return DAG.getNode(ISD::FADD, DL, VT,
243 DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
247 /// \brief Generate Min/Max node
248 SDValue AMDGPUTargetLowering::LowerMinMax(SDValue Op,
249 SelectionDAG &DAG) const {
251 EVT VT = Op.getValueType();
253 SDValue LHS = Op.getOperand(0);
254 SDValue RHS = Op.getOperand(1);
255 SDValue True = Op.getOperand(2);
256 SDValue False = Op.getOperand(3);
257 SDValue CC = Op.getOperand(4);
259 if (VT != MVT::f32 ||
260 !((LHS == True && RHS == False) || (LHS == False && RHS == True))) {
264 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
278 assert(0 && "Operation should already be optimised !");
286 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
288 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
297 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
299 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
301 case ISD::SETCC_INVALID:
302 assert(0 && "Invalid setcc condcode !");
309 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
310 SelectionDAG &DAG) const {
312 EVT VT = Op.getValueType();
314 SDValue Num = Op.getOperand(0);
315 SDValue Den = Op.getOperand(1);
317 SmallVector<SDValue, 8> Results;
319 // RCP = URECIP(Den) = 2^32 / Den + e
320 // e is rounding error.
321 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
323 // RCP_LO = umulo(RCP, Den) */
324 SDValue RCP_LO = DAG.getNode(ISD::UMULO, DL, VT, RCP, Den);
326 // RCP_HI = mulhu (RCP, Den) */
327 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
329 // NEG_RCP_LO = -RCP_LO
330 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
333 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
334 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
337 // Calculate the rounding error from the URECIP instruction
338 // E = mulhu(ABS_RCP_LO, RCP)
339 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
342 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
345 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
347 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
348 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
351 // Quotient = mulhu(Tmp0, Num)
352 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
354 // Num_S_Remainder = Quotient * Den
355 SDValue Num_S_Remainder = DAG.getNode(ISD::UMULO, DL, VT, Quotient, Den);
357 // Remainder = Num - Num_S_Remainder
358 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
360 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
361 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
362 DAG.getConstant(-1, VT),
363 DAG.getConstant(0, VT),
365 // Remainder_GE_Zero = (Remainder >= 0 ? -1 : 0)
366 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Remainder,
367 DAG.getConstant(0, VT),
368 DAG.getConstant(-1, VT),
369 DAG.getConstant(0, VT),
371 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
372 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
375 // Calculate Division result:
377 // Quotient_A_One = Quotient + 1
378 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
379 DAG.getConstant(1, VT));
381 // Quotient_S_One = Quotient - 1
382 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
383 DAG.getConstant(1, VT));
385 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
386 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
387 Quotient, Quotient_A_One, ISD::SETEQ);
389 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
390 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
391 Quotient_S_One, Div, ISD::SETEQ);
393 // Calculate Rem result:
395 // Remainder_S_Den = Remainder - Den
396 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
398 // Remainder_A_Den = Remainder + Den
399 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
401 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
402 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
403 Remainder, Remainder_S_Den, ISD::SETEQ);
405 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
406 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
407 Remainder_A_Den, Rem, ISD::SETEQ);
411 return DAG.getMergeValues(Ops, 2, DL);
414 //===----------------------------------------------------------------------===//
416 //===----------------------------------------------------------------------===//
418 bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
419 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
420 return CFP->isExactlyValue(1.0);
422 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
423 return C->isAllOnesValue();
428 bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
429 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
430 return CFP->getValueAPF().isZero();
432 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
433 return C->isNullValue();
438 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
439 const TargetRegisterClass *RC,
440 unsigned Reg, EVT VT) const {
441 MachineFunction &MF = DAG.getMachineFunction();
442 MachineRegisterInfo &MRI = MF.getRegInfo();
443 unsigned VirtualRegister;
444 if (!MRI.isLiveIn(Reg)) {
445 VirtualRegister = MRI.createVirtualRegister(RC);
446 MRI.addLiveIn(Reg, VirtualRegister);
448 VirtualRegister = MRI.getLiveInVirtReg(Reg);
450 return DAG.getRegister(VirtualRegister, VT);
453 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
455 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
459 NODE_NAME_CASE(CALL);
460 NODE_NAME_CASE(UMUL);
461 NODE_NAME_CASE(DIV_INF);
462 NODE_NAME_CASE(RET_FLAG);
463 NODE_NAME_CASE(BRANCH_COND);
466 NODE_NAME_CASE(DWORDADDR)
467 NODE_NAME_CASE(FRACT)
474 NODE_NAME_CASE(URECIP)
475 NODE_NAME_CASE(EXPORT)
476 NODE_NAME_CASE(CONST_ADDRESS)
477 NODE_NAME_CASE(REGISTER_LOAD)
478 NODE_NAME_CASE(REGISTER_STORE)