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"
17 #include "AMDGPURegisterInfo.h"
18 #include "AMDILIntrinsicInfo.h"
19 #include "AMDGPUSubtarget.h"
20 #include "llvm/CodeGen/CallingConvLower.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
28 #include "AMDGPUGenCallingConv.inc"
30 AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM) :
31 TargetLowering(TM, new TargetLoweringObjectFileELF()) {
33 // Initialize target lowering borrowed from AMDIL
36 // We need to custom lower some of the intrinsics
37 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
39 // Library functions. These default to Expand, but we have instructions
41 setOperationAction(ISD::FCEIL, MVT::f32, Legal);
42 setOperationAction(ISD::FEXP2, MVT::f32, Legal);
43 setOperationAction(ISD::FPOW, MVT::f32, Legal);
44 setOperationAction(ISD::FLOG2, MVT::f32, Legal);
45 setOperationAction(ISD::FABS, MVT::f32, Legal);
46 setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
47 setOperationAction(ISD::FRINT, MVT::f32, Legal);
49 // Lower floating point store/load to integer store/load to reduce the number
50 // of patterns in tablegen.
51 setOperationAction(ISD::STORE, MVT::f32, Promote);
52 AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
54 setOperationAction(ISD::STORE, MVT::v4f32, Promote);
55 AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
57 setOperationAction(ISD::LOAD, MVT::f32, Promote);
58 AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
60 setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
61 AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
63 setOperationAction(ISD::UDIV, MVT::i32, Expand);
64 setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
65 setOperationAction(ISD::UREM, MVT::i32, Expand);
68 //===---------------------------------------------------------------------===//
69 // TargetLowering Callbacks
70 //===---------------------------------------------------------------------===//
72 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
73 const SmallVectorImpl<ISD::InputArg> &Ins) const {
75 State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
78 SDValue AMDGPUTargetLowering::LowerReturn(
80 CallingConv::ID CallConv,
82 const SmallVectorImpl<ISD::OutputArg> &Outs,
83 const SmallVectorImpl<SDValue> &OutVals,
84 DebugLoc DL, SelectionDAG &DAG) const {
85 return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
88 //===---------------------------------------------------------------------===//
89 // Target specific lowering
90 //===---------------------------------------------------------------------===//
92 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
94 switch (Op.getOpcode()) {
97 assert(0 && "Custom lowering code for this"
98 "instruction is not implemented yet!");
100 // AMDIL DAG lowering
101 case ISD::SDIV: return LowerSDIV(Op, DAG);
102 case ISD::SREM: return LowerSREM(Op, DAG);
103 case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
104 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
105 // AMDGPU DAG lowering
106 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
107 case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
112 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
113 SelectionDAG &DAG) const {
114 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
115 DebugLoc DL = Op.getDebugLoc();
116 EVT VT = Op.getValueType();
118 switch (IntrinsicID) {
120 case AMDGPUIntrinsic::AMDIL_abs:
121 return LowerIntrinsicIABS(Op, DAG);
122 case AMDGPUIntrinsic::AMDIL_exp:
123 return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
124 case AMDGPUIntrinsic::AMDGPU_lrp:
125 return LowerIntrinsicLRP(Op, DAG);
126 case AMDGPUIntrinsic::AMDIL_fraction:
127 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
128 case AMDGPUIntrinsic::AMDIL_max:
129 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, Op.getOperand(1),
131 case AMDGPUIntrinsic::AMDGPU_imax:
132 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
134 case AMDGPUIntrinsic::AMDGPU_umax:
135 return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
137 case AMDGPUIntrinsic::AMDIL_min:
138 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, Op.getOperand(1),
140 case AMDGPUIntrinsic::AMDGPU_imin:
141 return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
143 case AMDGPUIntrinsic::AMDGPU_umin:
144 return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
146 case AMDGPUIntrinsic::AMDIL_round_nearest:
147 return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
151 ///IABS(a) = SMAX(sub(0, a), a)
152 SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
153 SelectionDAG &DAG) const {
155 DebugLoc DL = Op.getDebugLoc();
156 EVT VT = Op.getValueType();
157 SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
160 return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
163 /// Linear Interpolation
164 /// LRP(a, b, c) = muladd(a, b, (1 - a) * c)
165 SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
166 SelectionDAG &DAG) const {
167 DebugLoc DL = Op.getDebugLoc();
168 EVT VT = Op.getValueType();
169 SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
170 DAG.getConstantFP(1.0f, MVT::f32),
172 SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
174 return DAG.getNode(ISD::FADD, DL, VT,
175 DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
179 /// \brief Generate Min/Max node
180 SDValue AMDGPUTargetLowering::LowerMinMax(SDValue Op,
181 SelectionDAG &DAG) const {
182 DebugLoc DL = Op.getDebugLoc();
183 EVT VT = Op.getValueType();
185 SDValue LHS = Op.getOperand(0);
186 SDValue RHS = Op.getOperand(1);
187 SDValue True = Op.getOperand(2);
188 SDValue False = Op.getOperand(3);
189 SDValue CC = Op.getOperand(4);
191 if (VT != MVT::f32 ||
192 !((LHS == True && RHS == False) || (LHS == False && RHS == True))) {
196 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
210 assert(0 && "Operation should already be optimised !");
218 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
220 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
229 return DAG.getNode(AMDGPUISD::FMAX, DL, VT, LHS, RHS);
231 return DAG.getNode(AMDGPUISD::FMIN, DL, VT, LHS, RHS);
233 case ISD::SETCC_INVALID:
234 assert(0 && "Invalid setcc condcode !");
241 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
242 SelectionDAG &DAG) const {
243 DebugLoc DL = Op.getDebugLoc();
244 EVT VT = Op.getValueType();
246 SDValue Num = Op.getOperand(0);
247 SDValue Den = Op.getOperand(1);
249 SmallVector<SDValue, 8> Results;
251 // RCP = URECIP(Den) = 2^32 / Den + e
252 // e is rounding error.
253 SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
255 // RCP_LO = umulo(RCP, Den) */
256 SDValue RCP_LO = DAG.getNode(ISD::UMULO, DL, VT, RCP, Den);
258 // RCP_HI = mulhu (RCP, Den) */
259 SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
261 // NEG_RCP_LO = -RCP_LO
262 SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
265 // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
266 SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
269 // Calculate the rounding error from the URECIP instruction
270 // E = mulhu(ABS_RCP_LO, RCP)
271 SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
274 SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
277 SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
279 // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
280 SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
283 // Quotient = mulhu(Tmp0, Num)
284 SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
286 // Num_S_Remainder = Quotient * Den
287 SDValue Num_S_Remainder = DAG.getNode(ISD::UMULO, DL, VT, Quotient, Den);
289 // Remainder = Num - Num_S_Remainder
290 SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
292 // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
293 SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
294 DAG.getConstant(-1, VT),
295 DAG.getConstant(0, VT),
297 // Remainder_GE_Zero = (Remainder >= 0 ? -1 : 0)
298 SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Remainder,
299 DAG.getConstant(0, VT),
300 DAG.getConstant(-1, VT),
301 DAG.getConstant(0, VT),
303 // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
304 SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
307 // Calculate Division result:
309 // Quotient_A_One = Quotient + 1
310 SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
311 DAG.getConstant(1, VT));
313 // Quotient_S_One = Quotient - 1
314 SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
315 DAG.getConstant(1, VT));
317 // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
318 SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
319 Quotient, Quotient_A_One, ISD::SETEQ);
321 // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
322 Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
323 Quotient_S_One, Div, ISD::SETEQ);
325 // Calculate Rem result:
327 // Remainder_S_Den = Remainder - Den
328 SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
330 // Remainder_A_Den = Remainder + Den
331 SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
333 // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
334 SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
335 Remainder, Remainder_S_Den, ISD::SETEQ);
337 // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
338 Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
339 Remainder_A_Den, Rem, ISD::SETEQ);
343 return DAG.getMergeValues(Ops, 2, DL);
346 //===----------------------------------------------------------------------===//
348 //===----------------------------------------------------------------------===//
350 bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
351 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
352 return CFP->isExactlyValue(1.0);
354 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
355 return C->isAllOnesValue();
360 bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
361 if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
362 return CFP->getValueAPF().isZero();
364 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
365 return C->isNullValue();
370 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
371 const TargetRegisterClass *RC,
372 unsigned Reg, EVT VT) const {
373 MachineFunction &MF = DAG.getMachineFunction();
374 MachineRegisterInfo &MRI = MF.getRegInfo();
375 unsigned VirtualRegister;
376 if (!MRI.isLiveIn(Reg)) {
377 VirtualRegister = MRI.createVirtualRegister(RC);
378 MRI.addLiveIn(Reg, VirtualRegister);
380 VirtualRegister = MRI.getLiveInVirtReg(Reg);
382 return DAG.getRegister(VirtualRegister, VT);
385 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
387 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
391 NODE_NAME_CASE(CALL);
392 NODE_NAME_CASE(UMUL);
393 NODE_NAME_CASE(DIV_INF);
394 NODE_NAME_CASE(RET_FLAG);
395 NODE_NAME_CASE(BRANCH_COND);
398 NODE_NAME_CASE(DWORDADDR)
399 NODE_NAME_CASE(FRACT)
406 NODE_NAME_CASE(URECIP)
407 NODE_NAME_CASE(EXPORT)
408 NODE_NAME_CASE(CONST_ADDRESS)
409 NODE_NAME_CASE(REGISTER_LOAD)
410 NODE_NAME_CASE(REGISTER_STORE)